Skip to content

Commit cd3e9b3

Browse files
[3.13] gh-142787: Handle empty sqlite3 blob slices (GH-142824) (#145298)
(cherry picked from commit 06b0920) Co-authored-by: A.Ibrahim <abdulrasheedibrahim47@gmail.com>
1 parent 083477f commit cd3e9b3

File tree

3 files changed

+11
-0
lines changed

3 files changed

+11
-0
lines changed

Lib/test/test_sqlite3/test_dbapi.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1410,6 +1410,11 @@ def test_blob_get_slice(self):
14101410
def test_blob_get_empty_slice(self):
14111411
self.assertEqual(self.blob[5:5], b"")
14121412

1413+
def test_blob_get_empty_slice_oob_indices(self):
1414+
self.cx.execute("insert into test(b) values (?)", (b"abc",))
1415+
with self.cx.blobopen("test", "b", 2) as blob:
1416+
self.assertEqual(blob[5:-5], b"")
1417+
14131418
def test_blob_get_slice_negative_index(self):
14141419
self.assertEqual(self.blob[5:-5], self.data[5:-5])
14151420

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix assertion failure in :mod:`sqlite3` blob subscript when slicing with
2+
indices that result in an empty slice.

Modules/_sqlite/blob.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,10 @@ subscript_slice(pysqlite_Blob *self, PyObject *item)
428428
return NULL;
429429
}
430430

431+
if (len == 0) {
432+
return PyBytes_FromStringAndSize(NULL, 0);
433+
}
434+
431435
if (step == 1) {
432436
return read_multiple(self, len, start);
433437
}

0 commit comments

Comments
 (0)