Skip to content

Commit 06b0920

Browse files
gh-142787: Handle empty sqlite3 blob slices (#142824)
1 parent 3fc945d commit 06b0920

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
@@ -1379,6 +1379,11 @@ def test_blob_get_slice(self):
13791379
def test_blob_get_empty_slice(self):
13801380
self.assertEqual(self.blob[5:5], b"")
13811381

1382+
def test_blob_get_empty_slice_oob_indices(self):
1383+
self.cx.execute("insert into test(b) values (?)", (b"abc",))
1384+
with self.cx.blobopen("test", "b", 2) as blob:
1385+
self.assertEqual(blob[5:-5], b"")
1386+
13821387
def test_blob_get_slice_negative_index(self):
13831388
self.assertEqual(self.blob[5:-5], self.data[5:-5])
13841389

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
@@ -439,6 +439,10 @@ subscript_slice(pysqlite_Blob *self, PyObject *item)
439439
return NULL;
440440
}
441441

442+
if (len == 0) {
443+
return PyBytes_FromStringAndSize(NULL, 0);
444+
}
445+
442446
if (step == 1) {
443447
return read_multiple(self, len, start);
444448
}

0 commit comments

Comments
 (0)