class_notimpl.py 889 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. # Test that returning of NotImplemented from binary op methods leads to
  2. # TypeError.
  3. try:
  4. NotImplemented
  5. except NameError:
  6. print("SKIP")
  7. raise SystemExit
  8. class C:
  9. def __init__(self, value):
  10. self.value = value
  11. def __str__(self):
  12. return "C(%s)" % self.value
  13. def __add__(self, rhs):
  14. print(self, '+', rhs)
  15. return NotImplemented
  16. def __sub__(self, rhs):
  17. print(self, '-', rhs)
  18. return NotImplemented
  19. def __lt__(self, rhs):
  20. print(self, '<', rhs)
  21. return NotImplemented
  22. def __neg__(self):
  23. print('-', self)
  24. return NotImplemented
  25. c = C(0)
  26. try:
  27. c + 1
  28. except TypeError:
  29. print("TypeError")
  30. try:
  31. c - 2
  32. except TypeError:
  33. print("TypeError")
  34. try:
  35. c < 1
  36. except TypeError:
  37. print("TypeError")
  38. # NotImplemented isn't handled specially in unary methods
  39. print(-c)