python / cpython

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

new-style objects not weak-referencable #34984

Closed freddrake closed 22 years ago

freddrake commented 22 years ago
BPO 451773
Nosy @gvanrossum, @freddrake

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/gvanrossum' closed_at = created_at = labels = ['interpreter-core'] title = 'new-style objects not weak-referencable' updated_at = user = 'https://github.com/freddrake' ``` bugs.python.org fields: ```python activity = actor = 'gvanrossum' assignee = 'gvanrossum' closed = True closed_date = None closer = None components = ['Interpreter Core'] creation = creator = 'fdrake' dependencies = [] files = [] hgrepos = [] issue_num = 451773 keywords = [] message_count = 4.0 messages = ['6005', '6006', '6007', '6008'] nosy_count = 2.0 nosy_names = ['gvanrossum', 'fdrake'] pr_nums = [] priority = 'normal' resolution = 'fixed' stage = None status = 'closed' superseder = None type = None url = 'https://bugs.python.org/issue451773' versions = [] ```

freddrake commented 22 years ago

Objects using the new-style class types are not weakly referencable:

>>> import weakref
>>> class MyClass(object):
...    pass
...
>>> o = MyClass()
>>> weakref.ref(o)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: 'MyClass' objects are not weakly referencable
gvanrossum commented 22 years ago

Logged In: YES user_id=6380

This is a little tricker than I thought.

I don't want to enable weak-referencing for the 'object' type, because that would add a weak reference slot to every type (even list, dictionary etc.).

So I have to add a weakref slot at the same time that I add the __dict. But then types using __slots are not weak-referenceable.

Ideally, weak-referenceability might be a user option, default on for types with a __dict but default of for types that use __slots only.

Fred, would it be a safety issue if the list of weak references were made available as a __weakrefs__ special slot (if present)?

freddrake commented 22 years ago

Logged In: YES user_id=3066

Would it be possible to make weak-referencability a completely separate mixin class? I don't know enough about the mechanics of the new material to understand the issues.

I don't think there are any problems with exposing an __weakrefs__ attribute; the value should be the same as returned by weakref.getweakrefs(). It should be a new list on every reference.

Perhaps you can explain the issues here on Monday.

gvanrossum commented 22 years ago

Logged In: YES user_id=6380

I've found a way to add it. Read the checkin message.

Question: why is the field called 'tp_weaklistoffset'? It doesn't seem to be a list.