[Patches] [ python-Patches-1175850 ] Allow weak referencing of classic classes

SourceForge.net noreply at sourceforge.net
Sun Apr 10 21:47:57 CEST 2005


Patches item #1175850, was opened at 2005-04-03 07:29
Message generated for change (Comment added) made by glchapman
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=1175850&group_id=5470

Category: Core (C code)
Group: Python 2.5
Status: Open
Resolution: None
Priority: 5
Submitted By: Greg Chapman (glchapman)
Assigned to: Raymond Hettinger (rhettinger)
Summary: Allow weak referencing of classic classes

Initial Comment:
In Python 2.4, you can weakref instances of classic
classes but not the classes themselves.  Attached is a
patch which adds weakref support to classic classes.

----------------------------------------------------------------------

>Comment By: Greg Chapman (glchapman)
Date: 2005-04-10 11:47

Message:
Logged In: YES 
user_id=86307

Ah, I see, not so easy to do it properly.  Fair enough.  One
quick question though: Py_TPFLAGS_HAVE_WEAKREFS is included
in Py_TPFLAGS_DEFAULT.  I didn't include it explicitly
because of that and because neither of the other two types
in classobject.c explicitly uses it (though I see other
types do explicitly use it).  So are PyInstance_Type and
PyMethod_Type wrong?  (Should I include a change to their
definitions?)

----------------------------------------------------------------------

Comment By: Raymond Hettinger (rhettinger)
Date: 2005-04-10 10:51

Message:
Logged In: YES 
user_id=80475

Be sure to add Py_TPFLAGS_HAVE_WEAKREFS to tp_flags.

Beef-up the unittest.  See test_weakref() in
test.test_deque.py for an example.

Add classic classes to the list of weak referencables in
libweakref.tex.  Include a \versionadded tag.

Add an entry to Misc/NEWS.


----------------------------------------------------------------------

Comment By: Greg Chapman (glchapman)
Date: 2005-04-10 09:05

Message:
Logged In: YES 
user_id=86307

Here's my use case.  I've been experimenting with the
multimethods module written by David Mertz (part of his
gnosis utilities).  This uses mros from a declared signature
and from the actual parameter types to determine which
overload to call.  Classic classes don't have an mro, so I
needed to calculate one to accomodate them.  Having done so,
I wanted to cache it.  The best solution might be the one
I'm using now, which is simply to assign the calculated mro
to a __mro__ attribute of the classic class.  However, it
seemed better to me to use an external cache, rather than
modifying the class.  I wanted this external cache to be a
WeakKeyDictionary, because, if a module containing a classic
class is reloaded, I didn't want the old version of the
class kept alive solely by my cache (it avoided a reference
cycle with the mro itself by storing a list with the first
element set to None; then, when fetched,  copying the list
and putting the class in the copy's first element).

Anyway, feel free to reject this.  It was so easy to do, I
just thought I'd go ahead and post it in case the lack was
simply an oversight.


----------------------------------------------------------------------

Comment By: Raymond Hettinger (rhettinger)
Date: 2005-04-10 08:02

Message:
Logged In: YES 
user_id=80475

I'm curious why you would ever want to do this.  It is
somewhat rare to delete a class reference and, I presume,
rarer still to need a weak-reference to such a class.

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=1175850&group_id=5470


More information about the Patches mailing list