pickling a circular object inherited from list

Discussion in 'Python' started by Klaus Kopec, Dec 9, 2008.

  1. Klaus Kopec

    Klaus Kopec Guest

    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
    Klaus Kopec, Dec 9, 2008
    #1
    1. Advertising

  2. Klaus Kopec

    Miki Guest

    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,
    --
    Miki <>
    http://pythonwise.blogspot.com
    Miki, Dec 9, 2008
    #2
    1. Advertising

  3. Klaus Kopec

    Klaus Kopec Guest

    >> 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, ...)
    Klaus Kopec, Dec 9, 2008
    #3
  4. Klaus Kopec

    Ned Deily Guest

    In article <ghmchi$rhh$>,
    Klaus Kopec <> wrote:
    > >> 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, ...)


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

    --
    Ned Deily,
    Ned Deily, Dec 9, 2008
    #4
  5. Klaus Kopec

    Klaus Kopec Guest

    Ned Deily wrote:
    > In article <ghmchi$rhh$>,
    > Klaus Kopec <> wrote:
    >>>> 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, ...)

    >
    > 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!
    Klaus Kopec, Dec 10, 2008
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kiuhnm
    Replies:
    16
    Views:
    741
    Jonathan Mcdougall
    Jan 3, 2005
  2. Thomas Guettler

    Pickling object inherited from dict

    Thomas Guettler, Oct 27, 2003, in forum: Python
    Replies:
    1
    Views:
    294
    Thomas Guettler
    Oct 28, 2003
  3. Thomas Guettler

    Pickling Objects inherited from dict (Bug?)

    Thomas Guettler, Nov 5, 2003, in forum: Python
    Replies:
    1
    Views:
    297
    Michael Hudson
    Nov 5, 2003
  4. Alex
    Replies:
    9
    Views:
    373
  5. 7stud --
    Replies:
    11
    Views:
    407
    7stud --
    Nov 9, 2007
Loading...

Share This Page