Pickle of the instance of new style class fails when execute with exec statement

Борис Казаков boris.kazakov at gmail.com
Tue May 26 15:24:42 EDT 2009


Hi, I'm trying to execute the following code:

d = {}
exec('import cPickle')  in d
desc = """
class A(object):
    pass
"""
exec(desc) in d

exec('a = A()') in d
exec('cPickle.dump(a, open("tmp.txt","wb"))')in d

for some reason it fails with the following traceback:

Traceback (most recent call last):
  File "pickleTest.py", line 10, in <module>
    exec('cPickle.dump(a, open("tmp.txt","wb"))')in d
  File "<string>", line 1, in <module>
cPickle.PicklingError: Can't pickle <class 'A'>: attribute lookup
__builtin__.A failed

pickle also dosen't work, changing protocol dosen't seem to help.

it works when i do:

exec('import __builtin__') in d
exec('__builtin__.A = A') in d

but it pollutes builtin namespace, which I don't like.

May be there is a workaround, how to do this correctly?
Thanks in advance,
Boris.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090526/2771ac7d/attachment.html>


More information about the Python-list mailing list