Skip to content

Uncaught IndexError when parsing turtle #3404

@stigbd

Description

@stigbd

I stumbled upon this when playing around with parsing text to turtle. I did not expect rdflib to throw an IndexError.

Given the following strings:

  1. '@prefix ex: <http://example.org/>. ex:Cat ex:hasName "Kitty" .'
  2. '@prefix ex: <http://example.org/>. ex:Cat ex:hasName "Kitty" '
  3. '@prefix ex: <http://example.org/>. ex:Cat ex:hasName "Kitty"'

Expected behavior:

  • parsing (1) does not throw an exception.
  • parsing (2) throws rdflib BadSyntax exception.
  • parsing (3) throws rdflib BadSyntax exception.

Actual behavior:

  • parsing (1) does not throw an exception. ✔️
  • parsing (2) throws rdflib BadSyntax exception. ✔️
  • parsing (3) throws IndexError. ❌

This is how it was done:

>>> from rdflib import Graph
>>> data = '@prefix ex: <http://example.org/>. ex:Cat ex:hasName "Kitty" .'
>>> graph = Graph().parse(data=data, format="turtle")
>>> data = '@prefix ex: <http://example.org/>. ex:Cat ex:hasName "Kitty" '
>>> graph = Graph().parse(data=data, format="turtle")
Traceback (most recent call last):
  File "<python-input-81>", line 1, in <module>
    graph = Graph().parse(data=data, format="turtle")
  File "/home/stigbd/src/stigbd/hello-sparql/api/.venv/lib/python3.14/site-packages/rdflib/graph.py", line 1669, in parse
    raise se
  File "/home/stigbd/src/stigbd/hello-sparql/api/.venv/lib/python3.14/site-packages/rdflib/graph.py", line 1660, in parse
    parser.parse(source, self, **args)
    ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^
  File "/home/stigbd/src/stigbd/hello-sparql/api/.venv/lib/python3.14/site-packages/rdflib/plugins/parsers/notation3.py", line 2034, in parse
    p.loadStream(stream)
    ~~~~~~~~~~~~^^^^^^^^
  File "/home/stigbd/src/stigbd/hello-sparql/api/.venv/lib/python3.14/site-packages/rdflib/plugins/parsers/notation3.py", line 492, in loadStream
    return self.loadBuf(stream.read())  # Not ideal
           ~~~~~~~~~~~~^^^^^^^^^^^^^^^
  File "/home/stigbd/src/stigbd/hello-sparql/api/.venv/lib/python3.14/site-packages/rdflib/plugins/parsers/notation3.py", line 498, in loadBuf
    self.feed(buf)
    ~~~~~~~~~^^^^^
  File "/home/stigbd/src/stigbd/hello-sparql/api/.venv/lib/python3.14/site-packages/rdflib/plugins/parsers/notation3.py", line 524, in feed
    i = self.directiveOrStatement(s, j)
  File "/home/stigbd/src/stigbd/hello-sparql/api/.venv/lib/python3.14/site-packages/rdflib/plugins/parsers/notation3.py", line 543, in directiveOrStatement
    j = self.statement(argstr, i)
  File "/home/stigbd/src/stigbd/hello-sparql/api/.venv/lib/python3.14/site-packages/rdflib/plugins/parsers/notation3.py", line 791, in statement
    j = self.property_list(argstr, i, r[0])
  File "/home/stigbd/src/stigbd/hello-sparql/api/.venv/lib/python3.14/site-packages/rdflib/plugins/parsers/notation3.py", line 1153, in property_list
    i = self.objectList(argstr, j, objs)
  File "/home/stigbd/src/stigbd/hello-sparql/api/.venv/lib/python3.14/site-packages/rdflib/plugins/parsers/notation3.py", line 1209, in objectList
    self.BadSyntax(argstr, j, "EOF found after object")
    ~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/stigbd/src/stigbd/hello-sparql/api/.venv/lib/python3.14/site-packages/rdflib/plugins/parsers/notation3.py", line 1743, in BadSyntax
    raise BadSyntax(self._thisDoc, self.lines, argstr, i, msg)
rdflib.plugins.parsers.notation3.BadSyntax: <no detail available>
>>> data = '@prefix ex: <http://example.org/>. ex:Cat ex:hasName "Kitty"'
>>> graph = Graph().parse(data=data, format="turtle")
Traceback (most recent call last):
  File "<python-input-83>", line 1, in <module>
    graph = Graph().parse(data=data, format="turtle")
  File "/home/stigbd/src/stigbd/hello-sparql/api/.venv/lib/python3.14/site-packages/rdflib/graph.py", line 1660, in parse
    parser.parse(source, self, **args)
    ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^
  File "/home/stigbd/src/stigbd/hello-sparql/api/.venv/lib/python3.14/site-packages/rdflib/plugins/parsers/notation3.py", line 2034, in parse
    p.loadStream(stream)
    ~~~~~~~~~~~~^^^^^^^^
  File "/home/stigbd/src/stigbd/hello-sparql/api/.venv/lib/python3.14/site-packages/rdflib/plugins/parsers/notation3.py", line 492, in loadStream
    return self.loadBuf(stream.read())  # Not ideal
           ~~~~~~~~~~~~^^^^^^^^^^^^^^^
  File "/home/stigbd/src/stigbd/hello-sparql/api/.venv/lib/python3.14/site-packages/rdflib/plugins/parsers/notation3.py", line 498, in loadBuf
    self.feed(buf)
    ~~~~~~~~~^^^^^
  File "/home/stigbd/src/stigbd/hello-sparql/api/.venv/lib/python3.14/site-packages/rdflib/plugins/parsers/notation3.py", line 524, in feed
    i = self.directiveOrStatement(s, j)
  File "/home/stigbd/src/stigbd/hello-sparql/api/.venv/lib/python3.14/site-packages/rdflib/plugins/parsers/notation3.py", line 543, in directiveOrStatement
    j = self.statement(argstr, i)
  File "/home/stigbd/src/stigbd/hello-sparql/api/.venv/lib/python3.14/site-packages/rdflib/plugins/parsers/notation3.py", line 791, in statement
    j = self.property_list(argstr, i, r[0])
  File "/home/stigbd/src/stigbd/hello-sparql/api/.venv/lib/python3.14/site-packages/rdflib/plugins/parsers/notation3.py", line 1153, in property_list
    i = self.objectList(argstr, j, objs)
  File "/home/stigbd/src/stigbd/hello-sparql/api/.venv/lib/python3.14/site-packages/rdflib/plugins/parsers/notation3.py", line 1203, in objectList
    i = self.object(argstr, i, res)
  File "/home/stigbd/src/stigbd/hello-sparql/api/.venv/lib/python3.14/site-packages/rdflib/plugins/parsers/notation3.py", line 1500, in object
    j = self.subject(argstr, i, res)
  File "/home/stigbd/src/stigbd/hello-sparql/api/.venv/lib/python3.14/site-packages/rdflib/plugins/parsers/notation3.py", line 798, in subject
    return self.item(argstr, i, res)
           ~~~~~~~~~^^^^^^^^^^^^^^^^
  File "/home/stigbd/src/stigbd/hello-sparql/api/.venv/lib/python3.14/site-packages/rdflib/plugins/parsers/notation3.py", line 890, in item
    return self.path(argstr, i, res)
           ~~~~~~~~~^^^^^^^^^^^^^^^^
  File "/home/stigbd/src/stigbd/hello-sparql/api/.venv/lib/python3.14/site-packages/rdflib/plugins/parsers/notation3.py", line 897, in path
    j = self.nodeOrLiteral(argstr, i, res)
  File "/home/stigbd/src/stigbd/hello-sparql/api/.venv/lib/python3.14/site-packages/rdflib/plugins/parsers/notation3.py", line 1573, in nodeOrLiteral
    if argstr[j] == "@":  # Language?
       ~~~~~~^^^
IndexError: string index out of range

python version: 3.14.3
rdflib version: 7.6.0.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions