Bug in cPickle with packages and 'object' inherited class

C

Conrado PLG

Say you have this structure:

pna/
__init__.py
model.py


__init__.py is empty.

model.py is:


import cPickle as pickle

class A(object):
pass

def serialize():
pickle.dump(A(), open('temp.dat', 'wb'))


Now open a python interpreter on the root directory of that structure
and type:


import pna.model
pna.model.serialize()


The following exception is thrown:


Traceback (most recent call last):
File "test.py", line 2, in <module>
pna.model.serialize()
File "...\pna\model.py", line 7, in serialize
pickle.dump(A(), open('temp.dat', 'wb'))
cPickle.PicklingError: Can't pickle <class 'pna.model.A'>: import of
module pna.model failed


If you use 'pickle' instead of 'cPickle', it works.
If you don't inherit from 'object' in 'A', it works.

I'm using Python 2.5.1 on Windows XP. I've tested in Python 2.4.4 on
Linux and it works.

Any ideas of what is going on?

Thanks,
Conrado
 
G

Gabriel Genellina

Say you have this structure:

pna/
__init__.py
model.py


__init__.py is empty.

model.py is:


import cPickle as pickle

class A(object):
pass

def serialize():
pickle.dump(A(), open('temp.dat', 'wb'))


Now open a python interpreter on the root directory of that structure
and type:


import pna.model
pna.model.serialize()


The following exception is thrown:


Traceback (most recent call last):
File "test.py", line 2, in <module>
pna.model.serialize()
File "...\pna\model.py", line 7, in serialize
pickle.dump(A(), open('temp.dat', 'wb'))
cPickle.PicklingError: Can't pickle <class 'pna.model.A'>: import of
module pna.model failed

It works for me. What are those "..." in the file name?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,262
Messages
2,571,049
Members
48,769
Latest member
Clifft

Latest Threads

Top