pickling a circular object inherited from list

K

Klaus Kopec

Hello everyone,

I have a problem with inheritance from list. I want to create a tree
like object where child nodes are kept in self[:] and every child has a
field that points to its parent. Pickling such an object, however,
throws an AssertionError. See below for source code and output of an
easy example case of my problem.

What did I do wrong?

Best regards,
Klaus

======== source:
class myList(list):
pass

class myObject:
def __init__(self, parent=None):
self.parent = parent

if __name__ == '__main__':
r = myList()
r.append( myObject(r) )

from pickle import dump, load
dump(r, file('/tmp/x', 'w'))

========= output:
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "/tmp/python-11053-o.py", line 15, in ?
dump(r, f)
File "/usr/lib/python2.4/pickle.py", line 1382, in dump
Pickler(file, protocol, bin).dump(obj)
File "/usr/lib/python2.4/pickle.py", line 231, in dump
self.save(obj)
File "/usr/lib/python2.4/pickle.py", line 338, in save
self.save_reduce(obj=obj, *rv)
File "/usr/lib/python2.4/pickle.py", line 419, in save_reduce
self.memoize(obj)
File "/usr/lib/python2.4/pickle.py", line 251, in memoize
assert id(obj) not in self.memo
AssertionError
 
M

Miki

Hello Klaus,
I have a problem with inheritance from list. I want to create a tree
like object where child nodes are kept in self[:] and every child has a
field that points to its parent. Pickling such an object, however,
throws an AssertionError. See below for source code and output of an
easy example case of my problem.

What did I do wrong?
Old Python version? :)
Seems to work in 3.0 (don't have 2.6 currently to check but IMO it's
fixed there as well).

HTH,
 
K

Klaus Kopec

What did I do wrong?
Old Python version? :)
Seems to work in 3.0 (don't have 2.6 currently to check but IMO it's
fixed there as well).
It works for me with v3.0 as well, but not with v2.6.1 (same error as
stated before for v2.4).

Is there any way to fix this in v2.6.1 or even v2.4? Right now I cannot
switch to v3.0 because I depend on several not compatible packages
(numpy, biopython, ...)
 
N

Ned Deily

It works for me with v3.0 as well, but not with v2.6.1 (same error as
stated before for v2.4).

Is there any way to fix this in v2.6.1 or even v2.4? Right now I cannot
switch to v3.0 because I depend on several not compatible packages
(numpy, biopython, ...)

It looks like your example can be made to work by either specifying
pickle protocol 2 or by switching to cPickle.
 
K

Klaus Kopec

Ned said:
It looks like your example can be made to work by either specifying
pickle protocol 2 or by switching to cPickle.

Using pickle protocol 2 solved the problem. Thank you all for helping me
out!
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top