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 getattr(self, w_obj, w_name): if not self.config.objspace.std.getattributeshortcut: return DescrOperation.getattr(self, w_obj, w_name) # an optional shortcut for performance from pypy.objspace.descroperation import raiseattrerror from pypy.objspace.descroperation import object_getattribute w_type = self.type(w_obj) if not w_type.uses_object_getattribute: # slow path: look for a custom __getattribute__ on the class w_descr = w_type.lookup('__getattribute__') # if it was not actually overriden in the class, we remember this # fact for the next time. if w_descr is object_getattribute(self): w_type.uses_object_getattribute = True return self._handle_getattribute(w_descr, w_obj, w_name) # fast path: XXX this is duplicating most of the logic # from the default __getattribute__ and the getattr() method... name = self.str_w(w_name) w_descr = w_type.lookup(name) e = None if w_descr is not None: if not self.is_data_descr(w_descr): w_value = w_obj.getdictvalue_attr_is_in_class(self, w_name) if w_value is not None: return w_value try: return self.get(w_descr, w_obj) except OperationError, e: if not e.match(self, self.w_AttributeError): raise
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 getattr(self, w_obj, w_name): if not self.config.objspace.std.getattributeshortcut: return DescrOperation.getattr(self, w_obj, w_name) # an optional shortcut for performance w_type = self.type(w_obj) w_descr = w_type.getattribute_if_not_from_object() if w_descr is not None: return self._handle_getattribute(w_descr, w_obj, w_name) # fast path: XXX this is duplicating most of the logic # from the default __getattribute__ and the getattr() method... name = self.str_w(w_name) w_descr = w_type.lookup(name) e = None if w_descr is not None: w_get = None is_data = self.is_data_descr(w_descr) if is_data: w_get = self.lookup(w_descr, "__get__") if w_get is None: w_value = w_obj.getdictvalue(self, name) if w_value is not None: return w_value if not is_data: w_get = self.lookup(w_descr, "__get__") if w_get is not None: # __get__ is allowed to raise an AttributeError to trigger # use of __getattr__. try: return self.get_and_call_function(w_get, w_descr, w_obj, w_type) except OperationError, e: if not e.match(self, self.w_AttributeError): raise else: return w_descr
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)