Skip to content

Commit 6f1e8cc

Browse files
author
Erlend Egeberg Aasland
authored
bpo-43752: Fix sqlite3 regression for zero-sized blobs with converters (GH-25228)
1 parent 333d10c commit 6f1e8cc

File tree

3 files changed

+20
-12
lines changed

3 files changed

+20
-12
lines changed

Lib/sqlite3/test/types.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,9 @@ def test_number2(self):
255255
def test_convert_zero_sized_blob(self):
256256
self.con.execute("insert into test(cbin) values (?)", (b"",))
257257
cur = self.con.execute("select cbin from test")
258-
self.assertEqual(cur.fetchone()[0], b"blobish")
258+
# Zero-sized blobs with converters returns None. This differs from
259+
# blobs without a converter, where b"" is returned.
260+
self.assertIsNone(cur.fetchone()[0])
259261

260262

261263
class ColNamesTests(unittest.TestCase):
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix :mod:`sqlite3` regression for zero-sized blobs with converters, where
2+
``b""`` was returned instead of ``None``. The regression was introduced by
3+
GH-24723. Patch by Erlend E. Aasland.

Modules/_sqlite/cursor.c

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,6 @@ _pysqlite_fetch_one_row(pysqlite_Cursor* self)
240240
{
241241
int i, numcols;
242242
PyObject* row;
243-
PyObject* item = NULL;
244243
int coltype;
245244
PyObject* converter;
246245
PyObject* converted;
@@ -282,18 +281,22 @@ _pysqlite_fetch_one_row(pysqlite_Cursor* self)
282281
*/
283282
if (converter != Py_None) {
284283
const void *blob = sqlite3_column_blob(self->statement->st, i);
285-
if (blob == NULL && sqlite3_errcode(db) == SQLITE_NOMEM) {
286-
PyErr_NoMemory();
287-
goto error;
284+
if (blob == NULL) {
285+
if (sqlite3_errcode(db) == SQLITE_NOMEM) {
286+
PyErr_NoMemory();
287+
goto error;
288+
}
289+
converted = Py_NewRef(Py_None);
288290
}
289-
290-
nbytes = sqlite3_column_bytes(self->statement->st, i);
291-
item = PyBytes_FromStringAndSize(blob, nbytes);
292-
if (item == NULL) {
293-
goto error;
291+
else {
292+
nbytes = sqlite3_column_bytes(self->statement->st, i);
293+
PyObject *item = PyBytes_FromStringAndSize(blob, nbytes);
294+
if (item == NULL) {
295+
goto error;
296+
}
297+
converted = PyObject_CallOneArg(converter, item);
298+
Py_DECREF(item);
294299
}
295-
converted = PyObject_CallOneArg(converter, item);
296-
Py_DECREF(item);
297300
} else {
298301
Py_BEGIN_ALLOW_THREADS
299302
coltype = sqlite3_column_type(self->statement->st, i);

0 commit comments

Comments
 (0)