|
@@ -7,7 +7,7 @@
|
|
#include <structmember.h>
|
|
#include <structmember.h>
|
|
|
|
|
|
#include "graph.h"
|
|
#include "graph.h"
|
|
-#include "triple_obj.h"
|
|
|
|
|
|
+#include "py_triple.h"
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
typedef struct {
|
|
@@ -41,65 +41,39 @@ Graph_dealloc (GraphObject *self)
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
-static PyObject *
|
|
|
|
-Graph_get_size (GraphObject *self, void *closure)
|
|
|
|
-{
|
|
|
|
- size_t gr_size = LSUP_graph_size (self->ob_struct);
|
|
|
|
- PyObject *size = PyLong_FromSize_t (gr_size);
|
|
|
|
-
|
|
|
|
- Py_INCREF (size);
|
|
|
|
- return size;
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-/*
|
|
|
|
- * FIXME This should be deleted and the setter set to NULL or omitted, but
|
|
|
|
- * in doing so Python 3.8 raises a SystemError: bad call flags. See
|
|
|
|
- * https://bugs.python.org/issue39884 No resolution is clear yet.
|
|
|
|
- */
|
|
|
|
-static int
|
|
|
|
-Graph_set_size (GraphObject *self, PyObject value, void *closure)
|
|
|
|
-{ return -1; }
|
|
|
|
-
|
|
|
|
-
|
|
|
|
static PyObject *
|
|
static PyObject *
|
|
Graph_get_uri (GraphObject *self, void *closure)
|
|
Graph_get_uri (GraphObject *self, void *closure)
|
|
{
|
|
{
|
|
PyObject *uri = PyUnicode_FromString (
|
|
PyObject *uri = PyUnicode_FromString (
|
|
LSUP_graph_uri (self->ob_struct)->data);
|
|
LSUP_graph_uri (self->ob_struct)->data);
|
|
|
|
|
|
- Py_INCREF (uri);
|
|
|
|
|
|
+ Py_INCREF(uri);
|
|
return uri;
|
|
return uri;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
static int
|
|
static int
|
|
-Graph_set_uri (GraphObject *self, PyObject *uri, void *closure)
|
|
|
|
|
|
+Graph_set_uri (GraphObject *self, PyObject *value, void *closure)
|
|
{
|
|
{
|
|
- if (PyUnicode_READY (uri) != 0) return -1; // TODO redundant?
|
|
|
|
- if (PyUnicode_KIND (uri) != PyUnicode_1BYTE_KIND) return -1;
|
|
|
|
|
|
+ if (PyUnicode_READY(value) != 0) return -1;
|
|
|
|
+ if (PyUnicode_KIND(value) != PyUnicode_1BYTE_KIND) return -1;
|
|
|
|
|
|
- LSUP_rc rc = LSUP_graph_set_uri (self->ob_struct, PyUnicode_DATA (uri));
|
|
|
|
|
|
+ LSUP_rc rc = LSUP_graph_set_uri (self->ob_struct, PyUnicode_DATA(value));
|
|
|
|
|
|
return rc == LSUP_OK ? 0 : -1;
|
|
return rc == LSUP_OK ? 0 : -1;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
static PyGetSetDef Graph_getsetters[] = {
|
|
static PyGetSetDef Graph_getsetters[] = {
|
|
- {
|
|
|
|
- .name = "__len__",
|
|
|
|
- .get = (getter) Graph_get_size,
|
|
|
|
- .set = (setter) Graph_set_size,
|
|
|
|
- .doc = "Number of triples in the graph.",
|
|
|
|
- //.closure = NULL,
|
|
|
|
- },
|
|
|
|
|
|
+ /* FIXME This is throwing strange errors.
|
|
{
|
|
{
|
|
"uri", (getter) Graph_get_uri, (setter) Graph_set_uri,
|
|
"uri", (getter) Graph_get_uri, (setter) Graph_set_uri,
|
|
"Graph URI.", NULL
|
|
"Graph URI.", NULL
|
|
},
|
|
},
|
|
|
|
+ */
|
|
{NULL}
|
|
{NULL}
|
|
};
|
|
};
|
|
|
|
|
|
-
|
|
|
|
static PyObject *
|
|
static PyObject *
|
|
Graph_copy (PyTypeObject *cls, PyObject *src)
|
|
Graph_copy (PyTypeObject *cls, PyObject *src)
|
|
{
|
|
{
|
|
@@ -115,12 +89,13 @@ Graph_copy (PyTypeObject *cls, PyObject *src)
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
-/* TODO implement LSUP_graph_equals
|
|
|
|
static PyObject *
|
|
static PyObject *
|
|
Graph_richcmp (PyObject *gr1, PyObject *gr2, int op)
|
|
Graph_richcmp (PyObject *gr1, PyObject *gr2, int op)
|
|
{
|
|
{
|
|
if (op != Py_EQ && op != Py_NE) Py_RETURN_NOTIMPLEMENTED;
|
|
if (op != Py_EQ && op != Py_NE) Py_RETURN_NOTIMPLEMENTED;
|
|
|
|
|
|
|
|
+ Py_RETURN_TRUE; // TODO use graph xor == 0
|
|
|
|
+ /*
|
|
PyObject *res = NULL;
|
|
PyObject *res = NULL;
|
|
|
|
|
|
LSUP_Graph *t1 = ((GraphObject *) gr1)->ob_struct;
|
|
LSUP_Graph *t1 = ((GraphObject *) gr1)->ob_struct;
|
|
@@ -128,15 +103,16 @@ Graph_richcmp (PyObject *gr1, PyObject *gr2, int op)
|
|
|
|
|
|
if (LSUP_graph_equals (t1, t2) && op == Py_EQ) Py_RETURN_TRUE;
|
|
if (LSUP_graph_equals (t1, t2) && op == Py_EQ) Py_RETURN_TRUE;
|
|
Py_RETURN_FALSE;
|
|
Py_RETURN_FALSE;
|
|
|
|
+ */
|
|
}
|
|
}
|
|
-*/
|
|
|
|
|
|
|
|
|
|
|
|
-/* TODO Implement mdbstore bool ops
|
|
|
|
static inline PyObject *
|
|
static inline PyObject *
|
|
Graph_bool_op (
|
|
Graph_bool_op (
|
|
PyTypeObject *cls, LSUP_bool_op op, PyObject *gr1, PyObject *gr2)
|
|
PyTypeObject *cls, LSUP_bool_op op, PyObject *gr1, PyObject *gr2)
|
|
{
|
|
{
|
|
|
|
+ Py_RETURN_NONE; //TODO Implement mdbstore bool ops
|
|
|
|
+ /*
|
|
if (! PyObject_TypeCheck (gr1, cls) || ! PyObject_TypeCheck (gr2, cls))
|
|
if (! PyObject_TypeCheck (gr1, cls) || ! PyObject_TypeCheck (gr2, cls))
|
|
return NULL;
|
|
return NULL;
|
|
|
|
|
|
@@ -149,14 +125,20 @@ Graph_bool_op (
|
|
|
|
|
|
Py_INCREF(res);
|
|
Py_INCREF(res);
|
|
return (PyObject *) res;
|
|
return (PyObject *) res;
|
|
|
|
+ */
|
|
}
|
|
}
|
|
-*/
|
|
|
|
|
|
|
|
|
|
|
|
-static int Graph_add (PyObject *self, PyObject *triples)
|
|
|
|
|
|
+static PyObject *
|
|
|
|
+Graph_add (PyObject *self, PyObject *triples)
|
|
{
|
|
{
|
|
// Triple may be any iterable.
|
|
// Triple may be any iterable.
|
|
- if (! PyIter_Check (triples)) return -1;
|
|
|
|
|
|
+ PyObject *iter = PyObject_GetIter (triples);
|
|
|
|
+ if (! iter) {
|
|
|
|
+ PyErr_SetString (
|
|
|
|
+ PyExc_ValueError, "Triples object cannot be iterated.");
|
|
|
|
+ return NULL;
|
|
|
|
+ }
|
|
|
|
|
|
PyObject *trp_obj = NULL;
|
|
PyObject *trp_obj = NULL;
|
|
int rc = 0;
|
|
int rc = 0;
|
|
@@ -165,8 +147,10 @@ static int Graph_add (PyObject *self, PyObject *triples)
|
|
LSUP_GraphIterator *it = LSUP_graph_add_stream_init (
|
|
LSUP_GraphIterator *it = LSUP_graph_add_stream_init (
|
|
((GraphObject *)self)->ob_struct);
|
|
((GraphObject *)self)->ob_struct);
|
|
|
|
|
|
- for (i = 0; (trp_obj = PyIter_Next (triples)); i++) {
|
|
|
|
|
|
+ for (i = 0; (trp_obj = PyIter_Next (iter)); i++) {
|
|
if (!PyObject_TypeCheck (trp_obj, &TripleType)) {
|
|
if (!PyObject_TypeCheck (trp_obj, &TripleType)) {
|
|
|
|
+ PyErr_SetString (
|
|
|
|
+ PyExc_ValueError, "Object is not a triple.");
|
|
rc = -1;
|
|
rc = -1;
|
|
goto finalize;
|
|
goto finalize;
|
|
}
|
|
}
|
|
@@ -182,13 +166,18 @@ static int Graph_add (PyObject *self, PyObject *triples)
|
|
goto finalize;
|
|
goto finalize;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- LSUP_striple_free (sspo);
|
|
|
|
|
|
|
|
finalize:
|
|
finalize:
|
|
LSUP_graph_add_stream_done (it);
|
|
LSUP_graph_add_stream_done (it);
|
|
LSUP_striple_free (sspo);
|
|
LSUP_striple_free (sspo);
|
|
|
|
|
|
- return rc;
|
|
|
|
|
|
+ PyObject *ret = PyLong_FromSize_t (LSUP_graph_iter_cur (it));
|
|
|
|
+
|
|
|
|
+ if (rc == 0) {
|
|
|
|
+ Py_INCREF (ret);
|
|
|
|
+ return ret;
|
|
|
|
+ }
|
|
|
|
+ return NULL;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -205,6 +194,7 @@ static PyObject *Graph_lookup (
|
|
|
|
|
|
|
|
|
|
static PyMethodDef Graph_methods[] = {
|
|
static PyMethodDef Graph_methods[] = {
|
|
|
|
+ {"uri", (PyCFunction) Graph_get_uri, METH_NOARGS, "Get the graph URI."},
|
|
{"copy", (PyCFunction) Graph_copy, METH_CLASS, "Copy a graph."},
|
|
{"copy", (PyCFunction) Graph_copy, METH_CLASS, "Copy a graph."},
|
|
{"add", (PyCFunction) Graph_add, METH_O, "Add triples to a graph."},
|
|
{"add", (PyCFunction) Graph_add, METH_O, "Add triples to a graph."},
|
|
{
|
|
{
|
|
@@ -218,7 +208,6 @@ static PyMethodDef Graph_methods[] = {
|
|
};
|
|
};
|
|
|
|
|
|
|
|
|
|
-/* TODO Implement mdbstore bool ops
|
|
|
|
static inline PyObject *Graph_bool_and (
|
|
static inline PyObject *Graph_bool_and (
|
|
PyTypeObject *cls, PyObject *gr1, PyObject *gr2)
|
|
PyTypeObject *cls, PyObject *gr1, PyObject *gr2)
|
|
{ return Graph_bool_op (cls, LSUP_BOOL_INTERSECTION, gr1, gr2); }
|
|
{ return Graph_bool_op (cls, LSUP_BOOL_INTERSECTION, gr1, gr2); }
|
|
@@ -241,22 +230,34 @@ static PyNumberMethods Graph_number_methods = {
|
|
.nb_subtract = (binaryfunc) Graph_bool_subtract,
|
|
.nb_subtract = (binaryfunc) Graph_bool_subtract,
|
|
.nb_xor = (binaryfunc) Graph_bool_xor,
|
|
.nb_xor = (binaryfunc) Graph_bool_xor,
|
|
};
|
|
};
|
|
-*/
|
|
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static Py_ssize_t
|
|
|
|
+Graph_get_size (PyObject *self)
|
|
|
|
+{ return LSUP_graph_size (((GraphObject *) self)->ob_struct); }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+static PySequenceMethods Graph_seq_methods = {
|
|
|
|
+ .sq_length = (lenfunc) Graph_get_size,
|
|
|
|
+ //.sq_contains = Graph_contains, // TODO
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
|
|
PyTypeObject GraphType = {
|
|
PyTypeObject GraphType = {
|
|
PyVarObject_HEAD_INIT(NULL, 0)
|
|
PyVarObject_HEAD_INIT(NULL, 0)
|
|
.tp_name = "graph.Graph",
|
|
.tp_name = "graph.Graph",
|
|
.tp_doc = "RDF graph",
|
|
.tp_doc = "RDF graph",
|
|
- .tp_basicsize = sizeof(GraphObject),
|
|
|
|
|
|
+ .tp_basicsize = sizeof (GraphObject),
|
|
.tp_itemsize = 0,
|
|
.tp_itemsize = 0,
|
|
.tp_flags = Py_TPFLAGS_DEFAULT,
|
|
.tp_flags = Py_TPFLAGS_DEFAULT,
|
|
.tp_new = PyType_GenericNew,
|
|
.tp_new = PyType_GenericNew,
|
|
.tp_init = (initproc) Graph_init,
|
|
.tp_init = (initproc) Graph_init,
|
|
.tp_dealloc = (destructor) Graph_dealloc,
|
|
.tp_dealloc = (destructor) Graph_dealloc,
|
|
- .tp_methods = Graph_methods,
|
|
|
|
.tp_getset = Graph_getsetters,
|
|
.tp_getset = Graph_getsetters,
|
|
- //.tp_richcompare = Graph_richcmp, TODO implement LSUP_graph_equals
|
|
|
|
- //.tp_as_number = &Graph_number_methods,
|
|
|
|
|
|
+ .tp_methods = Graph_methods,
|
|
|
|
+ .tp_richcompare = Graph_richcmp,
|
|
|
|
+ .tp_as_number = &Graph_number_methods,
|
|
|
|
+ .tp_as_sequence = &Graph_seq_methods,
|
|
};
|
|
};
|
|
|
|
|
|
#endif
|
|
#endif
|