Question

Seeing an odd exception in the shelve module (well the shelve helper copy_reg module). Taking a look, it is trying to call a reference to what should be the __getstate__ method of the Pickler class. However, it would seem that this has returned None for some reason. Just wondered if anyone else has experienced this and if there is something that can be done to get shelve to work correctly?

Here is the return stack for the exception I am seeing:

  File "/usr/local/lib/python2.7/dist-packages/libgsync/drive/__init__.py", line 497, in stat
    self._pcache[search] = ent
  File "/usr/lib/python2.7/shelve.py", line 132, in __setitem__
    p.dump(value)
  File "/usr/lib/python2.7/copy_reg.py", line 84, in _reduce_ex
    dict = getstate()
DEBUG:      libgsync/drive/__init__.py:387:walk(): Exception: 'NoneType' object is not callable

I took the liberty of looking at the code and here is where it is malfunctioning, in the _reduce_ex() function:

try:
    getstate = self.__getstate__
except AttributeError:
    if getattr(self, "__slots__", None):
        raise TypeError("a class that defines __slots__ without "
                        "defining __getstate__ cannot be pickled")
    try:
        dict = self.__dict__
    except AttributeError:
        dict = None
else:
    dict = getstate()

Initially, it assigns self.__getstate__ to getstat, so this should be callable at this point. It doesn't appear to have raised an exception, since it is executing with the context of the else block. Strange...

Here is the debug output from the line of code where the exception is occurring:

DEBUG:        libgsync/drive/__init__.py:496:stat(): Updating path cache: /unittest

And here is the code causing the exception:

# Update path cache.
if self._pcache.get(search) is None:
    debug("Updating path cache: %s" % search)
    self._pcache[search] = ent
Was it helpful?

Solution

The value being assigned to the shelf dictionary was not a copyable object. I resolved the issue by deferring the class wrapping of the dictionary until after it had been cached.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top