From 3e12522bc926609b33079746085222301b68bcac Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Tue, 7 Feb 2017 18:52:16 +0000 Subject: [PATCH] Don't convert '{}'::unknown into an empty list Close #506. --- NEWS | 2 ++ psycopg/typecast.c | 8 -------- tests/test_types_basic.py | 10 ++++++---- 3 files changed, 8 insertions(+), 12 deletions(-) diff --git a/NEWS b/NEWS index b87c1101..33461e51 100644 --- a/NEWS +++ b/NEWS @@ -55,6 +55,8 @@ Other changes: connection. - `~connection.set_isolation_level()` will throw an exception if executed inside a transaction; previously it would have silently rolled it back. +- Empty arrays no more converted into lists if they don't have a type attached + (:ticket:`#506`) What's new in psycopg 2.6.3 diff --git a/psycopg/typecast.c b/psycopg/typecast.c index a4f123c3..4fd92be0 100644 --- a/psycopg/typecast.c +++ b/psycopg/typecast.c @@ -191,14 +191,6 @@ typecast_UNKNOWN_cast(const char *str, Py_ssize_t len, PyObject *curs) Dprintf("typecast_UNKNOWN_cast: str = '%s'," " len = " FORMAT_CODE_PY_SSIZE_T, str, len); - // PostgreSQL returns {} for empty array without explicit type. We convert - // that to list in order to handle empty lists. - if (len == 2 && str[0] == '{' && str[1] == '}') { - return PyList_New(0); - } - - Dprintf("typecast_UNKNOWN_cast: fallback to default cast"); - return typecast_default.cast(str, len, curs); } diff --git a/tests/test_types_basic.py b/tests/test_types_basic.py index d17e5297..b4a5f2c6 100755 --- a/tests/test_types_basic.py +++ b/tests/test_types_basic.py @@ -166,12 +166,14 @@ class TypesBasicTests(ConnectingTestCase): curs.execute("select col from array_test where id = 2") self.assertEqual(curs.fetchone()[0], []) - def testEmptyArray(self): + def testEmptyArrayNoCast(self): s = self.execute("SELECT '{}' AS foo") - self.failUnlessEqual(s, []) - s = self.execute("SELECT '{}'::text[] AS foo") - self.failUnlessEqual(s, []) + self.assertEqual(s, '{}') s = self.execute("SELECT %s AS foo", ([],)) + self.assertEqual(s, '{}') + + def testEmptyArray(self): + s = self.execute("SELECT '{}'::text[] AS foo") self.failUnlessEqual(s, []) s = self.execute("SELECT 1 != ALL(%s)", ([],)) self.failUnlessEqual(s, True)