Message187570
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. |
|
Date |
User |
Action |
Args |
2013-04-22 16:41:04 | Nils.Bruin | set | recipients:
+ Nils.Bruin |
2013-04-22 16:41:04 | Nils.Bruin | set | messageid: <1366648864.21.0.829843721965.issue17816@psf.upfronthosting.co.za> |
2013-04-22 16:41:04 | Nils.Bruin | link | issue17816 messages |
2013-04-22 16:41:03 | Nils.Bruin | create | |
|