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 <rep...@bugs.python.org>
<http://bugs.python.org/issue17816>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to