PageRenderTime 51ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/pypy/objspace/std/boolobject.py

https://bitbucket.org/pypy/pypy/
Python | 110 lines | 82 code | 25 blank | 3 comment | 5 complexity | 298881765cb53aa3a1e62300e6d47f83 MD5 | raw file
Possible License(s): AGPL-3.0, BSD-3-Clause, Apache-2.0
  1. """The builtin bool implementation"""
  2. import operator
  3. from rpython.rlib.rarithmetic import r_uint
  4. from rpython.tool.sourcetools import func_renamer, func_with_new_name
  5. from pypy.interpreter.gateway import WrappedDefault, interp2app, unwrap_spec
  6. from pypy.interpreter.typedef import TypeDef
  7. from pypy.objspace.std.intobject import W_AbstractIntObject, W_IntObject
  8. class W_BoolObject(W_IntObject):
  9. def __init__(self, boolval):
  10. self.intval = int(not not boolval)
  11. def __nonzero__(self):
  12. raise Exception("you cannot do that, you must use space.is_true()")
  13. def __repr__(self):
  14. """representation for debugging purposes"""
  15. return "%s(%s)" % (self.__class__.__name__, bool(self.intval))
  16. def is_w(self, space, w_other):
  17. return self is w_other
  18. def immutable_unique_id(self, space):
  19. return None
  20. def unwrap(self, space):
  21. return bool(self.intval)
  22. def uint_w(self, space):
  23. return r_uint(self.intval)
  24. def int(self, space):
  25. return space.newint(self.intval)
  26. @staticmethod
  27. @unwrap_spec(w_obj=WrappedDefault(False))
  28. def descr_new(space, w_booltype, w_obj):
  29. """T.__new__(S, ...) -> a new object with type S, a subtype of T"""
  30. space.w_bool.check_user_subclass(w_booltype)
  31. return space.newbool(space.is_true(w_obj))
  32. def descr_repr(self, space):
  33. return space.wrap('True' if self.intval else 'False')
  34. descr_str = func_with_new_name(descr_repr, 'descr_str')
  35. def descr_nonzero(self, space):
  36. return self
  37. def _make_bitwise_binop(opname):
  38. descr_name = 'descr_' + opname
  39. int_op = getattr(W_IntObject, descr_name)
  40. op = getattr(operator,
  41. opname + '_' if opname in ('and', 'or') else opname)
  42. @func_renamer(descr_name)
  43. def descr_binop(self, space, w_other):
  44. if not isinstance(w_other, W_BoolObject):
  45. return int_op(self, space, w_other)
  46. a = bool(self.intval)
  47. b = bool(w_other.intval)
  48. return space.newbool(op(a, b))
  49. @func_renamer('descr_r' + opname)
  50. def descr_rbinop(self, space, w_other):
  51. return descr_binop(self, space, w_other)
  52. return descr_binop, descr_rbinop
  53. descr_and, descr_rand = _make_bitwise_binop('and')
  54. descr_or, descr_ror = _make_bitwise_binop('or')
  55. descr_xor, descr_rxor = _make_bitwise_binop('xor')
  56. W_BoolObject.w_False = W_BoolObject(False)
  57. W_BoolObject.w_True = W_BoolObject(True)
  58. W_BoolObject.typedef = TypeDef("bool", W_IntObject.typedef,
  59. __doc__ = """bool(x) -> bool
  60. Returns True when the argument x is true, False otherwise.
  61. The builtins True and False are the only two instances of the class bool.
  62. The class bool is a subclass of the class int, and cannot be subclassed.""",
  63. __new__ = interp2app(W_BoolObject.descr_new),
  64. __repr__ = interp2app(W_BoolObject.descr_repr,
  65. doc=W_AbstractIntObject.descr_repr.__doc__),
  66. __str__ = interp2app(W_BoolObject.descr_str,
  67. doc=W_AbstractIntObject.descr_str.__doc__),
  68. __nonzero__ = interp2app(W_BoolObject.descr_nonzero,
  69. doc=W_AbstractIntObject.descr_nonzero.__doc__),
  70. __and__ = interp2app(W_BoolObject.descr_and,
  71. doc=W_AbstractIntObject.descr_and.__doc__),
  72. __rand__ = interp2app(W_BoolObject.descr_rand,
  73. doc=W_AbstractIntObject.descr_rand.__doc__),
  74. __or__ = interp2app(W_BoolObject.descr_or,
  75. doc=W_AbstractIntObject.descr_or.__doc__),
  76. __ror__ = interp2app(W_BoolObject.descr_ror,
  77. doc=W_AbstractIntObject.descr_ror.__doc__),
  78. __xor__ = interp2app(W_BoolObject.descr_xor,
  79. doc=W_AbstractIntObject.descr_xor.__doc__),
  80. __rxor__ = interp2app(W_BoolObject.descr_rxor,
  81. doc=W_AbstractIntObject.descr_rxor.__doc__),
  82. )
  83. W_BoolObject.typedef.acceptable_as_base_class = False