py_graph.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  1. #ifndef _PY_GRAPH_MOD_H
  2. #define _PY_GRAPH_MOD_H
  3. #define PY_SSIZE_T_CLEAN
  4. #include <Python.h>
  5. #include <structmember.h>
  6. #include "graph.h"
  7. #include "codec_nt.h"
  8. #include "py_triple.h"
  9. /*
  10. * Iterator helpers.
  11. */
  12. /*
  13. * String iterator for encoder output.
  14. *
  15. * Yields one string (one or more lines) at a time.
  16. */
  17. typedef struct {
  18. PyObject_HEAD
  19. LSUP_CodecIterator *it;
  20. unsigned char *line;
  21. } StringIteratorObject;
  22. static void
  23. StringIterator_dealloc (StringIteratorObject *it_obj)
  24. { it_obj->it->codec->encode_graph_done (it_obj->it); }
  25. static PyObject *
  26. StringIterator_next (StringIteratorObject *it_obj)
  27. {
  28. LSUP_rc rc = it_obj->it->codec->encode_graph_iter (
  29. it_obj->it, &it_obj->line);
  30. if (rc != LSUP_OK) {
  31. if (rc != LSUP_END)
  32. PyErr_SetString (PyExc_ValueError, "Error encoding graph.");
  33. // If not an error, this raises StopIteration.
  34. return NULL;
  35. }
  36. return PyUnicode_FromString ((char *) it_obj->line);
  37. }
  38. /*
  39. * String iterator type.
  40. *
  41. * Objects of this type are never generated from Python code, rather from
  42. * Graph_encode, hence the type has no special new or init function.
  43. */
  44. PyTypeObject StringIteratorType = {
  45. PyVarObject_HEAD_INIT(&PyType_Type, 0)
  46. .tp_name = "graph.StringIterator",
  47. .tp_basicsize = sizeof (StringIteratorObject),
  48. .tp_itemsize = 0,
  49. .tp_flags = Py_TPFLAGS_DEFAULT,
  50. .tp_dealloc = (destructor) StringIterator_dealloc,
  51. .tp_iter = PyObject_SelfIter,
  52. .tp_iternext = (iternextfunc)StringIterator_next,
  53. };
  54. /*
  55. * Graph iterator.
  56. *
  57. * Yields one triple at a time.
  58. */
  59. typedef struct {
  60. PyObject_HEAD
  61. LSUP_GraphIterator *it;
  62. LSUP_Triple *spo;
  63. } GraphIteratorObject;
  64. static void
  65. GraphIterator_dealloc (GraphIteratorObject *it_obj)
  66. {
  67. LSUP_graph_iter_free (it_obj->it);
  68. free (it_obj->spo);
  69. }
  70. static PyObject *
  71. GraphIterator_next (GraphIteratorObject *it_obj)
  72. {
  73. LSUP_rc rc = LSUP_graph_iter_next (it_obj->it, it_obj->spo);
  74. if (rc != LSUP_OK) {
  75. if (rc != LSUP_END)
  76. PyErr_SetString (PyExc_ValueError, "Error encoding graph.");
  77. // If not an error, this raises StopIteration.
  78. return NULL;
  79. }
  80. return build_triple (it_obj->spo);
  81. }
  82. /*
  83. * Graph iterator type.
  84. */
  85. PyTypeObject GraphIteratorType = {
  86. PyVarObject_HEAD_INIT(&PyType_Type, 0)
  87. .tp_name = "graph.GraphIterator",
  88. .tp_basicsize = sizeof (GraphIteratorObject),
  89. .tp_itemsize = 0,
  90. .tp_flags = Py_TPFLAGS_DEFAULT,
  91. .tp_dealloc = (destructor) GraphIterator_dealloc,
  92. .tp_iter = PyObject_SelfIter,
  93. .tp_iternext = (iternextfunc) GraphIterator_next,
  94. };
  95. /*
  96. * Graph stuff.
  97. */
  98. typedef struct {
  99. PyObject_HEAD
  100. LSUP_Graph *ob_struct;
  101. } GraphObject;
  102. static int
  103. Graph_init (GraphObject *self, PyObject *args, PyObject *kwargs)
  104. {
  105. unsigned char store_type;
  106. PyObject *uri_obj = NULL;
  107. LSUP_Term *uri = NULL, *src_uri = NULL;
  108. static char *kwlist[] = {"", "uri_obj", NULL};
  109. if (!PyArg_ParseTupleAndKeywords (
  110. args, kwargs, "b|O", kwlist, &store_type, &uri_obj))
  111. return -1;
  112. if (uri_obj) {
  113. if (!PyObject_TypeCheck (uri_obj, &TermType)) {
  114. PyErr_SetString (PyExc_TypeError, "uri is not a Term type.");
  115. return -1;
  116. }
  117. src_uri = ((TermObject *) uri_obj)->ob_struct;
  118. uri = LSUP_iriref_new (src_uri->data, LSUP_iriref_nsm (src_uri));
  119. if (! LSUP_IS_IRI (uri)) {
  120. PyErr_SetString (PyExc_TypeError, "uri is not a IRIREF type.");
  121. return -1;
  122. }
  123. } else uri = LSUP_iriref_new (NULL, NULL);
  124. // Set up the store if a function for that is defined.
  125. const LSUP_StoreInt *sif = LSUP_store_int (store_type);
  126. if (UNLIKELY (!sif)) {
  127. PyErr_SetString (
  128. PyExc_TypeError,
  129. "No interface defined for given store type.");
  130. return -1;
  131. }
  132. if (sif->setup_fn) {
  133. if (sif->setup_fn(NULL, false) < LSUP_OK) {
  134. PyErr_SetString (
  135. PyExc_IOError, "Error initializing back end store.");
  136. return -1;
  137. }
  138. }
  139. // TODO Make store ID, nsm and initial size accessible.
  140. self->ob_struct = LSUP_graph_new (
  141. uri, (LSUP_StoreType) store_type, NULL, NULL, 0);
  142. if (!self->ob_struct) {
  143. PyErr_SetString (PyExc_ValueError, "Could not create graph.");
  144. return -1;
  145. }
  146. LSUP_Term *uri2 = LSUP_graph_uri (self->ob_struct);
  147. log_debug("Graph URI (%p): %s", uri2, uri2->data);
  148. return 0;
  149. }
  150. static void
  151. Graph_dealloc (GraphObject *self)
  152. {
  153. LSUP_graph_free (self->ob_struct);
  154. Py_TYPE (self)->tp_free ((PyObject *) self);
  155. }
  156. static PyObject *
  157. Graph_get_uri (GraphObject *self, void *closure)
  158. {
  159. LSUP_Term *uri = LSUP_graph_uri (self->ob_struct);
  160. log_debug("Graph URI address: %p", uri);
  161. log_debug("Graph URI: %s", uri->data);
  162. return PyUnicode_FromString (uri->data);
  163. }
  164. static int
  165. Graph_set_uri (GraphObject *self, PyObject *value, void *closure)
  166. {
  167. if (!PyObject_TypeCheck (value, &TermType)) {
  168. PyErr_SetString (PyExc_TypeError, "URI is not a Term type.");
  169. return -1;
  170. }
  171. LSUP_Term *gr_uri = ((TermObject*)value)->ob_struct;
  172. log_debug ("New graph URI: %s", (gr_uri->data));
  173. LSUP_rc rc = LSUP_graph_set_uri (self->ob_struct, LSUP_term_copy (gr_uri));
  174. return rc == LSUP_OK ? 0 : -1;
  175. }
  176. static PyGetSetDef Graph_getsetters[] = {
  177. {
  178. "uri", (getter) Graph_get_uri, (setter) Graph_set_uri,
  179. "Graph URI.", NULL
  180. },
  181. {NULL}
  182. };
  183. static int
  184. Graph_copy_contents (GraphObject *self, GraphObject *dest)
  185. {
  186. if (LSUP_graph_copy_contents (self->ob_struct, dest->ob_struct) < LSUP_OK)
  187. {
  188. PyErr_SetString (PyExc_ValueError, "Error copying graph contents.");
  189. return -1;
  190. }
  191. return 0;
  192. };
  193. #if 0
  194. static PyObject *
  195. Graph_store (PyObject *self)
  196. {
  197. GraphObject *dest_obj = (GraphObject *) Py_TYPE (self)->tp_alloc(
  198. Py_TYPE (self), 0);
  199. if (!dest_obj) return PyErr_NoMemory();
  200. // TODO Make store ID, nsm and initial size accessible.
  201. LSUP_Graph *dest = LSUP_graph_new (
  202. uri, LSUP_STORE_HTABLE, NULL, NULL, 0);
  203. if (!dest) {
  204. PyErr_SetString (PyExc_ValueError, "Could not create graph.");
  205. return -1;
  206. }
  207. LSUP_rc rc = LSUP_GraphStore (
  208. ((GraphObject *) self)->ob_struct, &((dest_obj)->ob_struct), NULL);
  209. if (rc != LSUP_OK) {
  210. log_error (LSUP_strerror (rc));
  211. PyErr_SetString (PyExc_SystemError, "Error storing graph.");
  212. return NULL;
  213. };
  214. Py_INCREF (dest_obj);
  215. return (PyObject *)dest_obj;
  216. }
  217. #endif
  218. static PyObject *
  219. Graph_new_from_rdf (PyTypeObject *cls, PyObject *args)
  220. {
  221. PyObject *buf, *fileno_fn, *fileno_obj;
  222. const char *type;
  223. if (! PyArg_ParseTuple (args, "Os", &buf, &type)) return NULL;
  224. // Get the file descriptor from the Python BufferedIO object.
  225. // FIXME This is not sure to be reliable. See
  226. // https://docs.python.org/3/library/io.html?highlight=io%20bufferedreader#io.IOBase.fileno
  227. if (! (fileno_fn = PyObject_GetAttrString (buf, "fileno"))) {
  228. PyErr_SetString (PyExc_TypeError, "Object has no fileno function.");
  229. return NULL;
  230. }
  231. PyObject* fileno_args = PyTuple_New(0);
  232. if (! (fileno_obj = PyObject_CallObject (fileno_fn, fileno_args))) {
  233. PyErr_SetString (PyExc_SystemError, "Error calling fileno function.");
  234. return NULL;
  235. }
  236. int fd = PyLong_AsSize_t (fileno_obj);
  237. /*
  238. * From the Linux man page:
  239. *
  240. * > The file descriptor is not dup'ed, and will be closed when the stream
  241. * > created by fdopen() is closed. The result of applying fdopen() to a
  242. * > shared memory object is undefined.
  243. *
  244. * Hence the `dup()`.
  245. */
  246. fd = dup (fd);
  247. FILE *fh = fdopen (fd, "r");
  248. GraphObject *res = (GraphObject *) cls->tp_alloc(cls, 0);
  249. if (!res) return PyErr_NoMemory();
  250. const LSUP_Codec *codec;
  251. if (strcmp(type, "nt") == 0) codec = &nt_codec;
  252. // TODO other codecs here.
  253. else {
  254. PyErr_SetString (PyExc_ValueError, "Unsupported codec.");
  255. return NULL;
  256. }
  257. size_t ct;
  258. char *err;
  259. codec->decode_graph (fh, &res->ob_struct, &ct, &err);
  260. fclose (fh);
  261. log_debug ("Decoded %lu triples.", ct);
  262. if (UNLIKELY (err)) {
  263. PyErr_SetString (PyExc_IOError, err);
  264. return NULL;
  265. }
  266. Py_INCREF (res);
  267. return (PyObject *) res;
  268. }
  269. /** @brief Build a triple pattern for lookup purposes.
  270. */
  271. inline static int build_trp_pattern (PyObject *args, LSUP_Term *spo[])
  272. {
  273. PyObject *s_obj, *p_obj, *o_obj;
  274. if (! (PyArg_ParseTuple (args, "OOO", &s_obj, &p_obj, &o_obj)))
  275. return -1;
  276. if (s_obj != Py_None && !PyObject_TypeCheck (s_obj, &TermType)) {
  277. PyErr_SetString (PyExc_TypeError, "Subject must be a term or None.");
  278. return -1;
  279. }
  280. if (p_obj != Py_None && !PyObject_TypeCheck (p_obj, &TermType)) {
  281. PyErr_SetString (PyExc_TypeError, "Predicate must be a term or None.");
  282. return -1;
  283. }
  284. if (o_obj != Py_None && !PyObject_TypeCheck (o_obj, &TermType)) {
  285. PyErr_SetString (PyExc_TypeError, "Object must be a term or None.");
  286. return -1;
  287. }
  288. spo[0] = s_obj != Py_None ? ((TermObject *)s_obj)->ob_struct : NULL;
  289. spo[1] = p_obj != Py_None ? ((TermObject *)p_obj)->ob_struct : NULL;
  290. spo[2] = o_obj != Py_None ? ((TermObject *)o_obj)->ob_struct : NULL;
  291. return 0;
  292. }
  293. /*
  294. static PyObject *
  295. Graph_new_set_from_store_lookup (PyTypeObject *cls, PyObject *args)
  296. {
  297. LSUP_Term *spo[3];
  298. if (UNLIKELY ((build_trp_pattern (args, spo)) < 0)) return NULL;
  299. LSUP_Graph **gr_a = LSUP_graph_new_lookup (spo[0], spo[1], spo[2]);
  300. if (UNLIKELY (!gr_a)) {
  301. // TODO implement LSUP_strerror for more details.
  302. PyErr_SetString (PyExc_SystemError, "Error looking up triples.");
  303. return NULL;
  304. }
  305. PyObject *ret = PySet_New (NULL);
  306. size_t i;
  307. for (i = 0; gr_a[i] != NULL; i++) {
  308. PyObject *gr_obj = cls->tp_alloc(cls, 0);
  309. if (!gr_obj) return NULL;
  310. ((GraphObject *) gr_obj)->ob_struct = gr_a[i];
  311. Py_INCREF (gr_obj);
  312. PySet_Add (ret, gr_obj);
  313. }
  314. log_debug ("Found %lu graphs for pattern.", i + 1);
  315. Py_INCREF (ret);
  316. return ret;
  317. }
  318. */
  319. static PyObject *
  320. Graph_richcmp (PyObject *self, PyObject *other, int op)
  321. {
  322. // Only equality and non-equality are supported.
  323. if (op != Py_EQ && op != Py_NE) Py_RETURN_NOTIMPLEMENTED;
  324. LSUP_Graph *t1 = ((GraphObject *) self)->ob_struct;
  325. LSUP_Graph *t2 = ((GraphObject *) other)->ob_struct;
  326. if (LSUP_graph_equals (t1, t2) ^ (op == Py_NE)) Py_RETURN_TRUE;
  327. Py_RETURN_FALSE;
  328. }
  329. static inline PyObject *
  330. Graph_bool_op (
  331. PyTypeObject *cls, LSUP_bool_op op, PyObject *gr1, PyObject *gr2)
  332. {
  333. if (! PyObject_TypeCheck (gr1, cls) || ! PyObject_TypeCheck (gr2, cls))
  334. return NULL;
  335. GraphObject *res = (GraphObject *) cls->tp_alloc (cls, 0);
  336. if (!res) return NULL;
  337. LSUP_Graph *dest = LSUP_graph_new (
  338. NULL, LSUP_STORE_HTABLE, NULL, NULL, 0);
  339. if (!dest) {
  340. PyErr_SetString (PyExc_Exception, "Could not create destination graph.");
  341. return NULL;
  342. }
  343. LSUP_rc rc = LSUP_graph_bool_op (
  344. op, ((GraphObject *) gr1)->ob_struct,
  345. ((GraphObject *) gr2)->ob_struct, res->ob_struct);
  346. if (rc < LSUP_OK) {
  347. PyErr_SetString (PyExc_Exception, "Error performing boolean operation.");
  348. return NULL;
  349. }
  350. Py_INCREF(res);
  351. return (PyObject *) res;
  352. }
  353. static PyObject *
  354. Graph_add (PyObject *self, PyObject *triples)
  355. {
  356. // Triple may be any iterable.
  357. PyObject *iter = PyObject_GetIter (triples);
  358. if (! iter) {
  359. PyErr_SetString (
  360. PyExc_ValueError, "Triples object cannot be iterated.");
  361. return NULL;
  362. }
  363. PyObject *trp_obj;
  364. int rc = 0;
  365. size_t ct = 0;
  366. LSUP_GraphIterator *it = LSUP_graph_add_init (
  367. ((GraphObject *)self)->ob_struct);
  368. while ((trp_obj = PyIter_Next (iter))) {
  369. if (!PyObject_TypeCheck (trp_obj, &TripleType)) {
  370. PyErr_SetString (
  371. PyExc_ValueError, "Object is not a triple.");
  372. rc = -1;
  373. goto finally;
  374. }
  375. log_trace ("Inserting triple #%lu", ct);
  376. LSUP_rc db_rc = LSUP_graph_add_iter (
  377. it, ((TripleObject *) trp_obj)->ob_struct);
  378. if (db_rc == LSUP_OK) {
  379. rc = LSUP_OK;
  380. ct++;
  381. } else if (UNLIKELY (db_rc < 0)) {
  382. PyErr_SetString (PyExc_ValueError, "Error while adding triples.");
  383. rc = -1;
  384. goto finally;
  385. }
  386. // If db_rc > 0, it's a no-op and the counter is not increased.
  387. }
  388. finally:
  389. LSUP_graph_add_done (it);
  390. if (rc == LSUP_OK) return PyLong_FromSize_t (ct);
  391. return NULL;
  392. }
  393. static PyObject *Graph_remove (PyObject *self, PyObject *args)
  394. {
  395. LSUP_rc rc;
  396. LSUP_Term *spo[3];
  397. rc = build_trp_pattern (args, spo);
  398. if (rc < 0) goto finally;
  399. size_t ct;
  400. rc = LSUP_graph_remove (
  401. ((GraphObject *)self)->ob_struct, spo[0], spo[1], spo[2], &ct);
  402. if (rc < 0) {
  403. // TODO implement strerror for more details.
  404. PyErr_SetString (PyExc_SystemError, "Error removing triples.");
  405. goto finally;
  406. }
  407. log_debug ("Removed %lu triples.", ct);
  408. finally:
  409. if (rc < 0) return NULL;
  410. Py_RETURN_NONE;
  411. }
  412. static PyObject *Graph_lookup (PyObject *self, PyObject *args)
  413. {
  414. LSUP_rc rc;
  415. GraphIteratorObject *it_obj = NULL;
  416. LSUP_Term *spo[3];
  417. rc = build_trp_pattern (args, spo);
  418. if (UNLIKELY (rc < 0)) goto finally;
  419. size_t ct;
  420. LSUP_GraphIterator *it = LSUP_graph_lookup (
  421. ((GraphObject *)self)->ob_struct, spo[0], spo[1], spo[2], &ct);
  422. if (UNLIKELY (!it)) {
  423. // TODO implement LSUP_strerror for more details.
  424. PyErr_SetString (PyExc_SystemError, "Error looking up triples.");
  425. rc = -1;
  426. goto finally;
  427. }
  428. log_debug ("Found %lu triples.", ct);
  429. // Initialize the generator object.
  430. it_obj = PyObject_New (
  431. GraphIteratorObject, &GraphIteratorType);
  432. if (UNLIKELY (!it_obj)) return PyErr_NoMemory();
  433. it_obj->it = it;
  434. it_obj->spo = TRP_DUMMY;
  435. Py_INCREF (it_obj);
  436. finally:
  437. return (PyObject *)it_obj;
  438. }
  439. static PyObject *
  440. Graph_encode (PyObject *self, PyObject *args)
  441. {
  442. const char *type;
  443. if (! PyArg_ParseTuple (args, "s", &type)) return NULL;
  444. const LSUP_Codec *codec;
  445. if (strcmp(type, "nt") == 0) codec = &nt_codec;
  446. // TODO other codecs here.
  447. else {
  448. PyErr_SetString (PyExc_ValueError, "Unsupported codec.");
  449. return NULL;
  450. }
  451. LSUP_CodecIterator *it = codec->encode_graph_init (
  452. ((GraphObject *)self)->ob_struct);
  453. // Initialize the generator object.
  454. StringIteratorObject *it_obj = PyObject_New (
  455. StringIteratorObject, &StringIteratorType);
  456. if (!it_obj) return NULL;
  457. it_obj->it = it;
  458. it_obj->line = NULL;
  459. Py_INCREF (it_obj);
  460. return (PyObject *)it_obj;
  461. }
  462. static PyMethodDef Graph_methods[] = {
  463. {
  464. "copy", (PyCFunction) Graph_copy_contents, METH_CLASS | METH_VARARGS,
  465. "Copy the contents of a graph into another."
  466. },
  467. {
  468. "from_rdf", (PyCFunction) Graph_new_from_rdf,
  469. METH_CLASS | METH_VARARGS,
  470. "Create a graph from a RDF file."
  471. },
  472. /*
  473. {
  474. "from_lookup", (PyCFunction) Graph_new_set_from_store_lookup,
  475. METH_CLASS | METH_VARARGS,
  476. "Create a set of graphs from a store SPO lookup."
  477. },
  478. */
  479. /*
  480. {
  481. "store", (PyCFunction) Graph_store, METH_NOARGS,
  482. "Store a graph into the permanent back end."
  483. },
  484. */
  485. {"add", (PyCFunction) Graph_add, METH_O, "Add triples to a graph."},
  486. {
  487. "remove", (PyCFunction) Graph_remove, METH_VARARGS,
  488. "Remove triples from a graph by matching a pattern."
  489. },
  490. {
  491. "lookup", (PyCFunction) Graph_lookup, METH_VARARGS,
  492. "Look triples in a graph by matching a pattern."
  493. },
  494. {
  495. "to_rdf", (PyCFunction) Graph_encode, METH_VARARGS,
  496. "Encode a graph into a RDF byte buffer."
  497. },
  498. {NULL},
  499. };
  500. static inline PyObject *Graph_bool_and (
  501. PyTypeObject *cls, PyObject *gr1, PyObject *gr2)
  502. { return Graph_bool_op (cls, LSUP_BOOL_INTERSECTION, gr1, gr2); }
  503. static inline PyObject *Graph_bool_or (
  504. PyTypeObject *cls, PyObject *gr1, PyObject *gr2)
  505. { return Graph_bool_op (cls, LSUP_BOOL_UNION, gr1, gr2); }
  506. static inline PyObject *Graph_bool_subtract (
  507. PyTypeObject *cls, PyObject *gr1, PyObject *gr2)
  508. { return Graph_bool_op (cls, LSUP_BOOL_SUBTRACTION, gr1, gr2); }
  509. static inline PyObject *Graph_bool_xor (
  510. PyTypeObject *cls, PyObject *gr1, PyObject *gr2)
  511. { return Graph_bool_op (cls, LSUP_BOOL_XOR, gr1, gr2); }
  512. static PyNumberMethods Graph_number_methods = {
  513. .nb_and = (binaryfunc) Graph_bool_and,
  514. .nb_or = (binaryfunc) Graph_bool_or,
  515. .nb_subtract = (binaryfunc) Graph_bool_subtract,
  516. .nb_xor = (binaryfunc) Graph_bool_xor,
  517. };
  518. static int
  519. Graph_contains (PyObject *self, PyObject *value)
  520. {
  521. if (!PyObject_TypeCheck (value, &TripleType)) {
  522. PyErr_SetString (PyExc_ValueError, "Error parsing input value.");
  523. return -1;
  524. }
  525. int rc = LSUP_graph_contains (
  526. ((GraphObject *) self)->ob_struct,
  527. ((TripleObject *) value)->ob_struct);
  528. return rc;
  529. }
  530. static Py_ssize_t
  531. Graph_get_size (PyObject *self)
  532. { return LSUP_graph_size (((GraphObject *) self)->ob_struct); }
  533. static PySequenceMethods Graph_seq_methods = {
  534. .sq_length = (lenfunc) Graph_get_size,
  535. .sq_contains = (objobjproc) Graph_contains,
  536. };
  537. PyTypeObject GraphType = {
  538. PyVarObject_HEAD_INIT(NULL, 0)
  539. .tp_name = "graph.Graph",
  540. .tp_doc = "RDF graph",
  541. .tp_basicsize = sizeof (GraphObject),
  542. .tp_itemsize = 0,
  543. .tp_flags = Py_TPFLAGS_DEFAULT,
  544. .tp_new = PyType_GenericNew,
  545. .tp_init = (initproc) Graph_init,
  546. .tp_dealloc = (destructor) Graph_dealloc,
  547. .tp_getset = Graph_getsetters,
  548. .tp_methods = Graph_methods,
  549. .tp_richcompare = (richcmpfunc) Graph_richcmp,
  550. .tp_as_number = &Graph_number_methods,
  551. .tp_as_sequence = &Graph_seq_methods,
  552. };
  553. #endif