Correctly parse escaped quotes from hstore.

Parse regexp simplified.
This commit is contained in:
Daniele Varrazzo 2010-09-26 22:53:02 +01:00
parent 70880dde79
commit af835f8857
2 changed files with 5 additions and 9 deletions

View File

@ -523,18 +523,13 @@ class HstoreAdapter(object):
_re_hstore = regex.compile(r"""
# hstore key:
"( # catch in quotes
(?: # many of
[^"] # either not a quote
# or a quote escaped, i.e. preceded by an odd number of backslashes
| [^\\] (?:\\\\)* \\"
)*
)"
# a string of normal or escaped chars
"((?: [^"\\] | \\. )*)"
\s*=>\s* # hstore value
(?:
NULL # the value can be null - not catched
# or the same quoted string of the key
| "((?:[^"] | [^\\] (?:\\\\)* \\" )*)"
# or a quoted string like the key
| "((?: [^"\\] | \\. )*)"
)
(?:\s*,\s*|$) # pairs separated by comma or end of string.
""", regex.VERBOSE)

View File

@ -168,6 +168,7 @@ class HstoreTestCase(unittest.TestCase):
ok('"a"=>"1", "b"=>"2"', {'a': '1', 'b': '2'})
ok('"a" => "1" ,"b" => "2"', {'a': '1', 'b': '2'})
ok('"a"=>NULL, "b"=>"2"', {'a': None, 'b': '2'})
ok(r'"a"=>"\"", "\""=>"2"', {'a': '"', '"': '2'})
ok('"a"=>"\'", "\'"=>"2"', {'a': "'", "'": '2'})
ok('"a"=>"1", "b"=>NULL', {'a': '1', 'b': None})
ok(r'"a\\"=>"1"', {'a\\': '1'})