python / cpython

The Python programming language
https://www.python.org
Other
63.43k stars 30.37k forks source link

Allow weak referencing of classic classes #41797

Closed 26af53de-f307-4809-a4a7-9d50203b0a97 closed 19 years ago

26af53de-f307-4809-a4a7-9d50203b0a97 commented 19 years ago
BPO 1175850
Nosy @birkenfeld, @rhettinger
Files
  • classweakref.patch
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields: ```python assignee = 'https://github.com/rhettinger' closed_at = created_at = labels = ['interpreter-core'] title = 'Allow weak referencing of classic classes' updated_at = user = 'https://bugs.python.org/glchapman' ``` bugs.python.org fields: ```python activity = actor = 'georg.brandl' assignee = 'rhettinger' closed = True closed_date = None closer = None components = ['Interpreter Core'] creation = creator = 'glchapman' dependencies = [] files = ['6587'] hgrepos = [] issue_num = 1175850 keywords = ['patch'] message_count = 7.0 messages = ['48132', '48133', '48134', '48135', '48136', '48137', '48138'] nosy_count = 3.0 nosy_names = ['georg.brandl', 'rhettinger', 'glchapman'] pr_nums = [] priority = 'normal' resolution = 'postponed' stage = None status = 'closed' superseder = None type = None url = 'https://bugs.python.org/issue1175850' versions = ['Python 2.5'] ```

    26af53de-f307-4809-a4a7-9d50203b0a97 commented 19 years ago

    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.

    rhettinger commented 19 years ago

    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.

    26af53de-f307-4809-a4a7-9d50203b0a97 commented 19 years ago

    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.

    rhettinger commented 19 years ago

    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.

    26af53de-f307-4809-a4a7-9d50203b0a97 commented 19 years ago

    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?)

    26af53de-f307-4809-a4a7-9d50203b0a97 commented 19 years ago

    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.

    birkenfeld commented 19 years ago

    Logged In: YES user_id=1188172

    Changing to Postponed, as the new functionality as such wasn't rejected.