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 <>
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):
        def __repr__(self):
            return "A(%d)"%self.n

    def mess(n):
        L=[A(i) for i in range(n)]
        for i in range(n-1):
        return D


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.
Date User Action Args
2013-04-22 16:41:04Nils.Bruinsetrecipients: + Nils.Bruin
2013-04-22 16:41:04Nils.Bruinsetmessageid: <>
2013-04-22 16:41:04Nils.Bruinlinkissue17816 messages
2013-04-22 16:41:03Nils.Bruincreate