This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Allow weak referencing of classic classes
Type: Stage:
Components: Interpreter Core Versions: Python 2.5
process
Status: closed Resolution: postponed
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: georg.brandl, glchapman, rhettinger
Priority: normal Keywords: patch

Created on 2005-04-03 15:29 by glchapman, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
classweakref.patch glchapman, 2005-04-03 15:29
Messages (7)
msg48132 - (view) Author: Greg Chapman (glchapman) Date: 2005-04-03 15:29
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.
msg48133 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2005-04-10 16:02
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.
msg48134 - (view) Author: Greg Chapman (glchapman) Date: 2005-04-10 17:05
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.
msg48135 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2005-04-10 18:51
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.
msg48136 - (view) Author: Greg Chapman (glchapman) Date: 2005-04-10 19:47
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?)
msg48137 - (view) Author: Greg Chapman (glchapman) Date: 2005-07-09 14:26
Logged In: YES 
user_id=86307

Well, I'd been meaning to get around to finishing this, but
here it is three months later, and I haven't found (or made)
the time.  So I'm just going to close this as Rejected.
msg48138 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2005-07-09 15:29
Logged In: YES 
user_id=1188172

Changing to Postponed, as the new functionality as such
wasn't rejected.
History
Date User Action Args
2022-04-11 14:56:10adminsetgithub: 41797
2005-04-03 15:29:42glchapmancreate