# HG changeset patch # User Philip Jenvey # Date 1396901856 25200 # Mon Apr 07 13:17:36 2014 -0700 # Node ID b1cc800ba1defdd424e779100f5a5973c51aec2f # Parent 34ace7eb67e9de12d90fe84022d3e0d063488f84 issue21173: trigger WeakKeyDictionary.__len__ raising a ValueError diff -r 34ace7eb67e9 -r b1cc800ba1de Lib/test/test_weakref.py --- a/Lib/test/test_weakref.py Mon Apr 07 11:20:22 2014 +0200 +++ b/Lib/test/test_weakref.py Mon Apr 07 13:17:36 2014 -0700 @@ -1319,6 +1319,22 @@ it = None # should commit all removals self.check_weak_destroy_and_mutate_while_iterating(dict, testcontext) + def test_weak_keys_len_destroy_while_iterating(self): + it = None + dict, objects = self.make_weak_keyed_dict() + @contextlib.contextmanager + def testcontext(): + nonlocal it + it = iter(dict.items()) + next(it) + # Schedule a key/value for removal and recreate it + v = objects.pop().arg + gc.collect() # just in case + yield Object(v), v + with testcontext() as (k, v): + dict.clear() + self.assertEqual(len(dict), 0) + def test_weak_values_destroy_while_iterating(self): # Issue #7105: iterators shouldn't crash when a key is implicitly removed dict, objects = self.make_weak_valued_dict()