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 02ffb7423a
commit 9a031db8b9
3 changed files with 24 additions and 2 deletions

1
NEWS
View File

@ -33,6 +33,7 @@ What's new in psycopg 2.4.5
- 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

@ -852,9 +852,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

@ -501,6 +501,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', ','])
@ -555,6 +556,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",