RDFLib/rdflib-sparql — pull request #7 by uholzer (2013-01-05 16:33:35+00:00)
Title: py3compat: Fix regex for string literals

ur"\u0022" is converted to r"\u0022" by 2to3. When using u, the \u0022
is interpreted as unicode character. When only r is used, the \ is
taken literally. The STRING_LITERAL1 and STRING_LITERAL2 use \uxxxx and
therefore don't work as expected in Python 3.

For example, the regex sometimes read string literals beyond their end:

```
Python 3.2.3 (default, Jun 25 2012, 23:10:56)
[GCC 4.7.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import rdflib
>>> g = rdflib.Graph()
>>> res = g.query("SELECT (CONCAT('a','b') AS ?c) {}")
>>> for r in res: print(r)
...
(rdflib.term.Literal("a','b"),)
>>> res = g.query("SELECT (REGEX('foo','foo') AS ?c) { }")
pyparsing.ParseException: Expected "," (at char 25), (line:1, col:26)
```

The same works in Python 2:

```
Python 2.7.3rc2 (default, Apr 22 2012, 22:30:17)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import rdflib
>>> g = rdflib.Graph()
>>> res = g.query("SELECT (CONCAT('a','b') AS ?c) {}")
>>> for r in res: print(r)
...
(rdflib.term.Literal(u'ab'),)
>>> res = g.query("SELECT (REGEX('foo','foo') AS ?c) { }")
>>> for r in res: print(r)
...
(rdflib.term.Literal(u'true', datatype=rdflib.term.URIRef(u'http://www.w3.org/2001/XMLSchema#boolean')),)
```

I ran the tests for py27 and py32 against my fix. In the py27 case, the amount of failures and errors remain the same. In case of py32 I improved from 34 failures and 7 errors to 8 failures and 6 errors.