python3: help with pickle

Alan alanwilter at gmail.com
Thu Jul 15 06:34:00 EDT 2010


Hi there,

This is more an exercise to myself to understand python3. I took a code I
wrote (acpype) and I am trying to make it compatible with either python 3 or
2.

I am trying to make a pickle file compatible with either python 3 and 2 as I
believe it should be possible.

I've looked at http://docs.python.org/py3k/library/pickle.html but still,
when I create a pickle file for an object with python 2.7 and then I try to
load it in python3, I got this:

Python 3.1.2 (r312:79147, Jul  7 2010, 10:55:24)
[GCC 4.2.1 (Apple Inc. build 5659)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from acpype import * # this acpype package contains needed class ACTopol
>>> import pickle
>>> o = pickle.load(open('AAA.acpype/AAA.pkl','rb'))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/sw/lib/python3.1/pickle.py", line 1365, in load
    encoding=encoding, errors=errors).load()
TypeError: ('__init__() takes at least 2 positional arguments (1 given)',
<class 'acpype.ACTopol'>, ())
>>>

Now trying the contrary, pickle file is created with python3, I got this
when trying to load it with python 2.7:

Python 2.7 (r27:82500, Jul  7 2010, 10:48:15)
[GCC 4.2.1 (Apple Inc. build 5659)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from acpype import *
>>> import pickle
>>> o = pickle.load(open('AAA.pkl','rb'))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/sw/lib/python2.7/pickle.py", line 1378, in load
    return Unpickler(file).load()
  File "/sw/lib/python2.7/pickle.py", line 858, in load
    dispatch[key](self)
  File "/sw/lib/python2.7/pickle.py", line 1083, in load_newobj
    obj = cls.__new__(cls, *args)
AttributeError: class ACTopol has no attribute '__new__'

Apart that, everything else seems to work. Just one note more: when loading
the pickle file 2.7 in python 2.7, type(o) is <type 'instance'>, while
pickle 3 in python 3, type(o) is <class 'acpype.ACTopol'>

Many thanks in advance,

Alan

-- 
Alan Wilter S. da Silva, D.Sc. - CCPN Research Associate
Department of Biochemistry, University of Cambridge.
80 Tennis Court Road, Cambridge CB2 1GA, UK.
>>http://www.bio.cam.ac.uk/~awd28<<
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100715/e4f2fdcf/attachment.html>


More information about the Python-list mailing list