[Python-Dev] RE: [Python-checkins] python/dist/src/Lib weakref.py,1.19,1.20
Raymond Hettinger
python@rcn.com
Sun, 25 May 2003 02:05:28 -0400
[Raymondo]
> > The original code does its contortions to avoid raising a KeyError
> > whenever the dictionary entry might have disappeared due to the
> > ref count falling to zero and then a new, equal key was formed later.
[Timbot]
> Sorry, I can't picture what you're trying to say. Show some code?
Python 2.3b1 (#40, Apr 25 2003, 19:06:24) [MSC v.1200 32 bit (Intel)] on win32
Type "copyright", "credits" or "license" for more information.
IDLE 0.8 -- press F1 for help
>>> class C: pass
>>> import weakref
>>> wkd = weakref.WeakKeyDictionary()
>>> del wkd[C()]
>>> # No complaints
Python 2.3b1+ (#40, May 23 2003, 00:08:36) [MSC v.1200 32 b
Type "help", "copyright", "credits" or "license" for more i
>>> class C: pass
...
>>> import weakref
>>> wkd = weakref.WeakKeyDictionary()
>>> del wkd[C()]
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "C:\PY23\lib\weakref.py", line 167, in __delitem__
del self.data[ref(key)]
KeyError: <weakref at 006BC600; to 'instance' at 006BAA30>
>>> # Complains now.
[Raymond]
> > If the data disappeared, then, I think ref(key) will return None
[Timbot]
> No, ref(x) never returns None, regardless of what x may be. It may raise
> TypeError if x is not of a weakly referencable type, and it may raise
> MemoryError if we don't have enough memory left to construct a weakref, but
> those are the only things that can go wrong.
[Current version of the docs]
"""
ref( object[, callback])
Return a weak reference to object. The original object can be retrieved by calling the reference object if the referent is still
alive; if the referent is no longer alive, calling the reference object will cause None to be returned.
"""
Raymond Hettinger