/Lib/test/crashers/nasty_eq_vs_dict.py

http://unladen-swallow.googlecode.com/ · Python · 47 lines · 28 code · 10 blank · 9 comment · 4 complexity · 385c54c572af6d32b1058e577cb8c7c0 MD5 · raw file

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