def descr__frozenset__new__(space, w_frozensettype, w_iterable=None): from pypy.objspace.std.setobject import W_FrozensetObject if (space.is_w(w_frozensettype, space.w_frozenset) and w_iterable is not None and type(w_iterable) is W_FrozensetObject): return w_iterable w_obj = space.allocate_instance(W_FrozensetObject, w_frozensettype) W_FrozensetObject.__init__(w_obj, space, w_iterable) return w_obj
def descr__frozenset__new__(space, w_frozensettype, w_iterable=NoneNotWrapped): from pypy.objspace.std.setobject import W_FrozensetObject from pypy.objspace.std.setobject import _is_frozenset_exact if _is_frozenset_exact(w_iterable): return w_iterable w_obj = space.allocate_instance(W_FrozensetObject, w_frozensettype) W_FrozensetObject.__init__(w_obj, space, None) return w_obj
def descr__frozenset__new__(space, w_frozensettype, w_iterable=gateway.NoneNotWrapped): from pypy.objspace.std.setobject import W_FrozensetObject from pypy.objspace.std.setobject import make_setdata_from_w_iterable if (space.is_w(w_frozensettype, space.w_frozenset) and w_iterable is not None and type(w_iterable) is W_FrozensetObject): return w_iterable w_obj = space.allocate_instance(W_FrozensetObject, w_frozensettype) data = make_setdata_from_w_iterable(space, w_iterable) W_FrozensetObject.__init__(w_obj, space, data) return w_obj
def _wrap_not_rpython(self, x): "NOT_RPYTHON" # _____ this code is here to support testing only _____ # we might get there in non-translated versions if 'x' is # a long that fits the correct range. if is_valid_int(x): return self.newint(x) # wrap() of a container works on CPython, but the code is # not RPython. Don't use -- it is kept around mostly for tests. # Use instead newdict(), newlist(), newtuple(). if isinstance(x, dict): items_w = [(self.wrap(k), self.wrap(v)) for (k, v) in x.iteritems()] r = self.newdict() r.initialize_content(items_w) return r if isinstance(x, tuple): wrappeditems = [self.wrap(item) for item in list(x)] return self.newtuple(wrappeditems) if isinstance(x, list): wrappeditems = [self.wrap(item) for item in x] return self.newlist(wrappeditems) # The following cases are even stranger. # Really really only for tests. if type(x) is long: return self.wraplong(x) if isinstance(x, slice): return W_SliceObject(self.wrap(x.start), self.wrap(x.stop), self.wrap(x.step)) if isinstance(x, complex): return W_ComplexObject(x.real, x.imag) if isinstance(x, set): res = W_SetObject(self, self.newlist([self.wrap(item) for item in x])) return res if isinstance(x, frozenset): wrappeditems = [self.wrap(item) for item in x] return W_FrozensetObject(self, wrappeditems) if x is __builtin__.Ellipsis: # '__builtin__.Ellipsis' avoids confusion with special.Ellipsis return self.w_Ellipsis if self.config.objspace.nofaking: raise OperationError( self.w_RuntimeError, self.wrap("nofaking enabled: refusing " "to wrap cpython value %r" % (x, ))) if isinstance(x, type(Exception)) and issubclass(x, Exception): w_result = self.wrap_exception_cls(x) if w_result is not None: return w_result from pypy.objspace.std.fake import fake_object return fake_object(self, x)
def test_and(self): s = W_SetObject(self.space) _initialize_set(self.space, s, self.word) t0 = W_SetObject(self.space) _initialize_set(self.space, t0, self.otherword) t1 = W_FrozensetObject(self.space, self.otherword) r0 = s.descr_and(self.space, t0) r1 = s.descr_and(self.space, t1) assert r0.descr_eq(self.space, r1) == self.true sr = s.descr_intersection(self.space, [self.otherword]) assert r0.descr_eq(self.space, sr) == self.true
def test_and(self): s = W_SetObject(self.space) _initialize_set(self.space, s, self.word) t0 = W_SetObject(self.space) _initialize_set(self.space, t0, self.otherword) t1 = W_FrozensetObject(self.space, self.otherword) r0 = and__Set_Set(self.space, s, t0) r1 = and__Set_Set(self.space, s, t1) assert eq__Set_Set(self.space, r0, r1) == self.true sr = set_intersection__Set(self.space, s, [self.otherword]) assert eq__Set_Set(self.space, r0, sr) == self.true
def _wrap_not_rpython(self, x): "NOT_RPYTHON" # _____ this code is here to support testing only _____ # wrap() of a container works on CPython, but the code is # not RPython. Don't use -- it is kept around mostly for tests. # Use instead newdict(), newlist(), newtuple(). if isinstance(x, dict): items_w = [(self.wrap(k), self.wrap(v)) for (k, v) in x.iteritems()] r = self.newdict() r.initialize_content(items_w) return r if isinstance(x, tuple): wrappeditems = [self.wrap(item) for item in list(x)] return self.newtuple(wrappeditems) if isinstance(x, list): wrappeditems = [self.wrap(item) for item in x] return self.newlist(wrappeditems) # The following cases are even stranger. # Really really only for tests. if type(x) is long: return self.wraplong(x) if isinstance(x, slice): return W_SliceObject(self.wrap(x.start), self.wrap(x.stop), self.wrap(x.step)) if isinstance(x, complex): return W_ComplexObject(x.real, x.imag) if isinstance(x, set): res = W_SetObject(self, self.newlist([self.wrap(item) for item in x])) return res if isinstance(x, frozenset): wrappeditems = [self.wrap(item) for item in x] return W_FrozensetObject(self, wrappeditems) if x is __builtin__.Ellipsis: # '__builtin__.Ellipsis' avoids confusion with special.Ellipsis return self.w_Ellipsis raise OperationError( self.w_RuntimeError, self.wrap("refusing to wrap cpython value %r" % (x, )))
def _optimize_comparator(self, op, node): """Fold lists/sets of constants in the context of "in"/"not in". lists are folded into tuples, sets into frozensets, otherwise returns False """ if op in (ast.In, ast.NotIn): is_list = isinstance(node, ast.List) if is_list or isinstance(node, ast.Set): w_const = self._tuple_of_consts(node.elts) if w_const is not None: if not is_list: from pypy.objspace.std.setobject import ( W_FrozensetObject) w_const = W_FrozensetObject(self.space, w_const) self.load_const(w_const) return True return False
def newfrozenset(self, iterable_w=None): if iterable_w is None: return W_FrozensetObject(self, None) return W_FrozensetObject(self, self.newtuple(iterable_w))
def _wrap_not_rpython(self, x): "NOT_RPYTHON" # _____ this code is here to support testing only _____ # wrap() of a container works on CPython, but the code is # not RPython. Don't use -- it is kept around mostly for tests. # Use instead newdict(), newlist(), newtuple(). if isinstance(x, dict): items_w = [(self.wrap(k), self.wrap(v)) for (k, v) in x.iteritems()] r = self.newdict() r.initialize_content(items_w) return r if isinstance(x, tuple): wrappeditems = [self.wrap(item) for item in list(x)] return self.newtuple(wrappeditems) if isinstance(x, list): wrappeditems = [self.wrap(item) for item in x] return self.newlist(wrappeditems) # The following cases are even stranger. # Really really only for tests. if type(x) is long: if self.config.objspace.std.withsmalllong: from pypy.rlib.rarithmetic import r_longlong try: rx = r_longlong(x) except OverflowError: pass else: from pypy.objspace.std.smalllongobject import \ W_SmallLongObject return W_SmallLongObject(rx) return W_LongObject.fromlong(x) if isinstance(x, slice): return W_SliceObject(self.wrap(x.start), self.wrap(x.stop), self.wrap(x.step)) if isinstance(x, complex): return W_ComplexObject(x.real, x.imag) if isinstance(x, set): rdict_w = r_dict(self.eq_w, self.hash_w) for item in x: rdict_w[self.wrap(item)] = None res = W_SetObject(self, rdict_w) return res if isinstance(x, frozenset): wrappeditems = [self.wrap(item) for item in x] return W_FrozensetObject(self, wrappeditems) if x is __builtin__.Ellipsis: # '__builtin__.Ellipsis' avoids confusion with special.Ellipsis return self.w_Ellipsis if self.config.objspace.nofaking: raise OperationError(self.w_RuntimeError, self.wrap("nofaking enabled: refusing " "to wrap cpython value %r" %(x,))) if isinstance(x, type(Exception)) and issubclass(x, Exception): w_result = self.wrap_exception_cls(x) if w_result is not None: return w_result from pypy.objspace.std.fake import fake_object return fake_object(self, x)