Skip to content

Commit

Permalink
Fixed adaptation of lists of empty lists
Browse files Browse the repository at this point in the history
...somehow. Postgres doesn't support them and converts them into a
simple empty array. However this is not really our concern: the syntax
we return is valid.

Close #788
  • Loading branch information
dvarrazzo committed Oct 29, 2018
1 parent 25e6d01 commit 3e75b6f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
1 change: 1 addition & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ What's new in psycopg 2.7.6
(:ticket:`#746`).
- Fixed building on modern FreeBSD versions with Python 3.7 (:ticket:`#755`).
- Fixed hang trying to :sql:`COPY` via `~cursor.execute()` (:ticket:`#781`).
- Fixed adaptation of arrays of empty arrays (:ticket:`#788`).
- Fixed segfault accessing the `connection.readonly` and
`connection.deferrable` repeatedly (:ticket:`#790`).
- `~psycopg2.extras.execute_values()` accepts `~psycopg2.sql.Composable`
Expand Down
16 changes: 15 additions & 1 deletion psycopg/adapter_list.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ list_quote(listObject *self)
/* empty arrays are converted to NULLs (still searching for a way to
insert an empty array in postgresql */
if (len == 0) {
/* it cannot be ARRAY[] because it would make empty lists unusable
* in any() without a cast. But we may convert it into ARRAY[] below */
res = Bytes_FromString("'{}'");
goto exit;
}
Expand All @@ -79,7 +81,19 @@ list_quote(listObject *self)

/* Lists of arrays containing only nulls are also not supported
* by the ARRAY construct so we should do some special casing */
if (!PyList_Check(wrapped) || Bytes_AS_STRING(qs[i])[0] == 'A') {
if (PyList_Check(wrapped)) {
if (Bytes_AS_STRING(qs[i])[0] == 'A') {
all_nulls = 0;
}
else if (0 == strcmp(Bytes_AS_STRING(qs[i]), "'{}'")) {
/* case of issue #788: '{{}}' is not supported but
* array[array[]] is */
all_nulls = 0;
Py_CLEAR(qs[i]);
qs[i] = Bytes_FromString("ARRAY[]");
}
}
else {
all_nulls = 0;
}
}
Expand Down
4 changes: 4 additions & 0 deletions tests/test_types_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@ def testEmptyArrayRegression(self):
curs.execute("select col from array_test where id = 2")
self.assertEqual(curs.fetchone()[0], [])

# issue #788
curs.execute("select 10 = any(%s::int[])", ([[]], ))
self.assertFalse(curs.fetchone()[0])

def testEmptyArrayNoCast(self):
s = self.execute("SELECT '{}' AS foo")
self.assertEqual(s, '{}')
Expand Down

0 comments on commit 3e75b6f

Please sign in to comment.