[albatross-users] pickle problem
Dave Cole
djc at object-craft.com.au
Mon May 2 14:38:40 EST 2005
Andrew McNamara wrote:
>>ApplicationError: locals "user": Can't pickle <class 'Base.User'>: it's not the same object as Base.User
>>
>>
>>Any idea what the problem is? user of type Base.User is kept in the
>>session. This happens sometimes and sometimes it works ok.
>
>
> You might do better asking on python-list at python.org - this error is
> coming out of cPickle, rather than Albatross (albatross has simply caught
> the error and re-raised it to add more diagnostics).
>
> If I had to guess, I would say that the pickler is unable to identify
> the code object associated with that class. When you pickle an instance
> of a class, only the instance attributes and a reference (the module
> and class name) to the class are saved.
>
> You could try renaming your "User" class or the "Base" module temporarily -
> this will tell you if the pickler is stumbling across the wrong module.
>
> If you're playing tricks with the class definition at run-time, this will
> also confound the pickler, I think (eg, metaclasses), or maybe changing the
> instance's class at run-time.
Another way to make it happen bit me a long time ago.
If you reload a module then try to pickle an object created by the old
module before it was reloaded, then the pickle will fail.
I reported this as a Python bug, but Guido said that he does not
consider it to be a bug.
http://sourceforge.net/tracker/index.php?func=detail&aid=451547&group_id=5470&atid=105470
- Dave
djc at echidna:~$ python
Python 2.3.4 (#2, Jan 5 2005, 08:24:51)
[GCC 3.3.5 (Debian 1:3.3.5-5)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pickle, copy
>>> o = copy._EmptyClass()
>>> pickle.dumps(o, 1)
'(ccopy\n_EmptyClass\nq\x00oq\x01}q\x02b.'
>>> reload(copy)
<module 'copy' from '/usr/lib/python2.3/copy.pyc'>
>>> pickle.dumps(o, 1)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "/usr/lib/python2.3/pickle.py", line 1386, in dumps
Pickler(file, protocol, bin).dump(obj)
File "/usr/lib/python2.3/pickle.py", line 231, in dump
self.save(obj)
File "/usr/lib/python2.3/pickle.py", line 293, in save
f(self, obj) # Call unbound method with explicit self
File "/usr/lib/python2.3/pickle.py", line 721, in save_inst
save(cls)
File "/usr/lib/python2.3/pickle.py", line 293, in save
f(self, obj) # Call unbound method with explicit self
File "/usr/lib/python2.3/pickle.py", line 765, in save_global
raise PicklingError(
pickle.PicklingError: Can't pickle <class copy._EmptyClass at
0x401fb4ac>: it's not the same object as copy._EmptyClass
>>>
--
http://www.object-craft.com.au
More information about the Albatross-users
mailing list