Пример #1
0
def PySlice_Unpack(space, w_slice, start_p, stop_p, step_p):
    if not isinstance(w_slice, W_SliceObject):
        raise PyErr_BadInternalCall(space)

    if space.is_none(w_slice.w_step):
        step = 1
    else:
        step = W_SliceObject.eval_slice_index(space, w_slice.w_step)
        if step == 0:
            raise oefmt(space.w_ValueError, "slice step cannot be zero")
        if step < -sys.maxint:
            step = -sys.maxint
    step_p[0] = step

    if space.is_none(w_slice.w_start):
        start = sys.maxint if step < 0 else 0
    else:
        start = W_SliceObject.eval_slice_index(space, w_slice.w_start)
    start_p[0] = start

    if space.is_none(w_slice.w_stop):
        stop = -sys.maxint - 1 if step < 0 else sys.maxint
    else:
        stop = W_SliceObject.eval_slice_index(space, w_slice.w_stop)
    stop_p[0] = stop

    return 0
Пример #2
0
    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)
Пример #3
0
 def descr__new__(space, w_slicetype, args_w):
     from pypy.objspace.std.sliceobject import W_SliceObject
     w_start = space.w_None
     w_stop = space.w_None
     w_step = space.w_None
     if len(args_w) == 1:
         w_stop, = args_w
     elif len(args_w) == 2:
         w_start, w_stop = args_w
     elif len(args_w) == 3:
         w_start, w_stop, w_step = args_w
     elif len(args_w) > 3:
         raise oefmt(space.w_TypeError, "slice() takes at most 3 arguments")
     else:
         raise oefmt(space.w_TypeError, "slice() takes at least 1 argument")
     w_obj = space.allocate_instance(W_SliceObject, w_slicetype)
     W_SliceObject.__init__(w_obj, w_start, w_stop, w_step)
     return w_obj
Пример #4
0
 def descr__new__(space, w_slicetype, args_w):
     from pypy.objspace.std.sliceobject import W_SliceObject
     w_start = space.w_None
     w_stop = space.w_None
     w_step = space.w_None
     if len(args_w) == 1:
         w_stop, = args_w
     elif len(args_w) == 2:
         w_start, w_stop = args_w
     elif len(args_w) == 3:
         w_start, w_stop, w_step = args_w
     elif len(args_w) > 3:
         raise oefmt(space.w_TypeError, "slice() takes at most 3 arguments")
     else:
         raise oefmt(space.w_TypeError, "slice() takes at least 1 argument")
     w_obj = space.allocate_instance(W_SliceObject, w_slicetype)
     W_SliceObject.__init__(w_obj, w_start, w_stop, w_step)
     return w_obj
Пример #5
0
 def descr__new__(space, w_slicetype, args_w):
     from pypy.objspace.std.sliceobject import W_SliceObject
     w_start = space.w_None
     w_stop = space.w_None
     w_step = space.w_None
     if len(args_w) == 1:
         w_stop, = args_w
     elif len(args_w) == 2:
         w_start, w_stop = args_w
     elif len(args_w) == 3:
         w_start, w_stop, w_step = args_w
     elif len(args_w) > 3:
         raise oefmt(space.w_TypeError,
                     "cortar() toma por lo máximo 3 argumentos")
     else:
         raise oefmt(space.w_TypeError,
                     "cortar() toma por lo menos 1 argumento")
     w_obj = space.allocate_instance(W_SliceObject, w_slicetype)
     W_SliceObject.__init__(w_obj, w_start, w_stop, w_step)
     return w_obj
Пример #6
0
def PySlice_New(space, w_start, w_stop, w_step):
    """Return a new slice object with the given values.  The start, stop, and
    step parameters are used as the values of the slice object attributes of
    the same names.  Any of the values may be NULL, in which case the
    None will be used for the corresponding attribute.  Return NULL if
    the new object could not be allocated."""
    if w_start is None:
        w_start = space.w_None
    if w_stop is None:
        w_stop = space.w_None
    if w_step is None:
        w_step = space.w_None
    return W_SliceObject(w_start, w_stop, w_step)
Пример #7
0
    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, )))
Пример #8
0
 def newslice(self, w_start, w_end, w_step):
     return W_SliceObject(w_start, w_end, w_step)
Пример #9
0
 def _do_startup():
     self.threadlocals.enter_thread(self)
     W_SliceObject(w_some_obj(), w_some_obj(), w_some_obj())
Пример #10
0
 def newslice(self, w_start, w_end, w_step):
     is_root(w_start)
     is_root(w_end)
     is_root(w_step)
     W_SliceObject(w_start, w_end, w_step)
     return w_some_obj()
Пример #11
0
    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)
Пример #12
0
 def entry_point(argv):
     self.threadlocals.enter_thread(self)
     W_SliceObject(w_some_obj(), w_some_obj(), w_some_obj())
     if extra_func:
         extra_func(self)
     return 0