Skip to content

Commit f7c9f53

Browse files
committed
pythongh-150449: Address review: docs, tests, and rename write_offset
Classify negative-step slice support as a new feature per reviewer feedback (serhiy-storchaka): - Add versionchanged:: 3.16 directive to sqlite3.Blob documentation noting that negative-step slices are now supported. - Add What's New entry under Doc/whatsnew/3.16.rst. - Update NEWS entry wording to describe the change as a feature. - Add tests for empty-slice assignment edge cases (blob[5:5] = None and blob[5:5] = b'123') to verify TypeError and IndexError are raised correctly. - Rename read_offset to write_offset in ass_subscript_slice() since that variable is used as the offset for inner_write(), not a read.
1 parent 83d3a3c commit f7c9f53

5 files changed

Lines changed: 31 additions & 7 deletions

File tree

‎Doc/library/sqlite3.rst‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1751,6 +1751,10 @@ Blob objects
17511751

17521752
.. versionadded:: 3.11
17531753

1754+
.. versionchanged:: 3.16
1755+
:class:`Blob` now supports negative-step slices
1756+
(e.g. ``blob[9:0:-2]``) for both reading and writing.
1757+
17541758
A :class:`Blob` instance is a :term:`file-like object`
17551759
that can read and write data in an SQLite :abbr:`BLOB (Binary Large OBject)`.
17561760
Call :func:`len(blob) <len>` to get the size (number of bytes) of the blob.

‎Doc/whatsnew/3.16.rst‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,14 @@ os
101101
process via a pidfd. Available on Linux 5.6+.
102102
(Contributed by Maurycy Pawłowski-Wieroński in :gh:`149464`.)
103103

104+
sqlite3
105+
-------
106+
107+
* :class:`sqlite3.Blob` now supports negative-step slices for reading and
108+
writing (e.g. ``blob[9:0:-2]``). Previously, such slices would crash or
109+
raise :exc:`SystemError`.
110+
(Contributed by Jiseok CHOI in :gh:`150449`.)
111+
104112
.. Add improved modules above alphabetically, not here at the end.
105113
106114
Optimizations

‎Lib/test/test_sqlite3/test_dbapi.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1410,6 +1410,18 @@ def test_blob_set_empty_slice(self):
14101410
self.blob[0:0] = b""
14111411
self.assertEqual(self.blob[:], self.data)
14121412

1413+
def test_blob_set_empty_slice_wrong_type(self):
1414+
# Assigning a non-buffer object to an empty slice must raise TypeError
1415+
# even when the slice length is zero (gh-150449).
1416+
with self.assertRaises(TypeError):
1417+
self.blob[5:5] = None
1418+
1419+
def test_blob_set_empty_slice_wrong_size(self):
1420+
# Assigning a non-empty bytes object to an empty slice must raise
1421+
# IndexError because the sizes do not match (gh-150449).
1422+
with self.assertRaisesRegex(IndexError, "wrong size"):
1423+
self.blob[5:5] = b"123"
1424+
14131425
def test_blob_set_slice_with_skip(self):
14141426
self.blob[0:10:2] = b"12345"
14151427
actual = self.cx.execute("select b from test").fetchone()[0]
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
Fix :class:`sqlite3.Blob` raising :exc:`SystemError` (or :exc:`ValueError`
2-
on recent versions) when reading or writing with a negative-step slice such
3-
as ``blob[9:0:-2]``.
1+
:class:`sqlite3.Blob` now supports negative-step slices for reading and
2+
writing (e.g. ``blob[9:0:-2]``). Previously, such slices would crash with
3+
:exc:`SystemError` or raise :exc:`ValueError`.

‎Modules/_sqlite/blob.c‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -569,19 +569,19 @@ ass_subscript_slice(pysqlite_Blob *self, PyObject *item, PyObject *value)
569569
// update each element using the standard size_t-cursor pattern that
570570
// handles both positive and negative steps via unsigned arithmetic.
571571
Py_ssize_t last = start + (len - 1) * step;
572-
Py_ssize_t read_offset = Py_MIN(start, last);
572+
Py_ssize_t write_offset = Py_MIN(start, last);
573573
Py_ssize_t read_length = Py_ABS(start - last) + 1;
574-
PyObject *blob_bytes = read_multiple(self, read_length, read_offset);
574+
PyObject *blob_bytes = read_multiple(self, read_length, write_offset);
575575
if (blob_bytes != NULL) {
576576
char *blob_buf = PyBytes_AS_STRING(blob_bytes);
577577
size_t cur;
578578
Py_ssize_t i;
579579
for (cur = (size_t)start, i = 0; i < len;
580580
cur += (size_t)step, i++) {
581-
blob_buf[(Py_ssize_t)cur - read_offset] =
581+
blob_buf[(Py_ssize_t)cur - write_offset] =
582582
((char *)vbuf.buf)[i];
583583
}
584-
rc = inner_write(self, blob_buf, read_length, read_offset);
584+
rc = inner_write(self, blob_buf, read_length, write_offset);
585585
Py_DECREF(blob_bytes);
586586
}
587587
}

0 commit comments

Comments
 (0)