| 1 | """The builtin bool implementation""" |
| 2 | |
| 3 | import operator |
| 4 | |
| 5 | from rpython.rlib.rarithmetic import r_uint |
| 6 | from rpython.tool.sourcetools import func_renamer, func_with_new_name |
| 7 | |
| 8 | from pypy.interpreter.gateway import WrappedDefault, interp2app, unwrap_spec |
| 9 | from pypy.interpreter.typedef import TypeDef |
| 10 | from pypy.objspace.std.intobject import W_AbstractIntObject, W_IntObject |
| 11 | |
| 12 | |
| 13 | class W_BoolObject(W_IntObject): |
| 14 | |
| 15 | def __init__(self, boolval): |
| 16 | self.intval = int(not not boolval) |
| 17 | |
| 18 | def __nonzero__(self): |
| 19 | raise Exception("you cannot do that, you must use space.is_true()") |
| 20 | |
| 21 | def __repr__(self): |
| 22 | """representation for debugging purposes""" |
| 23 | return "%s(%s)" % (self.__class__.__name__, bool(self.intval)) |
| 24 | |
| 25 | def is_w(self, space, w_other): |
| 26 | return self is w_other |
| 27 | |
| 28 | def immutable_unique_id(self, space): |
| 29 | return None |
| 30 | |
| 31 | def unwrap(self, space): |
| 32 | return bool(self.intval) |
| 33 | |
| 34 | def uint_w(self, space): |
| 35 | return r_uint(self.intval) |
| 36 | |
| 37 | def int(self, space): |
| 38 | return space.newint(self.intval) |
| 39 | |
| 40 | @staticmethod |
| 41 | @unwrap_spec(w_obj=WrappedDefault(False)) |
| 42 | def descr_new(space, w_booltype, w_obj): |
| 43 | "Create and return a new object. See help(type) for accurate signature." |
| 44 | space.w_bool.check_user_subclass(w_booltype) |
| 45 | return space.newbool(space.is_true(w_obj)) |
| 46 | |
| 47 | def descr_repr(self, space): |
| 48 | return space.newtext('True' if self.intval else 'False') |
| 49 | descr_str = func_with_new_name(descr_repr, 'descr_str') |
| 50 | |
| 51 | def descr_bool(self, space): |
| 52 | return self |
| 53 | |
| 54 | def _make_bitwise_binop(opname): |
| 55 | descr_name = 'descr_' + opname |
| 56 | int_op = getattr(W_IntObject, descr_name) |
| 57 | op = getattr(operator, |
| 58 | opname + '_' if opname in ('and', 'or') else opname) |
| 59 | |
| 60 | @func_renamer(descr_name) |
| 61 | def descr_binop(self, space, w_other): |
| 62 | if not isinstance(w_other, W_BoolObject): |
| 63 | return int_op(self, space, w_other) |
| 64 | a = bool(self.intval) |
| 65 | b = bool(w_other.intval) |
| 66 | return space.newbool(op(a, b)) |
| 67 | |
| 68 | @func_renamer('descr_r' + opname) |
| 69 | def descr_rbinop(self, space, w_other): |
| 70 | return descr_binop(self, space, w_other) |
| 71 | |
| 72 | return descr_binop, descr_rbinop |
| 73 | |
| 74 | descr_and, descr_rand = _make_bitwise_binop('and') |
| 75 | descr_or, descr_ror = _make_bitwise_binop('or') |
| 76 | descr_xor, descr_rxor = _make_bitwise_binop('xor') |
| 77 | |
| 78 | |
| 79 | W_BoolObject.w_False = W_BoolObject(False) |
| 80 | W_BoolObject.w_True = W_BoolObject(True) |
| 81 | |
| 82 | |
| 83 | W_BoolObject.typedef = TypeDef("bool", W_IntObject.typedef, |
| 84 | __doc__ = """bool(x) -> bool |
| 85 | |
| 86 | Returns True when the argument x is true, False otherwise. |
| 87 | The builtins True and False are the only two instances of the class bool. |
| 88 | The class bool is a subclass of the class int, and cannot be subclassed.""", |
| 89 | __new__ = interp2app(W_BoolObject.descr_new), |
| 90 | __repr__ = interp2app(W_BoolObject.descr_repr, |
| 91 | doc=W_AbstractIntObject.descr_repr.__doc__), |
| 92 | __str__ = interp2app(W_BoolObject.descr_str, |
| 93 | doc=W_AbstractIntObject.descr_str.__doc__), |
| 94 | __bool__ = interp2app(W_BoolObject.descr_bool, |
| 95 | doc=W_AbstractIntObject.descr_bool.__doc__), |
| 96 | |
| 97 | __and__ = interp2app(W_BoolObject.descr_and, |
| 98 | doc=W_AbstractIntObject.descr_and.__doc__), |
| 99 | __rand__ = interp2app(W_BoolObject.descr_rand, |
| 100 | doc=W_AbstractIntObject.descr_rand.__doc__), |
| 101 | __or__ = interp2app(W_BoolObject.descr_or, |
| 102 | doc=W_AbstractIntObject.descr_or.__doc__), |
| 103 | __ror__ = interp2app(W_BoolObject.descr_ror, |
| 104 | doc=W_AbstractIntObject.descr_ror.__doc__), |
| 105 | __xor__ = interp2app(W_BoolObject.descr_xor, |
| 106 | doc=W_AbstractIntObject.descr_xor.__doc__), |
| 107 | __rxor__ = interp2app(W_BoolObject.descr_rxor, |
| 108 | doc=W_AbstractIntObject.descr_rxor.__doc__), |
| 109 | ) |
| 110 | W_BoolObject.typedef.acceptable_as_base_class = False |