def delslice(self, w_obj, w_start, w_stop): w_descr = self.lookup(w_obj, '__delslice__') if w_descr is not None: w_start, w_stop = old_slice_range(self, w_obj, w_start, w_stop) self.get_and_call_function(w_descr, w_obj, w_start, w_stop) else: ObjSpace.delslice(self, w_obj, w_start, w_stop)
def __init__(self, config=None): self._seen_extras = [] ObjSpace.__init__(self, config=config) self.setup() # Be sure to annotate W_SliceObject constructor. # In Python2, this is triggered by W_InstanceObject.__getslice__. def build_slice(): self.newslice(self.w_None, self.w_None, self.w_None) def attach_list_strategy(): # this is needed for modules which interacts directly with # std.listobject.W_ListObject, e.g. after an isinstance check. For # example, _hpy_universal. We need to attach a couple of attributes # so that the annotator annotates them with the correct types from pypy.objspace.std.listobject import W_ListObject, ObjectListStrategy space = self w_obj = w_some_obj() if isinstance(w_obj, W_ListObject): w_obj.space = space w_obj.strategy = ObjectListStrategy(space) list_w = [w_some_obj(), w_some_obj()] w_obj.lstorage = w_obj.strategy.erase(list_w) self._seen_extras.append(build_slice) self._seen_extras.append(attach_list_strategy)
def __init__(self, config=None): self._seen_extras = [] ObjSpace.__init__(self, config=config) self.setup() # Be sure to annotate W_SliceObject constructor. # In Python2, this is triggered by W_InstanceObject.__getslice__. def build_slice(): self.newslice(self.w_None, self.w_None, self.w_None) self._seen_extras.append(build_slice)
def exception_match(self, w_exc_type, w_check_class): try: check_class = self.unwrap(w_check_class) except UnwrapException: raise Exception, "non-constant except guard" if not isinstance(check_class, tuple): # the simple case return ObjSpace.exception_match(self, w_exc_type, w_check_class) # checking a tuple of classes for w_klass in self.unpacktuple(w_check_class): if ObjSpace.exception_match(self, w_exc_type, w_klass): return True return False
def exception_match(self, w_exc_type, w_check_class): try: check_class = self.unwrap(w_check_class) except UnwrapException: raise Exception, "non-constant except guard" if not isinstance(check_class, tuple): # the simple case return ObjSpace.exception_match(self, w_exc_type, w_check_class) # checking a tuple of classes for w_klass in self.viewiterable(w_check_class): if ObjSpace.exception_match(self, w_exc_type, w_klass): return True return False
def createexecutioncontext(self): # add space specific fields to execution context # note that this method must not call space methods that might need an # execution context themselves (e.g. nearly all space methods) ec = ObjSpace.createexecutioncontext(self) ec._py_repr = None return ec
def call_method(self, w_obj, methname, *arg_w): if self.config.objspace.opcodes.CALL_METHOD: from pypy.objspace.std.callmethod import call_method_opt return call_method_opt(self, w_obj, methname, *arg_w) else: return ObjSpace.call_method(self, w_obj, methname, *arg_w)
def listview(self, w_obj, expected_length=-1): t = self.listview_no_unpack(w_obj) if t is None: return ObjSpace.unpackiterable(self, w_obj, expected_length) if expected_length != -1 and len(t) != expected_length: raise self._wrap_expected_length(expected_length, len(t)) return t
def exception_issubclass_w(space, w_cls1, w_cls2): if isinstance(w_cls1, W_ClassObject): if isinstance(w_cls2, W_ClassObject): return w_cls1.is_subclass_of(w_cls2) return False if isinstance(w_cls2, W_ClassObject): return False return BaseObjSpace.exception_issubclass_w(space, w_cls1, w_cls2)
def fixedview(self, w_obj, expected_length=-1, unroll=False): """ Fast paths """ if isinstance(w_obj, W_TupleObject): t = w_obj.wrappeditems elif isinstance(w_obj, W_ListObject): t = w_obj.wrappeditems[:] else: if unroll: return make_sure_not_resized(ObjSpace.unpackiterable_unroll( self, w_obj, expected_length)[:]) else: return make_sure_not_resized(ObjSpace.unpackiterable( self, w_obj, expected_length)[:]) if expected_length != -1 and len(t) != expected_length: raise self._wrap_expected_length(expected_length, len(t)) return make_sure_not_resized(t)
def fixedview(self, w_obj, expected_length=-1, unroll=False): """ Fast paths """ if isinstance(w_obj, W_AbstractTupleObject) and self._uses_tuple_iter(w_obj): t = w_obj.tolist() elif type(w_obj) is W_ListObject: if unroll: t = w_obj.getitems_unroll() else: t = w_obj.getitems_fixedsize() else: if unroll: return make_sure_not_resized(ObjSpace.unpackiterable_unroll(self, w_obj, expected_length)) else: return make_sure_not_resized(ObjSpace.unpackiterable(self, w_obj, expected_length)[:]) if expected_length != -1 and len(t) != expected_length: raise self._wrap_expected_length(expected_length, len(t)) return make_sure_not_resized(t)
def finditem(self, w_obj, w_key): """ Perform a getitem on w_obj with w_key (any object). Returns found element or None on element not found. performance shortcut to avoid creating the OperationError(KeyError). """ if isinstance(w_obj, W_DictMultiObject) and not w_obj.user_overridden_class: return w_obj.getitem(w_key) return ObjSpace.finditem(self, w_obj, w_key)
def unpackiterable(self, w_obj, expected_length=-1): if isinstance(w_obj, W_AbstractTupleObject) and self._uses_tuple_iter(w_obj): t = w_obj.getitems_copy() elif type(w_obj) is W_ListObject: t = w_obj.getitems_copy() else: return ObjSpace.unpackiterable(self, w_obj, expected_length) if expected_length != -1 and len(t) != expected_length: raise self._wrap_expected_length(expected_length, len(t)) return t
def finditem_str(self, w_obj, key): """ Perform a getitem on w_obj with key (string). Returns found element or None on element not found. performance shortcut to avoid creating the OperationError(KeyError) and allocating W_BytesObject """ if isinstance(w_obj, W_DictMultiObject) and not w_obj.user_overridden_class: return w_obj.getitem_str(key) return ObjSpace.finditem_str(self, w_obj, key)
def unpackiterable(self, w_obj, expected_length=-1): if isinstance(w_obj, W_TupleObject): t = w_obj.wrappeditems[:] elif isinstance(w_obj, W_ListObject): t = w_obj.wrappeditems[:] else: return ObjSpace.unpackiterable(self, w_obj, expected_length) if expected_length != -1 and len(t) != expected_length: raise UnpackValueError("Expected length %d, got %d" % (expected_length, len(t))) return t
def exception_issubclass_w(space, w_cls1, w_cls2): cls1 = space.interpclass_w(w_cls1) cls2 = space.interpclass_w(w_cls2) if isinstance(cls1, W_ClassObject): if isinstance(cls2, W_ClassObject): return cls1.is_subclass_of(cls2) return False if isinstance(cls2, W_ClassObject): return False return BaseObjSpace.exception_issubclass_w(space, w_cls1, w_cls2)
def finditem(self, w_obj, w_key): """ Perform a getitem on w_obj with w_key (any object). Returns found element or None on element not found. performance shortcut to avoid creating the OperationError(KeyError). """ if (isinstance(w_obj, W_DictMultiObject) and not w_obj.user_overridden_class): return w_obj.getitem(w_key) return ObjSpace.finditem(self, w_obj, w_key)
def listview(self, w_obj, expected_length=-1): if isinstance(w_obj, W_ListObject): t = w_obj.wrappeditems elif isinstance(w_obj, W_TupleObject): t = w_obj.wrappeditems[:] else: return ObjSpace.unpackiterable(self, w_obj, expected_length) if expected_length != -1 and len(t) != expected_length: raise self._wrap_expected_length(expected_length, len(t)) return t
def exception_match(self, w_exc_type, w_check_class): try: check_class = self.unwrap(w_check_class) except UnwrapException: raise Exception, "non-constant except guard" if check_class in (NotImplementedError, AssertionError): raise error.FlowingError("Catching %s is not valid in RPython" % check_class.__name__) if not isinstance(check_class, tuple): # the simple case return ObjSpace.exception_match(self, w_exc_type, w_check_class) # special case for StackOverflow (see rlib/rstackovf.py) if check_class == rstackovf.StackOverflow: w_real_class = self.wrap(rstackovf._StackOverflow) return ObjSpace.exception_match(self, w_exc_type, w_real_class) # checking a tuple of classes for w_klass in self.fixedview(w_check_class): if self.exception_match(w_exc_type, w_klass): return True return False
def fixedview(self, w_obj, expected_length=-1, unroll=False): """ Fast paths """ if isinstance(w_obj, W_AbstractTupleObject) and self._uses_tuple_iter(w_obj): t = w_obj.tolist() elif type(w_obj) is W_ListObject: if unroll: t = w_obj.getitems_unroll() else: t = w_obj.getitems_fixedsize() else: if unroll: return make_sure_not_resized(ObjSpace.unpackiterable_unroll( self, w_obj, expected_length)) else: return make_sure_not_resized(ObjSpace.unpackiterable( self, w_obj, expected_length)[:]) if expected_length != -1 and len(t) != expected_length: raise self._wrap_expected_length(expected_length, len(t)) return make_sure_not_resized(t)
def finditem_str(self, w_obj, key): """ Perform a getitem on w_obj with key (string). Returns found element or None on element not found. performance shortcut to avoid creating the OperationError(KeyError) and allocating W_BytesObject """ if (isinstance(w_obj, W_DictMultiObject) and not w_obj.user_overridden_class): return w_obj.getitem_str(key) return ObjSpace.finditem_str(self, w_obj, key)
def getindex_w(self, w_obj, w_exception, objdescr=None): # Performance shortcut for the common case of w_obj being an int. # If withsmallint is disabled, we check for W_IntObject. # If withsmallint is enabled, we only check for W_SmallIntObject - it's # probably not useful to have a shortcut for W_IntObject at all then. if self.config.objspace.std.withsmallint: if type(w_obj) is W_SmallIntObject: return w_obj.intval else: if type(w_obj) is W_IntObject: return w_obj.intval return ObjSpace.getindex_w(self, w_obj, w_exception, objdescr)
def fixedview(self, w_obj, expected_length=-1): """ Fast paths """ if isinstance(w_obj, W_TupleObject): t = w_obj.wrappeditems elif isinstance(w_obj, W_ListObject): t = w_obj.wrappeditems[:] else: return ObjSpace.fixedview(self, w_obj, expected_length) if expected_length != -1 and len(t) != expected_length: raise UnpackValueError("Expected length %d, got %d" % (expected_length, len(t))) return t
def createexecutioncontext(self): # add space specific fields to execution context ec = ObjSpace.createexecutioncontext(self) ec._py_repr = self.newdict() if self.config.objspace.std.withmethodcache: SIZE = 1 << self.config.objspace.std.methodcachesizeexp ec.method_cache_versions = [None] * SIZE ec.method_cache_names = [None] * SIZE ec.method_cache_lookup_where = [(None, None)] * SIZE if self.config.objspace.std.withmethodcachecounter: ec.method_cache_hits = {} ec.method_cache_misses = {} return ec
def createexecutioncontext(self): # add space specific fields to execution context # note that this method must not call space methods that might need an # execution context themselves (e.g. nearly all space methods) ec = ObjSpace.createexecutioncontext(self) ec._py_repr = None if self.config.objspace.std.withmethodcache: SIZE = 1 << self.config.objspace.std.methodcachesizeexp ec.method_cache_versions = [None] * SIZE ec.method_cache_names = [None] * SIZE ec.method_cache_lookup_where = [(None, None)] * SIZE if self.config.objspace.std.withmethodcachecounter: ec.method_cache_hits = {} ec.method_cache_misses = {} return ec
def unpackiterable(self, w_iterable, expected_length=None): if not isinstance(w_iterable, Variable): l = list(self.unwrap(w_iterable)) if expected_length is not None and len(l) != expected_length: raise ValueError return [self.wrap(x) for x in l] if isinstance(w_iterable, Variable) and expected_length is None: raise UnwrapException, ("cannot unpack a Variable iterable" "without knowing its length") elif expected_length is not None: w_len = self.len(w_iterable) w_correct = self.eq(w_len, self.wrap(expected_length)) if not self.is_true(w_correct): e = OperationError(self.w_ValueError, self.w_None) e.normalize_exception(self) raise e return [self.do_operation("getitem", w_iterable, self.wrap(i)) for i in range(expected_length)] return ObjSpace.unpackiterable(self, w_iterable, expected_length)
def unpackiterable(self, w_iterable, expected_length=None): if not isinstance(w_iterable, Variable): l = list(self.unwrap(w_iterable)) if expected_length is not None and len(l) != expected_length: raise ValueError return [self.wrap(x) for x in l] if isinstance(w_iterable, Variable) and expected_length is None: raise UnwrapException, ("cannot unpack a Variable iterable" "without knowing its length") elif expected_length is not None: w_len = self.len(w_iterable) w_correct = self.eq(w_len, self.wrap(expected_length)) if not self.is_true(w_correct): e = OperationError(self.w_ValueError, self.w_None) e.normalize_exception(self) raise e return [self.do_operation('getitem', w_iterable, self.wrap(i)) for i in range(expected_length)] return ObjSpace.unpackiterable(self, w_iterable, expected_length)
def exception_issubclass_w(space, w_cls1, w_cls2): if (space.type(w_cls1) is space.w_type and space.type(w_cls2) is space.w_type): return BaseObjSpace.exception_issubclass_w(space, w_cls1, w_cls2) # if (not exception_is_valid_class_w(space, w_cls2) or not exception_is_valid_class_w(space, w_cls1)): return False # # The rest is the rare slow case. Use the general logic of issubclass() # (issue #3149). CPython 3.x doesn't do that (but there is a # many-years issue report: https://bugs.python.org/issue12029), and # there are probably tests, so we won't call abstract_issubclass_w() # either in PyPy3. try: return abstract_issubclass_w(space, w_cls1, w_cls2, True) except OperationError as e: if e. async (space): raise return False
def unpackiterable(self, w_iterable, expected_length=None): if not isinstance(w_iterable, Variable): l = list(self.unwrap(w_iterable)) if expected_length is not None and len(l) != expected_length: raise ValueError return [self.wrap(x) for x in l] if isinstance(w_iterable, Variable) and expected_length is None: raise UnwrapException, ("cannot unpack a Variable iterable" "without knowing its length") ## # XXX TEMPORARY HACK XXX TEMPORARY HACK XXX TEMPORARY HACK ## print ("*** cannot unpack a Variable iterable " ## "without knowing its length,") ## print " assuming a list or tuple with up to 7 items" ## items = [] ## w_len = self.len(w_iterable) ## i = 0 ## while True: ## w_i = self.wrap(i) ## w_cond = self.eq(w_len, w_i) ## if self.is_true(w_cond): ## break # done ## if i == 7: ## # too many values ## raise OperationError(self.w_AssertionError, self.w_None) ## w_item = self.do_operation('getitem', w_iterable, w_i) ## items.append(w_item) ## i += 1 ## return items ## # XXX TEMPORARY HACK XXX TEMPORARY HACK XXX TEMPORARY HACK elif expected_length is not None: w_len = self.len(w_iterable) w_correct = self.eq(w_len, self.wrap(expected_length)) if not self.is_true(w_correct): e = OperationError(self.w_ValueError, self.w_None) e.normalize_exception(self) raise e return [ self.do_operation('getitem', w_iterable, self.wrap(i)) for i in range(expected_length) ] return ObjSpace.unpackiterable(self, w_iterable, expected_length)
def unpackiterable(self, w_iterable, expected_length=None): if not isinstance(w_iterable, Variable): l = list(self.unwrap(w_iterable)) if expected_length is not None and len(l) != expected_length: raise ValueError return [self.wrap(x) for x in l] if isinstance(w_iterable, Variable) and expected_length is None: raise UnwrapException, ("cannot unpack a Variable iterable" "without knowing its length") ## # XXX TEMPORARY HACK XXX TEMPORARY HACK XXX TEMPORARY HACK ## print ("*** cannot unpack a Variable iterable " ## "without knowing its length,") ## print " assuming a list or tuple with up to 7 items" ## items = [] ## w_len = self.len(w_iterable) ## i = 0 ## while True: ## w_i = self.wrap(i) ## w_cond = self.eq(w_len, w_i) ## if self.is_true(w_cond): ## break # done ## if i == 7: ## # too many values ## raise OperationError(self.w_AssertionError, self.w_None) ## w_item = self.do_operation('getitem', w_iterable, w_i) ## items.append(w_item) ## i += 1 ## return items ## # XXX TEMPORARY HACK XXX TEMPORARY HACK XXX TEMPORARY HACK elif expected_length is not None: w_len = self.len(w_iterable) w_correct = self.eq(w_len, self.wrap(expected_length)) if not self.is_true(w_correct): e = OperationError(self.w_ValueError, self.w_None) e.normalize_exception(self) raise e return [self.do_operation('getitem', w_iterable, self.wrap(i)) for i in range(expected_length)] return ObjSpace.unpackiterable(self, w_iterable, expected_length)
def getindex_w(self, w_obj, w_exception, objdescr=None, errmsg=None): if type(w_obj) is W_IntObject: return w_obj.intval return ObjSpace.getindex_w(self, w_obj, w_exception, objdescr, errmsg)
def exception_is_valid_obj_as_class_w(space, w_obj): obj = space.interpclass_w(w_obj) if isinstance(obj, W_ClassObject): return True return BaseObjSpace.exception_is_valid_obj_as_class_w(space, w_obj)
def exception_is_valid_class_w(space, w_cls): cls = space.interpclass_w(w_cls) if isinstance(cls, W_ClassObject): return True return BaseObjSpace.exception_is_valid_class_w(space, w_cls)
def exception_getclass(space, w_obj): obj = space.interpclass_w(w_obj) if isinstance(obj, W_InstanceObject): return obj.w_class return BaseObjSpace.exception_getclass(space, w_obj)
def __init__(self, config=None): self._seen_extras = [] ObjSpace.__init__(self, config=config) self.setup()
def createexecutioncontext(self): ec = ObjSpace.createexecutioncontext(self) ec._py_repr = None return ec
def __init__(self, config=None): self._seen_extras = [] ObjSpace.__init__(self, config=config)
def createframe(self, code, w_globals, outer_func=None): from pypy.objspace.std.fake import CPythonFakeCode, CPythonFakeFrame if not we_are_translated() and isinstance(code, CPythonFakeCode): return CPythonFakeFrame(self, code, w_globals) else: return ObjSpace.createframe(self, code, w_globals, outer_func)
def newlist_utf8(self, list_u, is_ascii): if is_ascii: return W_ListObject.newlist_utf8(self, list_u) return ObjSpace.newlist_utf8(self, list_u, False)
def getindex_w(self, w_obj, w_exception, objdescr=None): if type(w_obj) is W_IntObject: return w_obj.intval return ObjSpace.getindex_w(self, w_obj, w_exception, objdescr)
def finditem(self, w_obj, w_key): # performance shortcut to avoid creating the OperationError(KeyError) if type(w_obj) is self.DictObjectCls: return w_obj.get(w_key, None) return ObjSpace.finditem(self, w_obj, w_key)