def is_true(w_obj): # a bit of duplication of the logic from DescrOperation.is_true... # first try 'nonzero' try: w_res = nonzero_shortcut(space, w_obj) except FailedToImplement: pass else: # the __nonzero__ method of built-in objects should # always directly return a Bool assert isinstance(w_res, W_BoolObject) return w_res.boolval # then try 'len' try: w_res = len_shortcut(space, w_obj) except FailedToImplement: pass else: # the __len__ method of built-in objects typically # returns an unwrappable integer try: return space.int_w(w_res) != 0 except OperationError: # I think no OperationError other than w_OverflowError # could occur here w_obj = w_res # general case fallback return DescrOperation.is_true(space, w_obj)
def is_true(self, w_obj): # first a few shortcuts for performance if type(w_obj) is W_BoolObject: return w_obj.boolval if w_obj is self.w_None: return False # then a shortcut for bootstrapping reasons if type(w_obj) is self.DictObjectCls: return w_obj.len() != 0 else: return DescrOperation.is_true(self, w_obj)
def is_true(w_obj): # a bit of duplication of the logic from DescrOperation.is_true... try: w_res = shortcut(space, w_obj) except FailedToImplement: pass else: # the __nonzero__ method of built-in objects should # always directly return a Bool; however, the __len__ method # of built-in objects typically returns an unwrappable integer if isinstance(w_res, W_BoolObject): return w_res.boolval try: return space.int_w(w_res) != 0 except OperationError: # I think no OperationError other than w_OverflowError # could occur here w_obj = w_res # general case fallback return DescrOperation.is_true(space, w_obj)
def is_true(self, w_obj): # a shortcut for performance # NOTE! this method is typically overridden by builtinshortcut.py. if type(w_obj) is W_BoolObject: return w_obj.boolval return DescrOperation.is_true(self, w_obj)