Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ConjunctiveStore.add() accepts values that make serialize() fail badly #200

Closed
dmm opened this issue Apr 11, 2012 · 2 comments
Closed

ConjunctiveStore.add() accepts values that make serialize() fail badly #200

dmm opened this issue Apr 11, 2012 · 2 comments

Comments

@dmm
Copy link

dmm commented Apr 11, 2012

For example:

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')

Fails with:

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.

@gromgull
Copy link
Member

The question is only really where to raise the exception. I guess an isinstance(Node) check in graph.add is the best place.

We have a related issue with graph identifiers for the conjuctive graph for some stores, URIs given as strings are generally turned into URIRefs, but not always.

Edit: which is #167

@dmm
Copy link
Author

dmm commented May 1, 2012

That's a good point. You don't want to have lots of paranoid code constantly checking types. My first intuition would be to deem some subset as the external API and check types there or at least clearly document what is and is not acceptable for parameters.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants