ConjunctiveStore.add() accepts values that make serialize() fail badly · Issue #200 · RDFLib/rdflib (original) (raw)

from rdflib.graph import Graph, ConjunctiveGraph
from rdflib import URIRef, Namespace

ns = Namespace('http://example.org/')
store = ConjunctiveGraph(identifier='temp')
store.add((ns['abc'], ns['attribute'], 'qwer'))
store.serialize(destination='test.n3', format='n3')
Traceback (most recent call last):
  File "test.py", line 7, in <module>
    store.serialize(destination='test.n3', format='n3')
  File "/usr/local/lib/python2.7/dist-packages/rdflib/graph.py", line 684, in serialize
    serializer.serialize(stream, base=base, encoding=encoding, **args)
  File "/usr/local/lib/python2.7/dist-packages/rdflib/plugins/serializers/turtle.py", line 180, in serialize
    if self.statement(subject) and not firstTime:
  File "/usr/local/lib/python2.7/dist-packages/rdflib/plugins/serializers/n3.py", line 91, in statement
    or super(N3Serializer, self).statement(subject))
  File "/usr/local/lib/python2.7/dist-packages/rdflib/plugins/serializers/turtle.py", line 239, in statement
    return self.s_squared(subject) or self.s_default(subject)
  File "/usr/local/lib/python2.7/dist-packages/rdflib/plugins/serializers/turtle.py", line 244, in s_default
    self.predicateList(subject)
  File "/usr/local/lib/python2.7/dist-packages/rdflib/plugins/serializers/turtle.py", line 342, in predicateList
    self.objectList(properties[propList[0]])
  File "/usr/local/lib/python2.7/dist-packages/rdflib/plugins/serializers/turtle.py", line 357, in objectList
    self.path(objects[0], OBJECT)
  File "/usr/local/lib/python2.7/dist-packages/rdflib/plugins/serializers/n3.py", line 95, in path
    super(N3Serializer, self).path(node, position, newline)
  File "/usr/local/lib/python2.7/dist-packages/rdflib/plugins/serializers/turtle.py", line 260, in path
    or self.p_default(node, position, newline)):
  File "/usr/local/lib/python2.7/dist-packages/rdflib/plugins/serializers/turtle.py", line 266, in p_default
    self.write(self.label(node, position))
  File "/usr/local/lib/python2.7/dist-packages/rdflib/plugins/serializers/turtle.py", line 279, in label
    return self.getQName(node, position==VERB) or node.n3()
AttributeError: 'str' object has no attribute 'n3'

The problem is that the object of the added triple should be a rdflib.term.Literal instead of a str. But the error doesn't occur until serialize() is called. As a result it isn't really clear what or where the problem is. ConjunctiveStore.add() should check that tuple it is given for valid values.