From af835f885710a89c54a2ec04c87f7e3df4d6f29f Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Sun, 26 Sep 2010 22:53:02 +0100 Subject: [PATCH] Correctly parse escaped quotes from hstore. Parse regexp simplified. --- lib/extras.py | 13 ++++--------- tests/types_extras.py | 1 + 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/lib/extras.py b/lib/extras.py index 0af58688..d9872c53 100644 --- a/lib/extras.py +++ b/lib/extras.py @@ -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) diff --git a/tests/types_extras.py b/tests/types_extras.py index 26ad3123..1671fe2b 100644 --- a/tests/types_extras.py +++ b/tests/types_extras.py @@ -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'})