py_graph.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  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. }
  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. // TODO Move store creation fn and handle into a separate module.
  135. LSUP_Store *store = LSUP_store_new (store_type, NULL, 0);
  136. if (sif->setup_fn) {
  137. if (sif->setup_fn(NULL, false) < LSUP_OK) {
  138. PyErr_SetString (
  139. PyExc_IOError, "Error initializing back end store.");
  140. return -1;
  141. }
  142. }
  143. // TODO Make store ID, nsm and initial size accessible.
  144. self->ob_struct = LSUP_graph_new (store, uri, NULL);
  145. if (!self->ob_struct) {
  146. PyErr_SetString (PyExc_ValueError, "Could not create graph.");
  147. return -1;
  148. }
  149. if (uri) LSUP_term_free (uri);
  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. const 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 PyObject *
  186. Graph_new_from_rdf (PyTypeObject *cls, PyObject *args)
  187. {
  188. PyObject *buf, *fileno_fn, *fileno_obj;
  189. const char *type;
  190. if (! PyArg_ParseTuple (args, "Os", &buf, &type)) return NULL;
  191. // Get the file descriptor from the Python BufferedIO object.
  192. // FIXME This is not sure to be reliable. See
  193. // https://docs.python.org/3/library/io.html?highlight=io%20bufferedreader#io.IOBase.fileno
  194. if (! (fileno_fn = PyObject_GetAttrString (buf, "fileno"))) {
  195. PyErr_SetString (PyExc_TypeError, "Object has no fileno function.");
  196. return NULL;
  197. }
  198. PyObject* fileno_args = PyTuple_New(0);
  199. if (! (fileno_obj = PyObject_CallObject (fileno_fn, fileno_args))) {
  200. PyErr_SetString (PyExc_SystemError, "Error calling fileno function.");
  201. return NULL;
  202. }
  203. int fd = PyLong_AsSize_t (fileno_obj);
  204. /*
  205. * From the Linux man page:
  206. *
  207. * > The file descriptor is not dup'ed, and will be closed when the stream
  208. * > created by fdopen() is closed. The result of applying fdopen() to a
  209. * > shared memory object is undefined.
  210. *
  211. * Hence the `dup()`.
  212. */
  213. fd = dup (fd);
  214. FILE *fh = fdopen (fd, "r");
  215. GraphObject *res = (GraphObject *) cls->tp_alloc(cls, 0);
  216. if (!res) return PyErr_NoMemory();
  217. const LSUP_Codec *codec;
  218. if (strcmp(type, "nt") == 0) codec = &nt_codec;
  219. else if (strcmp (type, "ttl") == 0) codec = &ttl_codec;
  220. // TODO other codecs here.
  221. else {
  222. PyErr_SetString (PyExc_ValueError, "Unsupported codec.");
  223. return NULL;
  224. }
  225. size_t ct;
  226. char *err;
  227. codec->decode_graph (fh, &res->ob_struct, &ct, &err);
  228. fclose (fh);
  229. LOG_DEBUG("Decoded %lu triples.", ct);
  230. if (UNLIKELY (err)) {
  231. PyErr_SetString (PyExc_IOError, err);
  232. return NULL;
  233. }
  234. Py_INCREF (res);
  235. return (PyObject *) res;
  236. }
  237. /** @brief Build a triple pattern for lookup purposes.
  238. */
  239. inline static int build_trp_pattern (PyObject *args, LSUP_Term *spo[])
  240. {
  241. PyObject *s_obj, *p_obj, *o_obj;
  242. if (! (PyArg_ParseTuple (args, "OOO", &s_obj, &p_obj, &o_obj)))
  243. return -1;
  244. if (s_obj != Py_None && !PyObject_TypeCheck (s_obj, &TermType)) {
  245. PyErr_SetString (PyExc_TypeError, "Subject must be a term or None.");
  246. return -1;
  247. }
  248. if (p_obj != Py_None && !PyObject_TypeCheck (p_obj, &TermType)) {
  249. PyErr_SetString (PyExc_TypeError, "Predicate must be a term or None.");
  250. return -1;
  251. }
  252. if (o_obj != Py_None && !PyObject_TypeCheck (o_obj, &TermType)) {
  253. PyErr_SetString (PyExc_TypeError, "Object must be a term or None.");
  254. return -1;
  255. }
  256. spo[0] = s_obj != Py_None ? ((TermObject *)s_obj)->ob_struct : NULL;
  257. spo[1] = p_obj != Py_None ? ((TermObject *)p_obj)->ob_struct : NULL;
  258. spo[2] = o_obj != Py_None ? ((TermObject *)o_obj)->ob_struct : NULL;
  259. return 0;
  260. }
  261. /** @brief Converter for a term struct in `O&` notation.
  262. *
  263. * See https://docs.python.org/3/c-api/arg.html#other-objects
  264. * Convert a PyObject argument into a LSUP_term. `Py_None` is acceptable.
  265. *
  266. * @param[in] obj Argument passed to calling function.
  267. *
  268. * @param[out] result Result of transform process.
  269. *
  270. * @return 1 on conversion success; 0 in failure.
  271. */
  272. static int arg_term_converter (PyObject *obj, void *result)
  273. {
  274. if (obj != Py_None && !PyObject_TypeCheck (obj, &TermType)) {
  275. PyErr_SetString (PyExc_TypeError, "Variable must be a Term or None.");
  276. return 0;
  277. }
  278. LSUP_Term **term = result;
  279. *term = obj != Py_None ? ((TermObject *)obj)->ob_struct : NULL;
  280. return 1;
  281. }
  282. /** @brief Converter for a graph struct in `O&` notation.
  283. *
  284. * See https://docs.python.org/3/c-api/arg.html#other-objects
  285. * Convert a PyObject argument into a LSUP_term.
  286. *
  287. * @param[in] obj Argument passed to calling function.
  288. *
  289. * @param[out] result Result of transform process.
  290. *
  291. * @return 1 on conversion success; 0 in failure.
  292. */
  293. static int arg_graph_converter (PyObject *obj, void *result);
  294. static PyObject *
  295. Graph_copy_contents (GraphObject *self, PyObject *args, PyObject *kwargs)
  296. {
  297. assert (!PyErr_Occurred());
  298. assert (args || kwargs);
  299. LSUP_Term *spo[3];
  300. LSUP_Graph *dest;
  301. static char *kwlist[] = {"", "s", "p", "o", NULL};
  302. if (!PyArg_ParseTupleAndKeywords(
  303. args, kwargs, "O&|O&O&O&", kwlist,
  304. arg_graph_converter, &dest,
  305. arg_term_converter, spo,
  306. arg_term_converter, spo + 1,
  307. arg_term_converter, spo + 2
  308. )) return NULL;
  309. if (LSUP_graph_copy_contents (
  310. self->ob_struct, dest,
  311. spo[0], spo[1], spo[2])
  312. < LSUP_OK)
  313. {
  314. PyErr_SetString (PyExc_ValueError, "Error copying graph contents.");
  315. return NULL;
  316. }
  317. Py_RETURN_NONE;
  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 (NULL, NULL, NULL);
  338. if (!dest) {
  339. PyErr_SetString (PyExc_Exception, "Could not create destination graph.");
  340. return NULL;
  341. }
  342. LSUP_rc rc = LSUP_graph_bool_op (
  343. op, ((GraphObject *) gr1)->ob_struct,
  344. ((GraphObject *) gr2)->ob_struct, res->ob_struct);
  345. if (rc < LSUP_OK) {
  346. PyErr_SetString (PyExc_Exception, "Error performing boolean operation.");
  347. return NULL;
  348. }
  349. Py_INCREF(res);
  350. return (PyObject *) res;
  351. }
  352. static PyObject *
  353. Graph_add (PyObject *self, PyObject *triples)
  354. {
  355. // Triple may be any iterable.
  356. PyObject *iter = PyObject_GetIter (triples);
  357. if (! iter) {
  358. PyErr_SetString (
  359. PyExc_ValueError, "Triples object cannot be iterated.");
  360. return NULL;
  361. }
  362. PyObject *trp_obj;
  363. int rc = 0;
  364. size_t ct = 0;
  365. LSUP_GraphIterator *it = LSUP_graph_add_init (
  366. ((GraphObject *)self)->ob_struct);
  367. while ((trp_obj = PyIter_Next (iter))) {
  368. if (!PyObject_TypeCheck (trp_obj, &TripleType)) {
  369. PyErr_SetString (
  370. PyExc_ValueError, "Object is not a triple.");
  371. rc = -1;
  372. goto finally;
  373. }
  374. LOG_TRACE("Inserting triple #%lu", ct);
  375. LSUP_rc db_rc = LSUP_graph_add_iter (
  376. it, ((TripleObject *) trp_obj)->ob_struct);
  377. if (db_rc == LSUP_OK) {
  378. rc = LSUP_OK;
  379. ct++;
  380. } else if (UNLIKELY (db_rc < 0)) {
  381. PyErr_SetString (PyExc_ValueError, "Error while adding triples.");
  382. rc = -1;
  383. goto finally;
  384. }
  385. // If db_rc > 0, it's a no-op and the counter is not increased.
  386. }
  387. finally:
  388. LSUP_graph_add_done (it);
  389. if (rc == LSUP_OK) return PyLong_FromSize_t (ct);
  390. return NULL;
  391. }
  392. static PyObject *Graph_remove (PyObject *self, PyObject *args)
  393. {
  394. LSUP_rc rc;
  395. LSUP_Term *spo[3];
  396. if (!PyArg_ParseTuple (
  397. args,
  398. "O&O&O&",
  399. arg_term_converter, spo,
  400. arg_term_converter, spo + 1,
  401. arg_term_converter, spo + 2
  402. )) Py_RETURN_NONE;
  403. size_t ct;
  404. rc = LSUP_graph_remove (
  405. ((GraphObject *)self)->ob_struct, spo[0], spo[1], spo[2], &ct);
  406. if (rc < 0) {
  407. // TODO implement strerror for more details.
  408. PyErr_SetString (PyExc_SystemError, "Error removing triples.");
  409. goto finally;
  410. }
  411. LOG_DEBUG("Removed %lu triples.", ct);
  412. finally:
  413. if (rc < 0) return NULL;
  414. Py_RETURN_NONE;
  415. }
  416. static PyObject *Graph_lookup (PyObject *self, PyObject *args)
  417. {
  418. LSUP_rc rc;
  419. GraphIteratorObject *it_obj = NULL;
  420. LSUP_Term *spo[3];
  421. if (!PyArg_ParseTuple (
  422. args,
  423. "O&O&O&",
  424. arg_term_converter, spo,
  425. arg_term_converter, spo + 1,
  426. arg_term_converter, spo + 2
  427. )) Py_RETURN_NONE;
  428. size_t ct;
  429. LSUP_GraphIterator *it = LSUP_graph_lookup (
  430. ((GraphObject *)self)->ob_struct, spo[0], spo[1], spo[2], &ct);
  431. if (UNLIKELY (!it)) {
  432. // TODO implement LSUP_strerror for more details.
  433. PyErr_SetString (PyExc_SystemError, "Error looking up triples.");
  434. rc = -1;
  435. goto finally;
  436. }
  437. LOG_DEBUG("Found %lu triples.", ct);
  438. // Initialize the generator object.
  439. it_obj = PyObject_New (
  440. GraphIteratorObject, &GraphIteratorType);
  441. if (UNLIKELY (!it_obj)) return PyErr_NoMemory();
  442. it_obj->it = it;
  443. it_obj->spo = TRP_DUMMY;
  444. Py_INCREF (it_obj);
  445. finally:
  446. return (PyObject *)it_obj;
  447. }
  448. static PyObject *
  449. Graph_encode (PyObject *self, PyObject *args)
  450. {
  451. const char *type;
  452. if (! PyArg_ParseTuple (args, "s", &type)) return NULL;
  453. // Initialize the generator object.
  454. StringIteratorObject *it_obj = PyObject_New (
  455. StringIteratorObject, &StringIteratorType);
  456. if (!it_obj) return NULL;
  457. if (strcmp (type, "nt") == 0) it_obj->codec = &nt_codec;
  458. else if (strcmp (type, "ttl") == 0) it_obj->codec = &ttl_codec;
  459. // TODO other codecs here.
  460. else {
  461. PyErr_SetString (PyExc_ValueError, "Unsupported codec.");
  462. return NULL;
  463. }
  464. it_obj->it = it_obj->codec->encode_graph_init (
  465. ((GraphObject *)self)->ob_struct);
  466. it_obj->line = NULL;
  467. Py_INCREF (it_obj);
  468. return (PyObject *)it_obj;
  469. }
  470. static PyMethodDef Graph_methods[] = {
  471. {
  472. "copy", (PyCFunction) Graph_copy_contents,
  473. METH_VARARGS | METH_KEYWORDS,
  474. "Copy the contents of a graph into another."
  475. },
  476. {
  477. "from_rdf", (PyCFunction) Graph_new_from_rdf,
  478. METH_CLASS | METH_VARARGS,
  479. "Create a graph from a RDF file."
  480. },
  481. {"add", (PyCFunction) Graph_add, METH_O, "Add triples to a graph."},
  482. {
  483. "remove", (PyCFunction) Graph_remove, METH_VARARGS,
  484. "Remove triples from a graph by matching a pattern."
  485. },
  486. {
  487. "lookup", (PyCFunction) Graph_lookup, METH_VARARGS,
  488. "Look triples in a graph by matching a pattern."
  489. },
  490. {
  491. "to_rdf", (PyCFunction) Graph_encode, METH_VARARGS,
  492. "Encode a graph into a RDF byte buffer."
  493. },
  494. {NULL},
  495. };
  496. static inline PyObject *Graph_bool_and (
  497. PyTypeObject *cls, PyObject *gr1, PyObject *gr2)
  498. { return Graph_bool_op (cls, LSUP_BOOL_INTERSECTION, gr1, gr2); }
  499. static inline PyObject *Graph_bool_or (
  500. PyTypeObject *cls, PyObject *gr1, PyObject *gr2)
  501. { return Graph_bool_op (cls, LSUP_BOOL_UNION, gr1, gr2); }
  502. static inline PyObject *Graph_bool_subtract (
  503. PyTypeObject *cls, PyObject *gr1, PyObject *gr2)
  504. { return Graph_bool_op (cls, LSUP_BOOL_SUBTRACTION, gr1, gr2); }
  505. static inline PyObject *Graph_bool_xor (
  506. PyTypeObject *cls, PyObject *gr1, PyObject *gr2)
  507. { return Graph_bool_op (cls, LSUP_BOOL_XOR, gr1, gr2); }
  508. static PyNumberMethods Graph_number_methods = {
  509. .nb_and = (binaryfunc) Graph_bool_and,
  510. .nb_or = (binaryfunc) Graph_bool_or,
  511. .nb_subtract = (binaryfunc) Graph_bool_subtract,
  512. .nb_xor = (binaryfunc) Graph_bool_xor,
  513. };
  514. static int
  515. Graph_contains (PyObject *self, PyObject *value)
  516. {
  517. if (!PyObject_TypeCheck (value, &TripleType)) {
  518. PyErr_SetString (PyExc_ValueError, "Error parsing input value.");
  519. return -1;
  520. }
  521. int rc = LSUP_graph_contains (
  522. ((GraphObject *) self)->ob_struct,
  523. ((TripleObject *) value)->ob_struct);
  524. return rc;
  525. }
  526. static Py_ssize_t
  527. Graph_get_size (PyObject *self)
  528. { return LSUP_graph_size (((GraphObject *) self)->ob_struct); }
  529. static PySequenceMethods Graph_seq_methods = {
  530. .sq_length = (lenfunc) Graph_get_size,
  531. .sq_contains = (objobjproc) Graph_contains,
  532. };
  533. PyTypeObject GraphType = {
  534. PyVarObject_HEAD_INIT(NULL, 0)
  535. .tp_name = "graph.Graph",
  536. .tp_doc = "RDF graph",
  537. .tp_basicsize = sizeof (GraphObject),
  538. .tp_itemsize = 0,
  539. .tp_flags = Py_TPFLAGS_DEFAULT,
  540. .tp_new = PyType_GenericNew,
  541. .tp_init = (initproc) Graph_init,
  542. .tp_dealloc = (destructor) Graph_dealloc,
  543. .tp_getset = Graph_getsetters,
  544. .tp_methods = Graph_methods,
  545. .tp_richcompare = (richcmpfunc) Graph_richcmp,
  546. .tp_as_number = &Graph_number_methods,
  547. .tp_as_sequence = &Graph_seq_methods,
  548. };
  549. static int arg_graph_converter (PyObject *obj, void *result)
  550. {
  551. if (!PyObject_TypeCheck (obj, &GraphType)) {
  552. PyErr_SetString (PyExc_TypeError, "Variable must be a Graph object.");
  553. return 0;
  554. }
  555. LSUP_Graph **gr = result;
  556. *gr = ((GraphObject *)obj)->ob_struct;
  557. return 1;
  558. }
  559. #endif