sqlite3: Blob crashes when using a negative-step slice
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 77.2k
- Forks
- 35.9k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
What happened?
sqlite3.Blob crashes when you use a negative step in a slice — both for reading and writing.
The sequence operations docs say s[i:j:k] with negative k is valid, and bytearray handles it fine.
import sqlite3
con = sqlite3.connect(":memory:")
con.execute("CREATE TABLE t(b BLOB)")
data = b"this blob data string is exactly fifty bytes long!"
con.execute("INSERT INTO t(b) VALUES (?)", (data,))
# bytearray works
ba = bytearray(data)
ba[9:0:-2] = b"12345"
print(ba)
# b't5i4 3l2b1data string is exactly fifty bytes long!'
# Blob crashes
blob = con.blobopen("t", "b", 1)
blob[9:0:-2] = b"12345"
blob.close()
>>> blob[9:0:-2] = b"12345"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
SystemError: Negative size passed to PyBytes_FromStringAndSize
Reading also crashes: blob[9:0:-2] raises the same error.
On Python 3.11.6 and 3.14.5 the error is SystemError: Negative size passed to PyBytes_FromStringAndSize.
On the current main branch read_multiple was changed to use PyBytesWriter_Create, so the message is now ValueError: size must be >= 0 — but the root cause is the same.
CPython versions tested on:
3.11.6, 3.14.5, main(629da5c914b4407e01c1dc06cbcbd8dce825fef3)
Operating systems tested on:
macOS - 26.3.1 (a)(25D771280a)
Related
- gh-142787 / gh-142824 — different bug (assertion failure on empty slice, already fixed)
- https://docs.python.org/3/library/stdtypes.html#typesseq-common (
s[i:j:k], negative k)
I'd like to work on a fix for this. I'll submit a PR.
Linked PRs
- gh-150450
- gh-155773
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne mit der sqlite3.Blob-Slicing-Implementierung und reproduziere die gemeldeten Fälle mit negativem Schritt sowohl beim Lesen als auch beim Schreiben anhand des Beispiels im Issue. Als erledigt gilt dies, wenn Slices mit negativem Schritt sich wie die dokumentierten Sequenzoperationen verhalten, ohne einen größenbezogenen Fehler auszulösen; die verknüpften PRs gh-150450 und gh-155773 zeigen, dass diese Arbeit bereits im Gange ist.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python, sqlite
- Bereich
- databases
- Issue-Typ
- Bug
- Schwierigkeit
- 3/5
- Geschätzter Aufwand
- 1-2 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Klar beschrieben
- Anfängerfreundlichkeit
- 25/100