Close #14205: dict lookup raises a RuntimeError if the dict is modified during

a lookup.

"if you want to make a sandbox on top of CPython, you have to fix segfaults"
so let's fix segfaults!
This commit is contained in:
Victor Stinner 2012-03-06 01:03:13 +01:00
parent b2c7cca0cf
commit 198b291df7
5 changed files with 31 additions and 352 deletions

View file

@ -379,7 +379,7 @@ class DictTest(unittest.TestCase):
x.fail = True
self.assertRaises(Exc, d.pop, x)
def test_mutatingiteration(self):
def test_mutating_iteration(self):
# changing dict size during iteration
d = {}
d[1] = 1
@ -387,6 +387,26 @@ class DictTest(unittest.TestCase):
for i in d:
d[i+1] = 1
def test_mutating_lookup(self):
# changing dict during a lookup
class NastyKey:
mutate_dict = None
def __hash__(self):
# hash collision!
return 1
def __eq__(self, other):
if self.mutate_dict:
self.mutate_dict[self] = 1
return self == other
d = {}
d[NastyKey()] = 0
NastyKey.mutate_dict = d
with self.assertRaises(RuntimeError):
d[NastyKey()] = None
def test_repr(self):
d = {}
self.assertEqual(repr(d), '{}')