[New-bugs-announce] [issue17816] Weak*Dictionary KeyErrors during callbacks

Nils Bruin report at bugs.python.org
Mon Apr 22 18:41:04 CEST 2013


New submission from Nils Bruin:

The following program is a little dependent on memory layout but will usually generate lots of

    Exception KeyError: (A(9996),) in <function remove at 0xa47050> ignored

messages in Python 2.7.

    import weakref

    class A(object):
        def __init__(self,n):
            self.n=n
        def __repr__(self):
            return "A(%d)"%self.n

    def mess(n):
        D=weakref.WeakValueDictionary()
        L=[A(i) for i in range(n)]
        for i in range(n-1):
            j=(i+10)%n
            D[L[i]]=L[j]
        return D

    D=mess(10000)
    D.clear()

The reason is that on D.clear() all entries are removed from D before actually deleting those entries. Once the entries are deleted one-by-one, sometimes the removal of a key will result in deallocation of that key, which may be a not-yet-deleted ex-value of the dictionary as well. The callback triggers on the weakref, but the dict itself was already emptied, so nothing is found.

I've checked and on Python 3.2.3 this problem does not seem to occur. I haven't checked the Python source to see how Python 3 behaves differently and whether that behaviour would be easy to backport to fix this bug in 2.7.

----------
components: Library (Lib)
messages: 187570
nosy: Nils.Bruin
priority: normal
severity: normal
status: open
title: Weak*Dictionary KeyErrors during callbacks
versions: Python 2.7

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


More information about the New-bugs-announce mailing list