Hacktoberfest 2026:維護者為十月標記出來的 issue,仍然開放、適合新手。 瀏覽 Hacktoberfest issue

shelve.Shelf.sync() leaves writeback disabled if writing a cached entry fails

未關閉 適合新手
#157,976 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

評估

難度
2/5
預估耗時
1-3 小時
新手友好度
75/100
Issue 類型
缺陷
描述清晰度
描述清楚
活躍度
活躍
技術堆疊
python
領域
backend

研究方向

該錯誤位於 Lib/shelve.py 中,具體在 Shelf.sync() 方法中。查看將快取條目寫回的迴圈以及設定 self.writeback 的位置。添加一個 finally 區塊,以便在任何例外時將 writeback 恢復到其原始狀態。執行提供的測試腳本來驗證修復,並檢查測試套件中是否已有針對 shelve 的測試(可能在 Lib/test/test_shelve.py 中)。

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

描述

stdlib type-bug

Bug report

Bug description:

Shelf.sync() sets self.writeback to False while it writes the cached entries back, and only sets it back to True after the loop. If writing one of the entries raises (for example, a cached object that can no longer be pickled), the exception propagates as expected, but writeback stays False. The shelf keeps working, and later mutations of accessed objects are silently lost:

import shelve

class Unpicklable:
    def __reduce__(self):
        raise RuntimeError("cannot pickle")

d = {}
s = shelve.Shelf(d, writeback=True)
s["a"] = []
s["a"].append(Unpicklable())
try:
    s.sync()
except RuntimeError:
    pass
print(s.writeback)  # False, expected True

s["a"].clear()
s["b"] = [1]
s["b"].append(2)
s.close()
print(shelve.Shelf(d)["b"])  # [1], expected [1, 2]

The cached entries are still in s.cache after the failure, so the caller could fix the offending object and call sync() again, but by then the shelf is no longer in writeback mode.

Restoring writeback in a finally block would fix this, leaving the cache intact so sync() can be retried.

CPython versions tested on:

3.13, 3.14, 3.15, 3.16, CPython main branch

Operating systems tested on:

Linux

Linked PRs
  • gh-157977
主要語言
Python
星號
77.2k
分支
36k
平均合併
1 天 12 小時
30 天內合併 PR
557

貢獻指南

開啟貢獻指南

從這裡開始

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

python/cpython 的其他 Issue

查看 python/cpython 的全部 Issue

相似的 Issue

更多 Python Issue

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

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