def _convert_idx_params(space, w_self, w_start, w_stop): start = slicetype._Eval_SliceIndex(space, w_start) stop = slicetype._Eval_SliceIndex(space, w_stop) length = len(w_self.data) if start < 0: start += length if start < 0: start = 0 if stop < 0: stop += length if stop < 0: stop = 0 return start, stop, length
def indices3(w_slice, space, length): if space.is_w(w_slice.w_step, space.w_None): step = 1 else: step = _Eval_SliceIndex(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_SliceIndex(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_SliceIndex(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 tuple_index__Tuple_ANY_ANY_ANY(space, w_tuple, w_obj, w_start, w_stop): start = slicetype._Eval_SliceIndex(space, w_start) stop = slicetype._Eval_SliceIndex(space, w_stop) length = len(w_tuple.wrappeditems) if start < 0: start += length if start < 0: start = 0 if stop < 0: stop += length if stop < 0: stop = 0 for i in range(start, min(stop, length)): w_item = w_tuple.wrappeditems[i] if space.eq_w(w_item, w_obj): return space.wrap(i) raise OperationError(space.w_ValueError, space.wrap("tuple.index(x): x not in tuple"))
def indices3(w_slice, space, length): if space.is_w(w_slice.w_step, space.w_None): step = 1 else: step = _Eval_SliceIndex(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_SliceIndex(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_SliceIndex(space, w_slice.w_stop) if stop < 0: stop += length if stop < 0: stop = -1 elif stop > length: stop = length return start, stop, step