pypy / pypy / objspace / std /

"""The builtin bool implementation"""

import operator

from rpython.rlib.rarithmetic import r_uint
from rpython.tool.sourcetools import func_renamer, func_with_new_name

from pypy.interpreter.gateway import WrappedDefault, interp2app, unwrap_spec
from pypy.objspace.std.intobject import W_AbstractIntObject, W_IntObject
from pypy.objspace.std.stdtypedef import StdTypeDef

class W_BoolObject(W_IntObject):

    def __init__(self, boolval):
        self.intval = not not boolval

    def __nonzero__(self):
        raise Exception("you cannot do that, you must use space.is_true()")

    def __repr__(self):
        """representation for debugging purposes"""
        return "%s(%s)" % (self.__class__.__name__, bool(self.intval))

    def is_w(self, space, w_other):
        return self is w_other

    def immutable_unique_id(self, space):
        return None

    def unwrap(self, space):
        return bool(self.intval)

    def uint_w(self, space):
        return r_uint(self.intval)

    def int(self, space):
        return space.newint(self.intval)

    def descr_new(space, w_booltype, w_obj):
        """T.__new__(S, ...) -> a new object with type S, a subtype of T"""
        return space.newbool(space.is_true(w_obj))

    def descr_repr(self, space):
        return space.wrap('True' if self.intval else 'False')
    descr_str = func_with_new_name(descr_repr, 'descr_str')

    def descr_nonzero(self, space):
        return self

    def _make_bitwise_binop(opname):
        descr_name = 'descr_' + opname
        int_op = getattr(W_IntObject, descr_name)
        op = getattr(operator,
                     opname + '_' if opname in ('and', 'or') else opname)

        def descr_binop(self, space, w_other):
            if not isinstance(w_other, W_BoolObject):
                return int_op(self, space, w_other)
            a = bool(self.intval)
            b = bool(w_other.intval)
            return space.newbool(op(a, b))

        @func_renamer('descr_r' + opname)
        def descr_rbinop(self, space, w_other):
            return descr_binop(self, space, w_other)

        return descr_binop, descr_rbinop

    descr_and, descr_rand = _make_bitwise_binop('and')
    descr_or, descr_ror = _make_bitwise_binop('or')
    descr_xor, descr_rxor = _make_bitwise_binop('xor')

W_BoolObject.w_False = W_BoolObject(False)
W_BoolObject.w_True = W_BoolObject(True)

W_BoolObject.typedef = StdTypeDef("bool", W_IntObject.typedef,
    __doc__ = """bool(x) -> bool

Returns True when the argument x is true, False otherwise.
The builtins True and False are the only two instances of the class bool.
The class bool is a subclass of the class int, and cannot be subclassed.""",
    __new__ = interp2app(W_BoolObject.descr_new),
    __repr__ = interp2app(W_BoolObject.descr_repr,
    __str__ = interp2app(W_BoolObject.descr_str,
    __nonzero__ = interp2app(W_BoolObject.descr_nonzero,

    __and__ = interp2app(W_BoolObject.descr_and,
    __rand__ = interp2app(W_BoolObject.descr_rand,
    __or__ = interp2app(W_BoolObject.descr_or,
    __ror__ = interp2app(W_BoolObject.descr_ror,
    __xor__ = interp2app(W_BoolObject.descr_xor,
    __rxor__ = interp2app(W_BoolObject.descr_rxor,
W_BoolObject.typedef.acceptable_as_base_class = False
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.