summaryrefslogtreecommitdiff
path: root/src/python/stdlib/test/crashers/nasty_eq_vs_dict.py
diff options
context:
space:
mode:
authorSimon <simon@hardwired.org.uk>2011-03-22 17:58:52 (GMT)
committer Simon <simon@hardwired.org.uk>2011-03-22 17:58:52 (GMT)
commit2e401babb793238564ca640fc802a52ab7f6c293 (patch)
tree87a9b471e82604e76f96d556f5771322fb31818a /src/python/stdlib/test/crashers/nasty_eq_vs_dict.py
parentc096b2b14a200a0cc0a08cfea839c9e7f4edf22e (diff)
parent04a9cbcb8855e64db660a8c6e23d79114b4afd83 (diff)
downloadpowder-2e401babb793238564ca640fc802a52ab7f6c293.zip
powder-2e401babb793238564ca640fc802a52ab7f6c293.tar.gz
Python console
Diffstat (limited to 'src/python/stdlib/test/crashers/nasty_eq_vs_dict.py')
-rw-r--r--src/python/stdlib/test/crashers/nasty_eq_vs_dict.py47
1 files changed, 47 insertions, 0 deletions
diff --git a/src/python/stdlib/test/crashers/nasty_eq_vs_dict.py b/src/python/stdlib/test/crashers/nasty_eq_vs_dict.py
new file mode 100644
index 0000000..3f3083d
--- /dev/null
+++ b/src/python/stdlib/test/crashers/nasty_eq_vs_dict.py
@@ -0,0 +1,47 @@
+# from http://mail.python.org/pipermail/python-dev/2001-June/015239.html
+
+# if you keep changing a dictionary while looking up a key, you can
+# provoke an infinite recursion in C
+
+# At the time neither Tim nor Michael could be bothered to think of a
+# way to fix it.
+
+class Yuck:
+ def __init__(self):
+ self.i = 0
+
+ def make_dangerous(self):
+ self.i = 1
+
+ def __hash__(self):
+ # direct to slot 4 in table of size 8; slot 12 when size 16
+ return 4 + 8
+
+ def __eq__(self, other):
+ if self.i == 0:
+ # leave dict alone
+ pass
+ elif self.i == 1:
+ # fiddle to 16 slots
+ self.__fill_dict(6)
+ self.i = 2
+ else:
+ # fiddle to 8 slots
+ self.__fill_dict(4)
+ self.i = 1
+
+ return 1
+
+ def __fill_dict(self, n):
+ self.i = 0
+ dict.clear()
+ for i in range(n):
+ dict[i] = i
+ dict[self] = "OK!"
+
+y = Yuck()
+dict = {y: "OK!"}
+
+z = Yuck()
+y.make_dangerous()
+print dict[z]