Title: Open shelves fail when Python exits
Type: behavior Stage:
Components: Documentation, Extension Modules, Library (Lib) Versions: Python 3.1, Python 2.7
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: georg.brandl Nosy List: georg.brandl, lehmannro
Priority: normal Keywords: patch

Created on 2009-09-17 15:55 by lehmannro, last changed 2009-09-17 16:16 by georg.brandl. This issue is now closed.

File name Uploaded Description Edit lehmannro, 2009-09-17 15:55 minimal example reproducing the error
shelve-warning.patch lehmannro, 2009-09-17 15:56 patch to Doc/library/shelve.rst, trunk
Messages (2)
msg92773 - (view) Author: Robert Lehmann (lehmannro) * Date: 2009-09-17 15:55
I'm reopening issue5483 by Zhigang Wang (zhigang) as a separate bug.

Shelves that are still open when Python terminates will try to sync. If
writeback=True, this pickles cached items.

In this example, serialization of Test() re-imports __main__, which is
already gc'd, and raises:
Exception cPickle.PicklingError: Can't pickle <class '__main__.Test'>:
it's not the same object as __main__.Test" in <bound method
Shelf.__del__ of {'a': <__main__.Test object at 0x...>}>

The error is ignored (due to Python already tearing down) but all cached
modifications are lost.
The promise "[t]he __del__() method of the Shelf class calls the close()
method, so the programmer generally need not do this explicitly" is not
true with writeback enabled.

I'm unsure if this error can be fixed (probably with atexit/weakref, but
that's more trouble than gain). I attached a patch to the docs simply
warning the user of this issue (+ documenting Shelf.close, + removing
above quote).
msg92776 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2009-09-17 16:16
Fixed docs (a bit differently) in r74876.
Date User Action Args
2009-09-17 16:16:19georg.brandlsetstatus: open -> closed
resolution: fixed
messages: + msg92776
2009-09-17 15:56:43lehmannrosetfiles: + shelve-warning.patch
keywords: + patch
2009-09-17 15:55:56lehmannrocreate