Allan Feldman <[email protected]> added the comment:
I definitely understand the possibility that some code is relying on the
current gc behavior of weakref callbacks being invoked after finalizers.
That being said, the behavior is currently inconsistent between gc and
reference counted paths. The language doesn't have to define the explicit
ordering but the internal inconsistency was definitely unexpected for us.
The result is that behavioral consistency becomes more difficult in application
code when using language provided structures such as WeakValueDictionary.
cache = weakref.WeakValueDictionary()
class Bar:
pass
class Foo:
def __init__(self):
self._self = self
self.value = Bar()
cache[id(self.value)] = self.value
def __del__(self):
# the cache may or may not have self.value at this point
# even though self.value is strongly referenced!
print(list(cache.items()))
>From the weakref docs:
> Entries in the dictionary will be discarded when no strong reference to the
> value exists any more.
But doesn't the code above imply that the entry is discarded even though there
are strong references to the value?
In any case, I definitely appreciate all the eyes on this Tim + Pablo! At the
very least, documentation updates do sound like a good idea if we're moving
forward with leaving the behavior of weakrefs as currently specified. In
particular, it would be worth pointing out that weakrefs callbacks can run even
when the object is referenced.
----------
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue40312>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com