This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author Nils.Bruin
Recipients Nils.Bruin
Date 2013-04-22.16:41:03
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1366648864.21.0.829843721965.issue17816@psf.upfronthosting.co.za>
In-reply-to
Content
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.
History
Date User Action Args
2013-04-22 16:41:04Nils.Bruinsetrecipients: + Nils.Bruin
2013-04-22 16:41:04Nils.Bruinsetmessageid: <1366648864.21.0.829843721965.issue17816@psf.upfronthosting.co.za>
2013-04-22 16:41:04Nils.Bruinlinkissue17816 messages
2013-04-22 16:41:03Nils.Bruincreate