python / python/cpython

sqlite3: Blob crashes when using a negative-step slice

未關閉
#150,449 7 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

3.13 3.14 3.15 3.16 extension-modules topic-sqlite3 type-bug type-feature
主要語言
Python
星號
77.2k
分支
35.9k
PR 合併指標
PR 指標待擷取

描述

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

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

從 sqlite3.Blob 切片實作開始,使用 issue 中的範例重現回報的負步長情況,包括讀取和寫入。完成的標準是負步長切片的行為與文件所述的序列操作一致,且不會引發與大小相關的錯誤;已連結的 PR gh-150450 和 gh-155773 表明相關工作已經在進行中。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
python, sqlite
領域
databases
Issue 類型
缺陷
難度
3/5
預估耗時
1-2 天
活躍度
停滯
描述清晰度
描述清楚
新手友好度
25/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。