Fixed empty strings handling in composite caster

Closes ticket #141.
This commit is contained in:
Daniele Varrazzo 2012-12-03 14:19:09 +00:00
parent bf45539585
commit 0653861bc4
3 changed files with 24 additions and 2 deletions

1
NEWS
View File

@ -18,6 +18,7 @@ What's new in psycopg 2.4.6
- Discard any result produced by 'executemany()' (ticket #133). - Discard any result produced by 'executemany()' (ticket #133).
- Fixed pickling of FixedOffsetTimezone objects (ticket #135). - Fixed pickling of FixedOffsetTimezone objects (ticket #135).
- Release the GIL around PQgetResult calls after COPY (ticket #140). - Release the GIL around PQgetResult calls after COPY (ticket #140).
- Fixed empty strings handling in composite caster (ticket #141).
What's new in psycopg 2.4.5 What's new in psycopg 2.4.5

View File

@ -861,9 +861,9 @@ class CompositeCaster(object):
for m in self._re_tokenize.finditer(s): for m in self._re_tokenize.finditer(s):
if m is None: if m is None:
raise psycopg2.InterfaceError("can't parse type: %r" % s) raise psycopg2.InterfaceError("can't parse type: %r" % s)
if m.group(1): if m.group(1) is not None:
rv.append(None) rv.append(None)
elif m.group(2): elif m.group(2) is not None:
rv.append(self._re_undouble.sub(r"\1", m.group(2))) rv.append(self._re_undouble.sub(r"\1", m.group(2)))
else: else:
rv.append(m.group(3)) rv.append(m.group(3))

View File

@ -503,6 +503,7 @@ class AdaptTypeTestCase(unittest.TestCase):
self.assertEqual(CompositeCaster.tokenize(s), v) self.assertEqual(CompositeCaster.tokenize(s), v)
ok("(,)", [None, None]) ok("(,)", [None, None])
ok('(,"")', [None, ''])
ok('(hello,,10.234,2010-11-11)', ['hello', None, '10.234', '2010-11-11']) ok('(hello,,10.234,2010-11-11)', ['hello', None, '10.234', '2010-11-11'])
ok('(10,"""")', ['10', '"']) ok('(10,"""")', ['10', '"'])
ok('(10,",")', ['10', ',']) ok('(10,",")', ['10', ','])
@ -556,6 +557,26 @@ class AdaptTypeTestCase(unittest.TestCase):
self.assertEqual(v.astring, "hello") self.assertEqual(v.astring, "hello")
self.assertEqual(v.adate, date(2011,1,2)) self.assertEqual(v.adate, date(2011,1,2))
@skip_if_no_composite
def test_empty_string(self):
# issue #141
self._create_type("type_ss", [('s1', 'text'), ('s2', 'text')])
curs = self.conn.cursor()
psycopg2.extras.register_composite("type_ss", curs)
def ok(t):
curs.execute("select %s::type_ss", (t,))
rv = curs.fetchone()[0]
self.assertEqual(t, rv)
ok(('a', 'b'))
ok(('a', ''))
ok(('', 'b'))
ok(('a', None))
ok((None, 'b'))
ok(('', ''))
ok((None, None))
@skip_if_no_composite @skip_if_no_composite
def test_cast_nested(self): def test_cast_nested(self):
self._create_type("type_is", self._create_type("type_is",