Don't convert '{}'::unknown into an empty list

Close #506.
This commit is contained in:
Daniele Varrazzo 2017-02-07 18:52:16 +00:00
parent 791befca18
commit 3e12522bc9
3 changed files with 8 additions and 12 deletions

2
NEWS
View File

@ -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

View File

@ -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);
}

View File

@ -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)