Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

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

Aperta Adatta ai principianti
#157,976 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
75/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python
Ambito
backend

Direzione di ricerca

Il bug si trova in Lib/shelve.py, specificatamente nel metodo Shelf.sync(). Esamina il ciclo che riscrive le voci memorizzate nella cache e dove viene impostato self.writeback. Aggiungi un blocco finally per ripristinare writeback al suo stato originale in caso di qualsiasi eccezione. Esegui lo script di test fornito per verificare la correzione e controlla se esistono già test per shelve nella suite di test (probabilmente Lib/test/test_shelve.py).

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

Descrizione

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
Lingua principale
Python
Stelle
77.2k
Fork
36k
Merge medio
1g 12h
PR unite (30g)
557

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.

Altre issue di python/cpython

Tutte le issue di python/cpython

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.