Browse Source

Assign correct term sub-types.

Stefano Cossu 1 year ago
parent
commit
f76189d122
2 changed files with 36 additions and 20 deletions
  1. 29 16
      cpython/py_term.h
  2. 7 4
      cpython/py_triple.h

+ 29 - 16
cpython/py_term.h

@@ -391,24 +391,37 @@ PyObject *
 build_term (LSUP_Term *t)
 {
     log_trace ("Building term: %s", t->data);
-    char *datatype = (
-            t->type == LSUP_TERM_LITERAL && t->datatype ?
-            t->datatype->data : NULL);
-    char *lang = (
-            t->type == LSUP_TERM_LT_LITERAL && strlen (t->lang) ?
-            t->lang : NULL);
-    PyObject *term_args = Py_BuildValue (
-            "bszz", t->type, t->data, datatype, lang);
-    if (UNLIKELY (!term_args)) {
-        PyErr_SetString (PyExc_ValueError, "Invalid object args.");
-        return NULL;
+    PyTypeObject *term_obj_type;
+    PyObject *term_args;
+    switch (t->type) {
+        case LSUP_TERM_IRIREF:
+            term_obj_type = &IRIRefType;
+            term_args = Py_BuildValue ("(s)", t->data);
+            break;
+        case LSUP_TERM_NS_IRIREF:
+            term_obj_type = &IRIRefType;
+            term_args = Py_BuildValue ("sO", t->data, LSUP_iriref_nsm (t));
+            break;
+        case LSUP_TERM_BNODE:
+            term_obj_type = &BNodeType;
+            term_args = Py_BuildValue ("(s)", t->data);
+            break;
+        case LSUP_TERM_LITERAL:
+            term_obj_type = &LiteralType;
+            term_args = Py_BuildValue ("sz", t->data, t->datatype->data, NULL);
+            break;
+        case LSUP_TERM_LT_LITERAL:
+            term_obj_type = &LiteralType;
+            term_args = Py_BuildValue ("sz", t->data, NULL, t->lang);
+            break;
+        default:
+            return NULL;
     }
-    PyObject *t_obj = PyObject_CallObject ((PyObject *)&TermType, term_args);
+    if (UNLIKELY (!term_args)) return NULL;
+    PyObject *t_obj = PyObject_CallObject (
+            (PyObject *)term_obj_type, term_args);
     Py_DECREF (term_args);
-    if (UNLIKELY (!t_obj)) {
-        PyErr_SetString (PyExc_ValueError, "Invalid object.");
-        return NULL;
-    }
+    if (UNLIKELY (!t_obj)) return NULL;
 
     return t_obj;
 }

+ 7 - 4
cpython/py_triple.h

@@ -120,10 +120,13 @@ PyTypeObject TripleType = {
 PyObject *
 build_triple (LSUP_Triple *spo)
 {
-    log_info ("Building triple.");
-    PyObject *trp_args = Py_BuildValue (
-            "OOO",
-            build_term (spo->s), build_term (spo->p), build_term (spo->o));
+    PyObject
+        *s_obj = build_term (spo->s),
+        *p_obj = build_term (spo->p),
+        *o_obj = build_term (spo->o);
+
+    log_trace ("Building triple.");
+    PyObject *trp_args = Py_BuildValue ("OOO", s_obj, p_obj, o_obj);
     if (UNLIKELY (!trp_args)) {
         PyErr_SetString (PyExc_SystemError, "Error building triple args.");
         return NULL;