[Patches] [Patch #103203] PEP 205: weak references implementation
noreply@sourceforge.net
noreply@sourceforge.net
Tue, 16 Jan 2001 21:35:07 -0800
Patch #103203 has been updated.
Project: python
Category: core (C code)
Status: Open
Submitted by: fdrake
Assigned to : nascheme
Summary: PEP 205: weak references implementation
Follow-Ups:
Date: 2001-Jan-16 21:35
By: fdrake
Comment:
Added a new (but broken!) version of the patch that fixes
WeakDictionary.get() but doesn't interact properly with GC (appearantly) --
not usable as is:
http://starship.python.net/crew/fdrake/patches/weakref.patch-3
Neil made the mistake of volunteering to look at the current problem, so
assigning to him. (Thanks!)
-------------------------------------------------------
Date: 2001-Jan-16 19:39
By: tim_one
Comment:
Just mentioning a general scenario in which weak keys are the bee's knees:
sometimes you want to add attributes to objects dynamically, in ways the
original class author(s -- may cut across many classes! even non-instance
objects; or objects from extension modules that don't support setattr)
never anticipated. Like maybe a German translation of the object's
docstrings; or a list of JPEG contents referenced by a URLish object; or a
table of usage statistics on methods and functions; etc. Then it's natural
to use the conceptual attr name as the name of a dict instead, and write
attr[object] = whatever
instead of
object.attr = whatever
There's no point keeping the "whatever" around when the object goes away,
but a regular dict makes the object and its "whatever" immortal. A dict
with weak keys is exactly on-target.
-------------------------------------------------------
Date: 2001-Jan-16 19:14
By: fdrake
Comment:
I'm not entirely sure why one would want weak keys rather than weak values,
but I suspect it has to do with expectations of how the dict will be used
in applications. I don't think it would be hard to support both flavors of
dictionaries using the primitive reference objects.
The constructor does not check that all incoming values are weak because
that isn't required. It uses self.update() rather than self.data.update()
to perform the inclusion, so any mapping that supports .keys() and
.__getitem__() is sufficient. Some additional checking could be done to
make sure that all added entries can be created before inserting any of
them, offering more atomicity.
.get() will be fixed in the next version of the patch; thanks!
I've added GC support, but don't have it working quite right. I'm trying
to isolate the failure now.
-------------------------------------------------------
Date: 2001-Jan-12 14:36
By: loewis
Comment:
The weak dictionary data type needs more clarification in documentation and
implementation. In java.util.WeakHashMap, they keys are weak, not the
values (i.e. once the key is forgotten, the entry is cleared). In
http://www.handshake.de/~dieter/weakdict.html, the values are weak, but it
raises KeyErrors for entries that are collected.
The initializer of a weak dict does not check the invariant (all values are
weak).
get() of a weakdict does not support an optional second parameter.
weakref and friends need to participate in garbage collection, as the
reference to the callback can create a cycle.
-------------------------------------------------------
Date: 2001-Jan-12 11:27
By: fdrake
Comment:
Updated patch to also support proxy semantics using the same invalidation
machinery as the basic weak reference type. The additional code is
*heavily* borrowed from Neil Schemenauer.
-------------------------------------------------------
Date: 2001-Jan-12 01:00
By: nobody
Comment:
Neil,
you may want to have a look at mxProxy. This is a proxy implementation
which already provides weak references (among other things) and has
the advantages you talked about in your reply.
--
Marc-Andre
-------------------------------------------------------
Date: 2001-Jan-11 15:27
By: nascheme
Comment:
Hmm, I think this can be done more cleanly with a proxy now the
the coercion stuff has been cleaned up. I will probably give it a go
tonight. The advantage of using a proxy is that you can have a weak
reference to any object and you only pay memory for weak references
if you use them.
-------------------------------------------------------
Date: 2001-Jan-11 14:05
By: fdrake
Comment:
Assigned to Martin, since he's indicated he intends to go over this very
carefully!
-------------------------------------------------------
-------------------------------------------------------
For more info, visit:
http://sourceforge.net/patch/?func=detailpatch&patch_id=103203&group_id=5470