shelve.Shelf.sync() leaves writeback disabled if writing a cached entry fails
Nadie ha tomado este issue todavía.
Evaluación
- Dificultad
- 2/5
- Tiempo estimado
- 1-3 horas
- Aptitud para principiantes
- 75/100
Línea de trabajo
El error está en Lib/shelve.py, específicamente en el método Shelf.sync(). Examina el bucle que escribe de nuevo las entradas en caché y dónde se establece self.writeback. Añade un bloque finally para restaurar writeback a su estado original ante cualquier excepción. Ejecuta el script de prueba proporcionado para verificar la corrección y comprueba si ya existen pruebas para shelve en la suite de pruebas (probablemente Lib/test/test_shelve.py).
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
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
- Lenguaje dominante
- Python
- Estrellas
- 77.2k
- Forks
- 36k
- Merge medio
- 1 d 12 h
- PR fusionados (30 d)
- 557
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de python/cpython
-
docs pending
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
-
stdlib type-feature
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
-
stdlib type-feature
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
-
build type-bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 76/100
-
stdlib topic-email type-feature
Dificultad 2/5 1-3 horas Aptitud para principiantes 70/100
Todos los issues de python/cpython
Issues similares
-
bug confirmed issue
Dificultad 2/5 1-3 horas Aptitud para principiantes 75/100
open-webui/open-webui#30750 · 1 comentario ·
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 75/100
-
enhancement
Dificultad 2/5 1-3 horas Aptitud para principiantes 75/100
OpenwaterHealth/openmotion-bloodflow-app#604 · 1 comentario ·
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 70/100
-
good first issue
Dificultad 1/5 Menos de una hora Aptitud para principiantes 90/100