Hacktoberfest 2026: as issues que os mantenedores marcaram para outubro, abertas e boas para iniciantes. Ver issues do Hacktoberfest

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

Aberta Para iniciantes
#157,976 0 comentários 0 reações 0 responsáveis Ver no GitHub

Ninguém assumiu esta issue ainda.

Avaliação

Dificuldade
2/5
Tempo estimado
1-3 horas
Facilidade para iniciantes
75/100
Tipo de issue
Bug
Clareza
Claramente especificada
Status de atividade
Ativa
Stack de tecnologia
python
Domínio
backend

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

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
Linguagem predominante
Python
Estrelas
77.2k
Forks
36k
Merge médio
1d 12h
PRs com merge (30d)
557

Guia de contribuição

Abrir o guia de contribuição

Primeiros passos

  1. Leia a issue inteira e depois o guia de contribuição do projeto.
  2. Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
  3. Faça um fork do repositório e trabalhe em uma branch.
  4. Abra um pull request que referencie o número da issue.

Mais de python/cpython

Todas as issues de python/cpython

Issues semelhantes

Mais issues de Python

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.