diff options
-rw-r--r-- | Lib/sqlite3/test/types.py | 4 | ||||
-rw-r--r-- | Misc/NEWS.d/next/Library/2021-04-06-21-18-29.bpo-43752.K7qmAF.rst | 3 | ||||
-rw-r--r-- | Modules/_sqlite/cursor.c | 25 |
3 files changed, 20 insertions, 12 deletions
diff --git a/Lib/sqlite3/test/types.py b/Lib/sqlite3/test/types.py index 2370dd1..4bb1de8 100644 --- a/Lib/sqlite3/test/types.py +++ b/Lib/sqlite3/test/types.py @@ -255,7 +255,9 @@ class DeclTypesTests(unittest.TestCase): def test_convert_zero_sized_blob(self): self.con.execute("insert into test(cbin) values (?)", (b"",)) cur = self.con.execute("select cbin from test") - self.assertEqual(cur.fetchone()[0], b"blobish") + # Zero-sized blobs with converters returns None. This differs from + # blobs without a converter, where b"" is returned. + self.assertIsNone(cur.fetchone()[0]) class ColNamesTests(unittest.TestCase): diff --git a/Misc/NEWS.d/next/Library/2021-04-06-21-18-29.bpo-43752.K7qmAF.rst b/Misc/NEWS.d/next/Library/2021-04-06-21-18-29.bpo-43752.K7qmAF.rst new file mode 100644 index 0000000..ef4b953 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2021-04-06-21-18-29.bpo-43752.K7qmAF.rst @@ -0,0 +1,3 @@ +Fix :mod:`sqlite3` regression for zero-sized blobs with converters, where +``b""`` was returned instead of ``None``. The regression was introduced by +GH-24723. Patch by Erlend E. Aasland. diff --git a/Modules/_sqlite/cursor.c b/Modules/_sqlite/cursor.c index dfaa557..09c9a8c 100644 --- a/Modules/_sqlite/cursor.c +++ b/Modules/_sqlite/cursor.c @@ -240,7 +240,6 @@ _pysqlite_fetch_one_row(pysqlite_Cursor* self) { int i, numcols; PyObject* row; - PyObject* item = NULL; int coltype; PyObject* converter; PyObject* converted; @@ -282,18 +281,22 @@ _pysqlite_fetch_one_row(pysqlite_Cursor* self) */ if (converter != Py_None) { const void *blob = sqlite3_column_blob(self->statement->st, i); - if (blob == NULL && sqlite3_errcode(db) == SQLITE_NOMEM) { - PyErr_NoMemory(); - goto error; + if (blob == NULL) { + if (sqlite3_errcode(db) == SQLITE_NOMEM) { + PyErr_NoMemory(); + goto error; + } + converted = Py_NewRef(Py_None); } - - nbytes = sqlite3_column_bytes(self->statement->st, i); - item = PyBytes_FromStringAndSize(blob, nbytes); - if (item == NULL) { - goto error; + else { + nbytes = sqlite3_column_bytes(self->statement->st, i); + PyObject *item = PyBytes_FromStringAndSize(blob, nbytes); + if (item == NULL) { + goto error; + } + converted = PyObject_CallOneArg(converter, item); + Py_DECREF(item); } - converted = PyObject_CallOneArg(converter, item); - Py_DECREF(item); } else { Py_BEGIN_ALLOW_THREADS coltype = sqlite3_column_type(self->statement->st, i); |