shelve.Shelf.sync() leaves writeback disabled if writing a cached entry fails
Ninguém assumiu esta issue ainda.
Avaliação
- Dificuldade
- 2/5
- Tempo estimado
- 1-3 horas
- Facilidade para iniciantes
- 75/100
Direção de pesquisa
O bug está em Lib/shelve.py, especificamente no método Shelf.sync(). Examine o loop que reescreve as entradas em cache e onde self.writeback é definido. Adicione um bloco finally para restaurar writeback ao seu estado original em qualquer exceção. Execute o script de teste fornecido para verificar a correção e verifique se já existem testes para shelve na suíte de testes (provavelmente Lib/test/test_shelve.py).
Escrita pelo modelo de indexação a partir do texto da issue.
Descrição
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
- Linguagem predominante
- Python
- Estrelas
- 77.2k
- Forks
- 36k
- Merge médio
- 1d 12h
- PRs com merge (30d)
- 557
Guia de contribuição
Primeiros passos
- Leia a issue inteira e depois o guia de contribuição do projeto.
- Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
- Faça um fork do repositório e trabalhe em uma branch.
- Abra um pull request que referencie o número da issue.
Mais de python/cpython
-
docs pending
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 78/100
-
stdlib type-feature
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 78/100
-
stdlib type-feature
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 72/100
-
build type-bug
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 76/100
-
stdlib topic-email type-feature
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 70/100
Todas as issues de python/cpython
Issues semelhantes
-
bug confirmed issue
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 75/100
open-webui/open-webui#30750 · 1 comentário ·
-
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 75/100
-
enhancement
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 75/100
OpenwaterHealth/openmotion-bloodflow-app#604 · 1 comentário ·
-
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 70/100
-
good first issue
Dificuldade 1/5 Menos de uma hora Facilidade para iniciantes 90/100