Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This is a greatly simplified example of what pickle didn't like about my data structure.</p> <pre><code>import cPickle as pickle class Member(object): def __init__(self, key): self.key = key self.pool = None def __hash__(self): return self.key class Pool(object): def __init__(self): self.members = set() def add_member(self, member): self.members.add(member) member.pool = self member = Member(1) pool = Pool() pool.add_member(member) with open("test.pkl", "w") as f: pickle.dump(member, f, pickle.HIGHEST_PROTOCOL) with open("test.pkl", "r") as f: x = pickle.load(f) </code></pre> <p>Pickle is known to be a little funny with circular structures, but if you toss custom hash functions and sets/dicts into the mix then things get quite hairy.</p> <p>In this particular example it partially unpickles the member and then encounters the pool. So it then partially unpickles the pool and encounters the members set. So it creates the set and tries to add the partially unpickled member to the set. At which point it dies in the custom hash function, because the member is only partially unpickled. I dread to think what might happen if you had an "if hasattr..." in the hash function.</p> <pre><code>$ python --version Python 2.6.5 $ python test.py Traceback (most recent call last): File "test.py", line 25, in &lt;module&gt; x = pickle.load(f) File "test.py", line 8, in __hash__ return self.key AttributeError: ("'Member' object has no attribute 'key'", &lt;type 'set'&gt;, ([&lt;__main__.Member object at 0xb76cdaac&gt;],)) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload