mirror of
https://github.com/psycopg/psycopg2.git
synced 2025-06-29 17:23:07 +03:00
parse_hstore converted in class method.
This commit is contained in:
parent
5844e989c4
commit
fef9727cce
|
@ -35,7 +35,7 @@ try:
|
|||
except:
|
||||
logging = None
|
||||
|
||||
from psycopg2 import DATETIME, DataError, InterfaceError
|
||||
import psycopg2
|
||||
from psycopg2 import extensions as _ext
|
||||
from psycopg2.extensions import cursor as _cursor
|
||||
from psycopg2.extensions import connection as _connection
|
||||
|
@ -533,7 +533,7 @@ _re_hstore = regex.compile(r"""
|
|||
(?:\s*,\s*|$) # pairs separated by comma or end of string.
|
||||
""", regex.VERBOSE)
|
||||
|
||||
def parse_hstore(s, cur):
|
||||
def parse(self, s, cur):
|
||||
"""Parse an hstore representation in a Python string.
|
||||
|
||||
The hstore is represented as something like::
|
||||
|
@ -547,9 +547,9 @@ def parse_hstore(s, cur):
|
|||
|
||||
rv = {}
|
||||
start = 0
|
||||
for m in _re_hstore.finditer(s):
|
||||
for m in self._re_hstore.finditer(s):
|
||||
if m is None or m.start() != start:
|
||||
raise InterfaceError(
|
||||
raise psycopg2.InterfaceError(
|
||||
"error parsing hstore pair at char %d" % start)
|
||||
k = m.group(1).decode("string_escape")
|
||||
v = m.group(2)
|
||||
|
@ -560,10 +560,12 @@ def parse_hstore(s, cur):
|
|||
start = m.end()
|
||||
|
||||
if start < len(s):
|
||||
raise InterfaceError(
|
||||
raise psycopg2.InterfaceError(
|
||||
"error parsing hstore: unparsed data after char %d" % start)
|
||||
|
||||
return rv
|
||||
|
||||
parse = classmethod(parse)
|
||||
|
||||
|
||||
__all__ = filter(lambda k: not k.startswith('_'), locals().keys())
|
||||
|
|
|
@ -158,10 +158,10 @@ class HstoreTestCase(unittest.TestCase):
|
|||
self.assertEqual(ii[3], ("E'd'", "E'%s'" % encc))
|
||||
|
||||
def test_parse(self):
|
||||
from psycopg2.extras import parse_hstore
|
||||
from psycopg2.extras import HstoreAdapter
|
||||
|
||||
def ok(s, d):
|
||||
self.assertEqual(parse_hstore(s, None), d)
|
||||
self.assertEqual(HstoreAdapter.parse(s, None), d)
|
||||
|
||||
ok(None, None)
|
||||
ok('', {})
|
||||
|
@ -176,7 +176,8 @@ class HstoreTestCase(unittest.TestCase):
|
|||
ok(r'"a\\\\\""=>"1"', {r'a\\"': '1'})
|
||||
|
||||
def ko(s):
|
||||
self.assertRaises(psycopg2.InterfaceError, parse_hstore, s, None)
|
||||
self.assertRaises(psycopg2.InterfaceError,
|
||||
HstoreAdapter.parse, s, None)
|
||||
|
||||
ko('a')
|
||||
ko('"a"')
|
||||
|
|
Loading…
Reference in New Issue
Block a user