https://github.com/python/cpython/commit/cd3e9b3fd28072758b412e758df1b67c19ae01b0
commit: cd3e9b3fd28072758b412e758df1b67c19ae01b0
branch: 3.13
author: Miss Islington (bot) <[email protected]>
committer: erlend-aasland <[email protected]>
date: 2026-02-27T01:13:08+01:00
summary:

[3.13] gh-142787: Handle empty sqlite3 blob slices (GH-142824) (#145298)

(cherry picked from commit 06b0920f1292690a22ab2b271dfefe2c63cacf07)

Co-authored-by: A.Ibrahim <[email protected]>

files:
A Misc/NEWS.d/next/Library/2025-12-16-13-34-48.gh-issue-142787.wNitJX.rst
M Lib/test/test_sqlite3/test_dbapi.py
M Modules/_sqlite/blob.c

diff --git a/Lib/test/test_sqlite3/test_dbapi.py 
b/Lib/test/test_sqlite3/test_dbapi.py
index 7ad4136350c765..4d815bc3d59623 100644
--- a/Lib/test/test_sqlite3/test_dbapi.py
+++ b/Lib/test/test_sqlite3/test_dbapi.py
@@ -1410,6 +1410,11 @@ def test_blob_get_slice(self):
     def test_blob_get_empty_slice(self):
         self.assertEqual(self.blob[5:5], b"")
 
+    def test_blob_get_empty_slice_oob_indices(self):
+        self.cx.execute("insert into test(b) values (?)", (b"abc",))
+        with self.cx.blobopen("test", "b", 2) as blob:
+            self.assertEqual(blob[5:-5], b"")
+
     def test_blob_get_slice_negative_index(self):
         self.assertEqual(self.blob[5:-5], self.data[5:-5])
 
diff --git 
a/Misc/NEWS.d/next/Library/2025-12-16-13-34-48.gh-issue-142787.wNitJX.rst 
b/Misc/NEWS.d/next/Library/2025-12-16-13-34-48.gh-issue-142787.wNitJX.rst
new file mode 100644
index 00000000000000..e928bd2cac72a8
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2025-12-16-13-34-48.gh-issue-142787.wNitJX.rst
@@ -0,0 +1,2 @@
+Fix assertion failure in :mod:`sqlite3` blob subscript when slicing with
+indices that result in an empty slice.
diff --git a/Modules/_sqlite/blob.c b/Modules/_sqlite/blob.c
index 6ad3f9c0968313..4db1ac474ef253 100644
--- a/Modules/_sqlite/blob.c
+++ b/Modules/_sqlite/blob.c
@@ -428,6 +428,10 @@ subscript_slice(pysqlite_Blob *self, PyObject *item)
         return NULL;
     }
 
+    if (len == 0) {
+        return PyBytes_FromStringAndSize(NULL, 0);
+    }
+
     if (step == 1) {
         return read_multiple(self, len, start);
     }

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to