Really weird issue with shelve (python)

python, shelve

Solution

I think you've hit a minor bug in 2.6.4's code related to the cleanup at end of program. If you run `python -v` you can see exactly at what point of the cleanup the error comes:

# cleanup[1] foo_package.g
Exception TypeError: "'NoneType' object is not callable" in  ignored

Python sets references to `None` during the cleanup at the end of program, and it looks like it's getting confused about the status of `g.shelf`. As a workaround you could set `g.shelf = None` after the `close`. I would also recommend opening a bug in Python's bug tracker!

Problem

I create a file called `foo_module.py` containing the following code: ``` import shelve, whichdb, os from foo_package.g import g g.shelf = shelve.open("foo_path") g.shelf.close() print whichdb.whichdb("foo_path") # => dbhash os.remove("foo_path") ``` Next to that file I create a directory called `foo_package` than contains an empty `__init__.py` file and a file called `g.py` that just contains: ``` class g: pass ``` Now when I run `foo_module.py` I get a weird error message: `Exception TypeError: "'NoneType' object is not callable" in ignored` But then, if I rename the directory from `foo_package` to `foo`, and change the import line in `foo_module.py`, I don't get any error. Wtf is going on here? Running Python 2.6.4 on WinXP.

Original source