RDFLib/rdflibissueplayground — Issue #11: sparql query result serializer possibly broken in Python 2.4
Author: gjhiggins
Created: 2012-02-20 05:28:27+00:00
State: open
Labels: open

rubinsztajn, 2010-06-28T17:34:41.000Z
Using rdflib 3.0.0 on Python 2.4

If I query a graph like so:

g = ConjunctiveGraph('rdfxmlfile.rdf')
r = g.query('PREFIX foaf: SELECT * WHERE { ?s foaf:name ?o }')

len(g) #496

print r.serialize()

I get the following error:

Traceback (most recent call last):
File "sparql.py", line 13, in ?
print r.serialize()
File "/home/rubinsta/lib/python/rdfextras/sparql/query.py", line 1529, in serialize
writer.write_header(allvarsL)
File "/home/rubinsta/lib/python/rdfextras/sparql/query.py", line 1280, in write_header
AttributesNSImpl(attr_vals, attr_qnames))
File "/usr/lib/python2.4/xml/sax/saxutils.py", line 139, in startElementNS
name = self._current_context[name[0]] + ":" + name[1]
KeyError

This works fine on Python 2.6

I realize that a fix to support Python 2.4 is unlikely but I would love confirmation that this is a bug that crops up only in pre 2.6 versions of Python.

Thanks!

Comment 1 by gromgull
Much time has passed - but we've changed this code - perhaps you can check if it works in 2.4 now?

I think we gave up 2.4 support and said we require at least 2.5 a while back - but if it's easy to fix we can do it!

Comment 2 by gjhigg...@gmail.com
ForTheRecord:

Re-enabling support for Python2.4 requires only a small amount of backward-compatibility change to the RDFLib 3 codebase (3.1 or 3.2dev) in order to address the following 2.4 issues:

File "/tmp/rdflib-3.1.0/rdflib/plugins/serializers/nt.py", line 47
chars.append('\u%04X' % ord(char) if ord(char) <= 0xFFFF else '\U%08X' % ord(char))
^
SyntaxError: invalid syntax

This can be trivially re-expressed as a loop

File "/tmp/rdflib-3.1.0/rdflib/compare.py", line 76, in ?
import hashlib
ImportError: No module named hashlib

This is easily fixable via testing for ImportError.

File "/tmp/rdflib-3.1.0/test/test_conventions.py", line 20, in module_names
for importer, name, ispkg in pkgutil.iter_modules([path]):
AttributeError: 'module' object has no attribute 'iter_modules'

This test enforces the convention of lowercase module names in the RDFLib codebase and its failure can be ignored or adjusted to raise a SkipTest exception.

With these issues addressed, all RDFLib 3.2dev tests pass under Python2.4

FTR Caveat:

SPARQL support has been migrated to RDFExtras. Whilst the RDFLib 3.1/2dev core library does support Python 2.4, RDFExtras does not.

Is it worth applying these few backwards-compatible changes in order to support people still using Python 2.4?