FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

[3.15] gh-150449: Raise ValueError for sqlite3.Blob slices with a negative step by serhiy-storchaka · Pull Request #155773 · python/cpython · GitHub

Repository navigation

Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) .py  (1) .rst  (1) All 3 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
13 changes: 13 additions & 0 deletions Lib/test/test_sqlite3/test_dbapi.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1396,6 +1396,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)
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -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`.
13 changes: 13 additions & 0 deletions Modules/_sqlite/blob.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,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);
}
Expand Down Expand Up @@ -548,6 +554,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);
Expand Down
Loading

Back | FazBrowse Home | New Git URL