[issue31254] WeakKeyDictionary/Mapping doesn't call __missing__

Antoine Pitrou report at bugs.python.org
Fri Aug 25 06:13:02 EDT 2017


Antoine Pitrou added the comment:

Antony, if you write (line numbers added for clarity):

1. try:
2.    d[obj]
3. except KeyError:
4.    d[obj] = some new value...
5. # expect d[obj] to exist at this point

it is possible that d[obj] still exists at line 2 but not anymore at line 5 (because there would have been a garbage collection run in-between), at least if the original key is not `obj` but some other object equal to `obj`.  Using setdefault() would ensure that doesn't happen.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue31254>
_______________________________________


More information about the Python-bugs-list mailing list