def getslice__String_ANY_ANY(space, w_str, w_start, w_stop): s = w_str._value start, stop = normalize_simple_slice(space, len(s), w_start, w_stop) if start == stop: return W_StringObject.EMPTY else: return sliced(space, s, start, stop, w_str)
def descr_getslice(self, space, w_start, w_stop): selfvalue = self._val(space) start, stop = normalize_simple_slice(space, len(selfvalue), w_start, w_stop) if start == stop: return self._empty() else: return self._sliced(space, selfvalue, start, stop, self)
def getslice__List_ANY_ANY(space, w_list, w_start, w_stop): length = w_list.length() start, stop = normalize_simple_slice(space, length, w_start, w_stop) slicelength = stop - start if slicelength == 0: return make_empty_list(space) return w_list.getslice(start, stop, 1, stop - start)
def setslice__List_ANY_ANY_ANY(space, w_list, w_start, w_stop, w_sequence): length = len(w_list.wrappeditems) start, stop = normalize_simple_slice(space, length, w_start, w_stop) sequence2 = space.listview(w_sequence) items = w_list.wrappeditems _setitem_slice_helper(space, items, start, 1, stop-start, sequence2, empty_elem=None)
def getslice__RopeUnicode_ANY_ANY(space, w_uni, w_start, w_stop): node = w_uni._node length = node.length() start, stop = normalize_simple_slice(space, length, w_start, w_stop) sl = stop - start if sl == 0: return W_RopeUnicodeObject.EMPTY return W_RopeUnicodeObject(rope.getslice(node, start, stop, 1, sl))
def getslice__Rope_ANY_ANY(space, w_str, w_start, w_stop): node = w_str._node length = node.length() start, stop = normalize_simple_slice(space, length, w_start, w_stop) sl = stop - start if sl == 0: return W_RopeObject.EMPTY return W_RopeObject(rope.getslice(node, start, stop, 1, sl))
def getslice__RangeList_ANY_ANY(space, w_rangelist, w_start, w_stop): if w_rangelist.w_list is not None: return space.getslice(w_rangelist.w_list, w_start, w_stop) length = w_rangelist.length start, stop = normalize_simple_slice(space, length, w_start, w_stop) slicelength = stop - start assert slicelength >= 0 rangestart = w_rangelist.getitem_unchecked(start) rangestep = w_rangelist.step return W_RangeListObject(rangestart, rangestep, slicelength)
def getslice__StringSlice_ANY_ANY(space, w_str, w_start, w_stop): length = w_str.stop - w_str.start start, stop = normalize_simple_slice(space, length, w_start, w_stop) sl = stop - start if sl == 0: return W_StringObject.EMPTY else: s = w_str.str start = w_str.start + start stop = w_str.start + stop return W_StringSliceObject(s, start, stop)
def test_normalize_simple_slice(self): space = self.space w = space.wrap def getslice(length, start, stop): # returns range(length)[start:stop] but without special # support for negative start or stop return [i for i in range(length) if start <= i < stop] assert getslice(10, 2, 5) == [2, 3, 4] for length in range(5): for start in range(-2*length-2, 2*length+3): for stop in range(-2*length-2, 2*length+3): mystart, mystop = normalize_simple_slice(space, length, w(start), w(stop)) assert 0 <= mystart <= mystop <= length assert (getslice(length, start, stop) == getslice(length, mystart, mystop))
def delslice__List_ANY_ANY(space, w_list, w_start, w_stop): length = len(w_list.wrappeditems) start, stop = normalize_simple_slice(space, length, w_start, w_stop) _delitem_slice_helper(space, w_list.wrappeditems, start, 1, stop-start)
def getslice__Unicode_ANY_ANY(space, w_uni, w_start, w_stop): uni = w_uni._value start, stop = normalize_simple_slice(space, len(uni), w_start, w_stop) return W_UnicodeObject(uni[start:stop])
def getslice__List_ANY_ANY(space, w_list, w_start, w_stop): length = len(w_list.wrappeditems) start, stop = normalize_simple_slice(space, length, w_start, w_stop) return W_ListObject(w_list.wrappeditems[start:stop])
def delslice__List_ANY_ANY(space, w_list, w_start, w_stop): length = len(w_list.wrappeditems) start, stop = normalize_simple_slice(space, length, w_start, w_stop) _delitem_slice_helper(space, w_list, start, 1, stop - start)
def setslice__List_ANY_ANY_ANY(space, w_list, w_start, w_stop, w_sequence): length = len(w_list.wrappeditems) start, stop = normalize_simple_slice(space, length, w_start, w_stop) _setitem_slice_helper(space, w_list, start, 1, stop - start, w_sequence)
def getslice__Tuple_ANY_ANY(space, w_tuple, w_start, w_stop): length = len(w_tuple.wrappeditems) start, stop = normalize_simple_slice(space, length, w_start, w_stop) return space.newtuple(w_tuple.wrappeditems[start:stop])
def setslice__List_ANY_ANY_ANY(space, w_list, w_start, w_stop, w_iterable): length = w_list.length() start, stop = normalize_simple_slice(space, length, w_start, w_stop) sequence_w = space.listview(w_iterable) w_other = W_ListObject(space, sequence_w) w_list.setslice(start, 1, stop - start, w_other)
def setslice__List_ANY_ANY_List(space, w_list, w_start, w_stop, w_other): length = w_list.length() start, stop = normalize_simple_slice(space, length, w_start, w_stop) w_list.setslice(start, 1, stop-start, w_other)
def setslice__List_ANY_ANY_List(space, w_list, w_start, w_stop, w_other): length = w_list.length() start, stop = normalize_simple_slice(space, length, w_start, w_stop) w_list.setslice(start, 1, stop - start, w_other)
def delslice__List_ANY_ANY(space, w_list, w_start, w_stop): length = w_list.length() start, stop = normalize_simple_slice(space, length, w_start, w_stop) w_list.deleteslice(start, 1, stop - start)
def delslice__List_ANY_ANY(space, w_list, w_start, w_stop): length = w_list.length() start, stop = normalize_simple_slice(space, length, w_start, w_stop) w_list.deleteslice(start, 1, stop-start)
def setslice__List_ANY_ANY_ANY(space, w_list, w_start, w_stop, w_iterable): length = w_list.length() start, stop = normalize_simple_slice(space, length, w_start, w_stop) sequence_w = space.listview(w_iterable) w_other = W_ListObject(space, sequence_w) w_list.setslice(start, 1, stop-start, w_other)
def descr_getslice(self, space, w_start, w_stop): length = self.length() start, stop = normalize_simple_slice(space, length, w_start, w_stop) return space.newtuple(self.tolist()[start:stop])
def setslice__List_ANY_ANY_ANY(space, w_list, w_start, w_stop, w_sequence): length = len(w_list.wrappeditems) start, stop = normalize_simple_slice(space, length, w_start, w_stop) _setitem_slice_helper(space, w_list, start, 1, stop-start, w_sequence)