https://github.com/python/cpython/commit/15045f45cee9761e6add6498c284396a957f7fcd
commit: 15045f45cee9761e6add6498c284396a957f7fcd
branch: 3.14
author: Serhiy Storchaka <[email protected]>
committer: serhiy-storchaka <[email protected]>
date: 2026-10-10T11:42:41Z
summary:

[3.14] gh-150449: Raise ValueError for sqlite3.Blob slices with a negative step 
(GH-155773) (GH-159110)

Reading or writing such a slice computed a negative length and failed with
SystemError.  Empty slices, which select nothing, are still allowed.
(cherry picked from commit 503560dcadd9eaa42f152115b495143f1fc64ff2)


(cherry picked from commit d9e8d226a3d05ea020256086aee24d21f9792206)

files:
A Misc/NEWS.d/next/Library/2026-08-14-09-28-21.gh-issue-150449.Nf3Qz1.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 42b835758e04a53..e5abcf40bd7b2dc 100644
--- a/Lib/test/test_sqlite3/test_dbapi.py
+++ b/Lib/test/test_sqlite3/test_dbapi.py
@@ -1418,6 +1418,19 @@ def test_blob_set_slice(self):
         actual = self.cx.execute("select b from test").fetchone()[0]
         self.assertEqual(actual, expected)
 
+    def test_blob_slice_with_negative_step(self):
+        # gh-150449: this used to raise SystemError
+        for sl in (slice(9, 0, -2), slice(None, None, -1), slice(9, None, -2)):
+            with self.subTest(slice=sl):
+                with self.assertRaises(ValueError):
+                    self.blob[sl]
+                with self.assertRaises(ValueError):
+                    self.blob[sl] = b"1" * len(self.data[sl])
+        # an empty slice selects nothing and is still allowed
+        self.assertEqual(self.blob[3:8:-1], b"")
+        self.blob[3:8:-1] = b""
+        self.assertEqual(self.blob[:], self.data)
+
     def test_blob_set_empty_slice(self):
         self.blob[0:0] = b""
         self.assertEqual(self.blob[:], self.data)
diff --git 
a/Misc/NEWS.d/next/Library/2026-08-14-09-28-21.gh-issue-150449.Nf3Qz1.rst 
b/Misc/NEWS.d/next/Library/2026-08-14-09-28-21.gh-issue-150449.Nf3Qz1.rst
new file mode 100644
index 000000000000000..5810596a5a507e5
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2026-08-14-09-28-21.gh-issue-150449.Nf3Qz1.rst
@@ -0,0 +1,3 @@
+Fix :class:`sqlite3.Blob` slices with a negative step.
+Reading or writing such a slice raised :exc:`SystemError`.
+It now raises :exc:`ValueError`.
diff --git a/Modules/_sqlite/blob.c b/Modules/_sqlite/blob.c
index a99e2e8bf35eb07..dd0255e417e830a 100644
--- a/Modules/_sqlite/blob.c
+++ b/Modules/_sqlite/blob.c
@@ -439,6 +439,12 @@ subscript_slice(pysqlite_Blob *self, PyObject *item)
         return PyBytes_FromStringAndSize(NULL, 0);
     }
 
+    if (step < 0) {
+        PyErr_SetString(PyExc_ValueError,
+                        "Blob doesn't support slices with a negative step");
+        return NULL;
+    }
+
     if (step == 1) {
         return read_multiple(self, len, start);
     }
@@ -541,6 +547,13 @@ ass_subscript_slice(pysqlite_Blob *self, PyObject *item, 
PyObject *value)
         return 0;
     }
 
+    if (step < 0) {
+        PyErr_SetString(PyExc_ValueError,
+                        "Blob doesn't support slices with a negative step");
+        PyBuffer_Release(&vbuf);
+        return -1;
+    }
+
     int rc = -1;
     if (step == 1) {
         rc = inner_write(self, vbuf.buf, 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