Skip to content

Commit c0c2018

Browse files
committed
pythongh-150449: add tests for negative-step slices with start <= stop
Exercise the case where step is negative but start <= stop, which produces an empty slice. Verify that: - blob[3:8:-1] returns b"" (no crash) - blob[3:8:-1] = b"" is a no-op - blob[3:8:-1] = b"abc" raises IndexError (wrong size) Also add blob[5:5:-1] == b"" to cover the start == stop edge case.
1 parent 85312b4 commit c0c2018

1 file changed

Lines changed: 13 additions & 0 deletions

File tree

‎Lib/test/test_sqlite3/test_dbapi.py‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1395,6 +1395,10 @@ def test_blob_get_slice_with_negative_step(self):
13951395
self.assertEqual(self.blob[9:0:-2], self.data[9:0:-2])
13961396
self.assertEqual(self.blob[9::-2], self.data[9::-2])
13971397
self.assertEqual(self.blob[::-1], self.data[::-1])
1398+
# When start <= stop with a negative step the slice is empty; this
1399+
# must return b"" rather than crashing or raising an exception.
1400+
self.assertEqual(self.blob[3:8:-1], self.data[3:8:-1]) # b""
1401+
self.assertEqual(self.blob[5:5:-1], self.data[5:5:-1]) # b""
13981402

13991403
def test_blob_set_slice(self):
14001404
self.blob[0:5] = b"12345"
@@ -1427,6 +1431,15 @@ def test_blob_set_slice_with_negative_step(self):
14271431
actual2 = self.cx.execute("select b from test").fetchone()[0]
14281432
self.assertEqual(actual2, bytes(expected2))
14291433

1434+
# When start <= stop with a negative step the slice is empty;
1435+
# assigning b"" to it must be a no-op (blob contents unchanged).
1436+
state_before = bytes(self.blob[:])
1437+
self.blob[3:8:-1] = b""
1438+
self.assertEqual(bytes(self.blob[:]), state_before)
1439+
# Assigning a non-empty sequence to an empty slice must raise.
1440+
with self.assertRaisesRegex(IndexError, "wrong size"):
1441+
self.blob[3:8:-1] = b"abc"
1442+
14301443
def test_blob_mapping_invalid_index_type(self):
14311444
msg = "indices must be integers"
14321445
with self.assertRaisesRegex(TypeError, msg):

0 commit comments

Comments
 (0)