python / python/cpython

sqlite3: Blob crashes when using a negative-step slice

Aperta
#150,449 7 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

3.13 3.14 3.15 3.16 extension-modules topic-sqlite3 type-bug type-feature
Lingua principale
Python
Stelle
77.2k
Fork
35.9k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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

I'd like to work on a fix for this. I'll submit a PR.

Linked PRs
  • gh-150450
  • gh-155773

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia con l’implementazione dello slicing di sqlite3.Blob e riproduci i casi segnalati con passo negativo sia per la lettura sia per la scrittura, usando l’esempio nell’issue. Il lavoro è completo quando gli slice con passo negativo si comportano come le operazioni di sequenza documentate senza sollevare un errore relativo alle dimensioni; le PR collegate gh-150450 e gh-155773 mostrano che il lavoro è già in corso.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python, sqlite
Ambito
databases
Tipo di issue
Bug
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Ferma
Chiarezza
Specificata chiaramente
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.