py_graph.h 18 KB

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