pypy / pypy / objspace / std / sliceobject.py

Benjamin Peterso… ea8031c 
Armin Rigo bf255bb 
Benjamin Peterso… 7e2f9bc 
Adrien Di Mascio 6a7235e 
Benjamin Peterso… 7e2f9bc 

Carl Friedrich B… 2e61706 
Armin Rigo bf255bb 


Armin Rigo 72ff127 
Armin Rigo 4eaa4d9 
Armin Rigo fca0236 
Armin Rigo 901f6d6 


Armin Rigo bf255bb 



Armin Rigo fca0236 
Samuele Pedroni ca4b87c 

Armin Rigo fca0236 
Carl Friedrich B… ae47a8c 
Armin Rigo 4e0bd06 

Carl Friedrich B… 2e61706 
Armin Rigo 4e0bd06 


Carl Friedrich B… ae47a8c 
Armin Rigo 4e0bd06 




Carl Friedrich B… 2e61706 
Armin Rigo 4e0bd06 











Carl Friedrich B… ae47a8c 
Armin Rigo 4e0bd06 




Carl Friedrich B… 2e61706 
Armin Rigo 4e0bd06 


Amaury Forgeot d… ff893fd 








Armin Rigo 4e0bd06 

Armin Rigo fca0236 

Armin Rigo 4e0bd06 







Amaury Forgeot d… 7acae99 



Armin Rigo bf255bb 

Armin Rigo 4e0bd06 
Armin Rigo 50f5c28 

Armin Rigo e1f094c 
Armin Rigo af8bd2e 

Armin Rigo 50f5c28 

Armin Rigo 1167487 
Armin Rigo 50f5c28 



Armin Rigo af8bd2e 



Armin Rigo 50f5c28 


Christian Tismer ced7cfd 
Christian Tismer 5fed469 
holger krekel 3b9086f 
Christian Tismer 2e4e2ed 
Adrien Di Mascio 6a7235e 
Adrien Di Mascio 63a60eb 












Armin Rigo 09bec65 










Samuele Pedroni f586819 
Adrien Di Mascio 63a60eb 
Samuele Pedroni f586819 
Guido Wesdorp f74fa3d 
Armin Rigo fca0236 
Samuele Pedroni f586819 



Armin Rigo 4e0bd06 
Samuele Pedroni f586819 
"""Slice object"""

from pypy.interpreter.error import OperationError
from pypy.interpreter import gateway
from pypy.objspace.std.model import registerimplementation, W_Object
from pypy.objspace.std.register_all import register_all
from pypy.objspace.std.slicetype import _eval_slice_index

class W_SliceObject(W_Object):
    from pypy.objspace.std.slicetype import slice_typedef as typedef
    _immutable_fields_ = ['w_start', 'w_stop', 'w_step']

    def __init__(w_self, w_start, w_stop, w_step):
        assert w_start is not None
        assert w_stop is not None
        assert w_step is not None
        w_self.w_start = w_start
        w_self.w_stop = w_stop
        w_self.w_step = w_step

    def unwrap(w_slice, space):
        return slice(space.unwrap(w_slice.w_start), space.unwrap(w_slice.w_stop), space.unwrap(w_slice.w_step))

    def indices3(w_slice, space, length):
        if space.is_w(w_slice.w_step, space.w_None):
            step = 1
        else:
            step = _eval_slice_index(space, w_slice.w_step)
            if step == 0:
                raise OperationError(space.w_ValueError,
                                     space.wrap("slice step cannot be zero"))
        if space.is_w(w_slice.w_start, space.w_None):
            if step < 0:
                start = length - 1
            else:
                start = 0
        else:
            start = _eval_slice_index(space, w_slice.w_start)
            if start < 0:
                start += length
                if start < 0:
                    if step < 0:
                        start = -1
                    else:
                        start = 0
            elif start >= length:
                if step < 0:
                    start = length - 1
                else:
                    start = length
        if space.is_w(w_slice.w_stop, space.w_None):
            if step < 0:
                stop = -1
            else:
                stop = length
        else:
            stop = _eval_slice_index(space, w_slice.w_stop)
            if stop < 0:
                stop += length
                if stop < 0:
                    if step < 0:
                        stop = -1
                    else:
                        stop = 0
            elif stop >= length:
                if step < 0:
                    stop = length - 1
                else:
                    stop = length
        return start, stop, step

    def indices4(w_slice, space, length):
        start, stop, step = w_slice.indices3(space, length)
        if (step < 0 and stop >= start) or (step > 0 and start >= stop):
            slicelength = 0
        elif step < 0:
            slicelength = (stop - start + 1) / step + 1
        else:
            slicelength = (stop - start - 1) / step + 1
        return start, stop, step, slicelength

    def __repr__(self):
        return "<W_SliceObject(%r, %r, %r)>" % (
            self.w_start, self.w_stop, self.w_step)

registerimplementation(W_SliceObject)


def normalize_simple_slice(space, length, w_start, w_stop):
    """Helper for the {get,set,del}slice multimethod implementations."""
    # this returns a pair (start, stop) which is usable for slicing
    # a sequence of the given length in the most friendly way, i.e.
    # guaranteeing that 0 <= start <= stop <= length.
    start = space.int_w(w_start)
    stop = space.int_w(w_stop)
    assert length >= 0
    if start < 0:
        start = 0
    if stop < start:
        stop = start
    if stop > length:
        stop = length
        if start > length:
            start = length
    return start, stop


repr__Slice = gateway.applevel("""
    def repr__Slice(aslice):
        return 'slice(%r, %r, %r)' % (aslice.start, aslice.stop, aslice.step)
""", filename=__file__).interphook("repr__Slice")

def eq__Slice_Slice(space, w_slice1, w_slice2):
    # We need this because CPython considers that slice1 == slice1
    # is *always* True (e.g. even if slice1 was built with non-comparable
    # parameters
    if space.is_w(w_slice1, w_slice2):
        return space.w_True
    if space.eq_w(w_slice1.w_start, w_slice2.w_start) and \
        space.eq_w(w_slice1.w_stop, w_slice2.w_stop) and \
        space.eq_w(w_slice1.w_step, w_slice2.w_step):
        return space.w_True
    else:
        return space.w_False

def lt__Slice_Slice(space, w_slice1, w_slice2):
    if space.is_w(w_slice1, w_slice2):
        return space.w_False   # see comments in eq__Slice_Slice()
    if space.eq_w(w_slice1.w_start, w_slice2.w_start):
        if space.eq_w(w_slice1.w_stop, w_slice2.w_stop):
            return space.lt(w_slice1.w_step, w_slice2.w_step)
        else:
            return space.lt(w_slice1.w_stop, w_slice2.w_stop)
    else:
        return space.lt(w_slice1.w_start, w_slice2.w_start)

# indices impl

def slice_indices__Slice_ANY(space, w_slice, w_length):
    length = space.getindex_w(w_length, space.w_OverflowError)
    start, stop, step = w_slice.indices3(space, length)
    return space.newtuple([space.wrap(start), space.wrap(stop),
                           space.wrap(step)])

# register all methods
from pypy.objspace.std import slicetype
register_all(vars(), slicetype)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.