Debian Codegraph @

cpython27/Lib/test/crashers/nasty_eq_vs_dict.py

raw symbols SCIP
1 # from http://mail.python.org/pipermail/python-dev/2001-June/015239.html
2
3 # if you keep changing a dictionary while looking up a key, you can
4 # provoke an infinite recursion in C
5
6 # At the time neither Tim nor Michael could be bothered to think of a
7 # way to fix it.
8
9 class Yuck:
10 def __init__(self):
11 self.i = 0
12
13 def make_dangerous(self):
14 self.i = 1
15
16 def __hash__(self):
17 # direct to slot 4 in table of size 8; slot 12 when size 16
18 return 4 + 8
19
20 def __eq__(self, other):
21 if self.i == 0:
22 # leave dict alone
23 pass
24 elif self.i == 1:
25 # fiddle to 16 slots
26 self.__fill_dict(6)
27 self.i = 2
28 else:
29 # fiddle to 8 slots
30 self.__fill_dict(4)
31 self.i = 1
32
33 return 1
34
35 def __fill_dict(self, n):
36 self.i = 0
37 dict.clear()
38 for i in range(n):
39 dict[i] = i
40 dict[self] = "OK!"
41
42 y = Yuck()
43 dict = {y: "OK!"}
44
45 z = Yuck()
46 y.make_dangerous()
47 print dict[z]