[Python-Dev] Re: pickle me, Elmo? (weakref feature request)
Jeremy Hylton
jeremy@zope.com
10 Feb 2003 14:59:41 -0500
On Mon, 2003-02-10 at 14:48, Guido van Rossum wrote:
> I brainstormed for a few minutes with Fred Drake, the weakref expert,
> but we saw many obstacles to implementing it. It would be A Major
> Project to implement this: it would require adding new methods to
> weakref objects, and new pickling codes, and an extension to the
> pickling and unpickling engines...
>
I suspect that something like this feature could be implemented using
persistent_id() and persistent_load(). The persistent_id() could do
something special for weak references, returning a persistent id that is
the id() of the referenced object. The persistent_load() function could
check for one in the memo and create a new weakref if it exists;
otherwise, it would return None.
The one hitch is that the only reference to an object might occur after
the weakref. The unpickler wouldn't know that the object occurred
later. Not sure how to finesse this problem; it seems to require a
second pass over the pickle stream at some point, which is a major
rewrite. Perhaps the persistent_load() always returns a proxy object
that isn't a weakref, but has the same interface. When the unpickler is
done, it gives all these objects a chance to check the memo for the real
objects.
Anyway, I think you could come up with something without too much
effort.
Jeremy