]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-43368: Fix fetching empty bytes in sqlite3 (GH-24706)
authorMariusz Felisiak <felisiak.mariusz@gmail.com>
Wed, 3 Mar 2021 14:16:24 +0000 (15:16 +0100)
committerGitHub <noreply@github.com>
Wed, 3 Mar 2021 14:16:24 +0000 (16:16 +0200)
Regression introduced in 47feb1feb28631b6647699b7633109aa85340966.

Lib/sqlite3/test/regression.py
Misc/NEWS.d/next/Library/2021-03-02-13-45-05.bpo-43368.t9XEkQ.rst [new file with mode: 0644]
Modules/_sqlite/cursor.c

index c8e0b27564ad1094b540145d70ecfb23a422368b..417a53109c87c72fbc43b956614e42d3486479c9 100644 (file)
@@ -409,6 +409,10 @@ class RegressionTests(unittest.TestCase):
             self.con.execute("select 1")  # trigger seg fault
             method(None)
 
+    def test_return_empty_bytestring(self):
+        cur = self.con.execute("select X''")
+        val = cur.fetchone()[0]
+        self.assertEqual(val, b'')
 
 
 def suite():
diff --git a/Misc/NEWS.d/next/Library/2021-03-02-13-45-05.bpo-43368.t9XEkQ.rst b/Misc/NEWS.d/next/Library/2021-03-02-13-45-05.bpo-43368.t9XEkQ.rst
new file mode 100644 (file)
index 0000000..f9a4aa2
--- /dev/null
@@ -0,0 +1,2 @@
+Fix a regression introduced in GH-24562, where an empty bytestring was fetched
+as ``None`` instead of ``b''`` in :mod:`sqlite3`. Patch by Mariusz Felisiak.
index 23ab7451fdaa00d073e7067ad6b9ae46859fa788..764eec5fbcac6dae7aa042d437e3a6532c851b96 100644 (file)
@@ -333,12 +333,8 @@ _pysqlite_fetch_one_row(pysqlite_Cursor* self)
             } else {
                 /* coltype == SQLITE_BLOB */
                 const char *blob = sqlite3_column_blob(self->statement->st, i);
-                if (!blob) {
-                    converted = Py_NewRef(Py_None);
-                } else {
-                    nbytes = sqlite3_column_bytes(self->statement->st, i);
-                    converted = PyBytes_FromStringAndSize(blob, nbytes);
-                }
+                nbytes = sqlite3_column_bytes(self->statement->st, i);
+                converted = PyBytes_FromStringAndSize(blob, nbytes);
             }
         }