| 1 |
"""Slice object""" |
| 2 |
|
| 3 |
import sys |
| 4 |
from pypy.interpreter import gateway |
| 5 |
from pypy.interpreter.baseobjspace import W_Root |
| 6 |
from pypy.interpreter.error import OperationError, oefmt |
| 7 |
from pypy.interpreter.typedef import GetSetProperty, TypeDef |
| 8 |
from rpython.rlib.objectmodel import specialize |
| 9 |
from rpython.rlib import jit |
| 10 |
|
| 11 |
|
| 12 |
class W_SliceObject(W_Root): |
| 13 |
_immutable_fields_ = ['w_start', 'w_stop', 'w_step'] |
| 14 |
|
| 15 |
def __init__(self, w_start, w_stop, w_step): |
| 16 |
assert w_start is not None |
| 17 |
assert w_stop is not None |
| 18 |
assert w_step is not None |
| 19 |
self.w_start = w_start |
| 20 |
self.w_stop = w_stop |
| 21 |
self.w_step = w_step |
| 22 |
|
| 23 |
def unwrap(w_slice, space): |
| 24 |
return slice(space.unwrap(w_slice.w_start), space.unwrap(w_slice.w_stop), space.unwrap(w_slice.w_step)) |
| 25 |
|
| 26 |
def unpack(w_slice, space): |
| 27 |
if space.is_w(w_slice.w_step, space.w_None): |
| 28 |
step = 1 |
| 29 |
else: |
| 30 |
step = _eval_slice_index(space, w_slice.w_step) |
| 31 |
if step == 0: |
| 32 |
raise oefmt(space.w_ValueError, "slice step cannot be zero") |
| 33 |
if space.is_w(w_slice.w_start, space.w_None): |
| 34 |
if step < 0: |
| 35 |
start = sys.maxint |
| 36 |
else: |
| 37 |
start = 0 |
| 38 |
else: |
| 39 |
start = _eval_slice_index(space, w_slice.w_start) |
| 40 |
if space.is_w(w_slice.w_stop, space.w_None): |
| 41 |
if step < 0: |
| 42 |
stop = -sys.maxint-1 |
| 43 |
else: |
| 44 |
stop = sys.maxint |
| 45 |
else: |
| 46 |
stop = _eval_slice_index(space, w_slice.w_stop) |
| 47 |
return start, stop, step |
| 48 |
|
| 49 |
@staticmethod |
| 50 |
def adjust_indices(start, stop, step, length): |
| 51 |
if start < 0: |
| 52 |
start += length |
| 53 |
if start < 0: |
| 54 |
if step < 0: |
| 55 |
start = -1 |
| 56 |
else: |
| 57 |
start = 0 |
| 58 |
elif start >= length: |
| 59 |
if step < 0: |
| 60 |
start = length - 1 |
| 61 |
else: |
| 62 |
start = length |
| 63 |
|
| 64 |
if stop < 0: |
| 65 |
stop += length |
| 66 |
if stop < 0: |
| 67 |
if step < 0: |
| 68 |
stop = -1 |
| 69 |
else: |
| 70 |
stop = 0 |
| 71 |
elif stop >= length: |
| 72 |
if step < 0: |
| 73 |
stop = length - 1 |
| 74 |
else: |
| 75 |
stop = length |
| 76 |
if (step < 0 and stop >= start) or (step > 0 and start >= stop): |
| 77 |
slicelength = 0 |
| 78 |
elif step < 0: |
| 79 |
slicelength = (stop - start + 1) / step + 1 |
| 80 |
else: |
| 81 |
slicelength = (stop - start - 1) / step + 1 |
| 82 |
return start, stop, step, slicelength |
| 83 |
|
| 84 |
def indices3(w_slice, space, length): |
| 85 |
start, stop, step = w_slice.unpack(space) |
| 86 |
start, stop, step, slicelength = w_slice.adjust_indices(start, stop, step, length) |
| 87 |
return start, stop, step |
| 88 |
|
| 89 |
def indices4(w_slice, space, length): |
| 90 |
start, stop, step = w_slice.unpack(space) |
| 91 |
return w_slice.adjust_indices(start, stop, step, length) |
| 92 |
|
| 93 |
def __repr__(self): |
| 94 |
return "<W_SliceObject(%r, %r, %r)>" % ( |
| 95 |
self.w_start, self.w_stop, self.w_step) |
| 96 |
|
| 97 |
@staticmethod |
| 98 |
def descr__new__(space, w_slicetype, args_w): |
| 99 |
from pypy.objspace.std.sliceobject import W_SliceObject |
| 100 |
w_start = space.w_None |
| 101 |
w_stop = space.w_None |
| 102 |
w_step = space.w_None |
| 103 |
if len(args_w) == 1: |
| 104 |
w_stop, = args_w |
| 105 |
elif len(args_w) == 2: |
| 106 |
w_start, w_stop = args_w |
| 107 |
elif len(args_w) == 3: |
| 108 |
w_start, w_stop, w_step = args_w |
| 109 |
elif len(args_w) > 3: |
| 110 |
raise oefmt(space.w_TypeError, "slice() takes at most 3 arguments") |
| 111 |
else: |
| 112 |
raise oefmt(space.w_TypeError, "slice() takes at least 1 argument") |
| 113 |
w_obj = space.allocate_instance(W_SliceObject, w_slicetype) |
| 114 |
W_SliceObject.__init__(w_obj, w_start, w_stop, w_step) |
| 115 |
return w_obj |
| 116 |
|
| 117 |
def descr_repr(self, space): |
| 118 |
return space.newtext("slice(%s, %s, %s)" % ( |
| 119 |
space.text_w(space.repr(self.w_start)), |
| 120 |
space.text_w(space.repr(self.w_stop)), |
| 121 |
space.text_w(space.repr(self.w_step)))) |
| 122 |
|
| 123 |
def descr__reduce__(self, space): |
| 124 |
from pypy.objspace.std.sliceobject import W_SliceObject |
| 125 |
assert isinstance(self, W_SliceObject) |
| 126 |
return space.newtuple2( |
| 127 |
space.type(self), |
| 128 |
space.newtuple([self.w_start, self.w_stop, self.w_step])) |
| 129 |
|
| 130 |
def descr_eq(self, space, w_other): |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
if space.is_w(self, w_other): |
| 135 |
return space.w_True |
| 136 |
if not isinstance(w_other, W_SliceObject): |
| 137 |
return space.w_NotImplemented |
| 138 |
if space.eq_w(self.w_start, w_other.w_start) and \ |
| 139 |
space.eq_w(self.w_stop, w_other.w_stop) and \ |
| 140 |
space.eq_w(self.w_step, w_other.w_step): |
| 141 |
return space.w_True |
| 142 |
else: |
| 143 |
return space.w_False |
| 144 |
|
| 145 |
def descr_ne(self, space, w_other): |
| 146 |
if space.is_w(self, w_other): |
| 147 |
return space.w_False |
| 148 |
if not isinstance(w_other, W_SliceObject): |
| 149 |
return space.w_NotImplemented |
| 150 |
if space.eq_w(self.w_start, w_other.w_start) and \ |
| 151 |
space.eq_w(self.w_stop, w_other.w_stop) and \ |
| 152 |
space.eq_w(self.w_step, w_other.w_step): |
| 153 |
return space.w_False |
| 154 |
else: |
| 155 |
return space.w_True |
| 156 |
|
| 157 |
def descr_lt(self, space, w_other): |
| 158 |
if space.is_w(self, w_other): |
| 159 |
return space.w_False |
| 160 |
if not isinstance(w_other, W_SliceObject): |
| 161 |
return space.w_NotImplemented |
| 162 |
if space.eq_w(self.w_start, w_other.w_start): |
| 163 |
if space.eq_w(self.w_stop, w_other.w_stop): |
| 164 |
return space.lt(self.w_step, w_other.w_step) |
| 165 |
else: |
| 166 |
return space.lt(self.w_stop, w_other.w_stop) |
| 167 |
else: |
| 168 |
return space.lt(self.w_start, w_other.w_start) |
| 169 |
|
| 170 |
def descr_indices(self, space, w_length): |
| 171 |
"""S.indices(len) -> (start, stop, stride) |
| 172 |
|
| 173 |
Assuming a sequence of length len, calculate the start and stop |
| 174 |
indices, and the stride length of the extended slice described by |
| 175 |
S. Out of bounds indices are clipped in a manner consistent with the |
| 176 |
handling of normal slices. |
| 177 |
""" |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
return app_indices(space, self.w_start, self.w_stop, |
| 183 |
self.w_step, w_length) |
| 184 |
|
| 185 |
@staticmethod |
| 186 |
def eval_slice_index(space, w_int): |
| 187 |
"""Helper for cpyext""" |
| 188 |
return _eval_slice_index(space, w_int) |
| 189 |
|
| 190 |
|
| 191 |
def slicewprop(name): |
| 192 |
def fget(space, w_obj): |
| 193 |
from pypy.objspace.std.sliceobject import W_SliceObject |
| 194 |
if not isinstance(w_obj, W_SliceObject): |
| 195 |
raise oefmt(space.w_TypeError, "descriptor is for 'slice'") |
| 196 |
return getattr(w_obj, name) |
| 197 |
return GetSetProperty(fget) |
| 198 |
|
| 199 |
W_SliceObject.typedef = TypeDef("slice", |
| 200 |
__doc__ = '''slice([start,] stop[, step]) |
| 201 |
|
| 202 |
Create a slice object. This is used for extended slicing (e.g. a[0:10:2]).''', |
| 203 |
__new__ = gateway.interp2app(W_SliceObject.descr__new__), |
| 204 |
__repr__ = gateway.interp2app(W_SliceObject.descr_repr), |
| 205 |
__hash__ = None, |
| 206 |
__reduce__ = gateway.interp2app(W_SliceObject.descr__reduce__), |
| 207 |
|
| 208 |
__eq__ = gateway.interp2app(W_SliceObject.descr_eq), |
| 209 |
__ne__ = gateway.interp2app(W_SliceObject.descr_ne), |
| 210 |
__lt__ = gateway.interp2app(W_SliceObject.descr_lt), |
| 211 |
|
| 212 |
start = slicewprop('w_start'), |
| 213 |
stop = slicewprop('w_stop'), |
| 214 |
step = slicewprop('w_step'), |
| 215 |
indices = gateway.interp2app(W_SliceObject.descr_indices), |
| 216 |
) |
| 217 |
W_SliceObject.typedef.acceptable_as_base_class = False |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
def _eval_slice_index(space, w_int): |
| 222 |
|
| 223 |
|
| 224 |
try: |
| 225 |
return space.getindex_w(w_int, None) |
| 226 |
except OperationError as err: |
| 227 |
if not err.match(space, space.w_TypeError): |
| 228 |
raise |
| 229 |
raise oefmt(space.w_TypeError, |
| 230 |
"slice indices must be integers or None or have an " |
| 231 |
"__index__ method") |
| 232 |
|
| 233 |
def adapt_lower_bound(space, size, w_index): |
| 234 |
index = _eval_slice_index(space, w_index) |
| 235 |
if index < 0: |
| 236 |
index = index + size |
| 237 |
if index < 0: |
| 238 |
index = 0 |
| 239 |
assert index >= 0 |
| 240 |
return index |
| 241 |
|
| 242 |
def unwrap_start_stop(space, size, w_start, w_end): |
| 243 |
if space.is_none(w_start): |
| 244 |
start = 0 |
| 245 |
else: |
| 246 |
start = adapt_lower_bound(space, size, w_start) |
| 247 |
|
| 248 |
if space.is_none(w_end): |
| 249 |
end = size |
| 250 |
assert end >= 0 |
| 251 |
else: |
| 252 |
end = adapt_lower_bound(space, size, w_end) |
| 253 |
return start, end |
| 254 |
|
| 255 |
def normalize_simple_slice(space, length, w_start, w_stop): |
| 256 |
"""Helper for the {get,set,del}slice implementations.""" |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
start = space.int_w(w_start) |
| 261 |
stop = space.int_w(w_stop) |
| 262 |
assert length >= 0 |
| 263 |
if start < 0: |
| 264 |
start = 0 |
| 265 |
|
| 266 |
|
| 267 |
if jit.isconstant(stop) and stop == sys.maxint: |
| 268 |
pass |
| 269 |
else: |
| 270 |
if stop < start: |
| 271 |
stop = start |
| 272 |
if stop <= length: |
| 273 |
return start, stop |
| 274 |
|
| 275 |
stop = length |
| 276 |
if jit.isconstant(start) and start == 0: |
| 277 |
pass |
| 278 |
elif start > stop: |
| 279 |
start = stop |
| 280 |
return start, stop |
| 281 |
|
| 282 |
|
| 283 |
app = gateway.applevel(""" |
| 284 |
from _operator import index |
| 285 |
|
| 286 |
def evaluate_slice_index(x): |
| 287 |
try: |
| 288 |
return index(x) |
| 289 |
except TypeError: |
| 290 |
raise TypeError("slice indices must be integers or " |
| 291 |
"None or have an __index__ method") |
| 292 |
|
| 293 |
def _getlongindices(start, stop, step, length): |
| 294 |
if step is None: |
| 295 |
step = 1 |
| 296 |
else: |
| 297 |
step = evaluate_slice_index(step) |
| 298 |
if step == 0: |
| 299 |
raise ValueError("slice step cannot be zero") |
| 300 |
|
| 301 |
# Find lower and upper bounds for start and stop. |
| 302 |
if step < 0: |
| 303 |
lower = -1 |
| 304 |
upper = length - 1 |
| 305 |
else: |
| 306 |
lower = 0 |
| 307 |
upper = length |
| 308 |
|
| 309 |
# Compute start. |
| 310 |
if start is None: |
| 311 |
start = upper if step < 0 else lower |
| 312 |
else: |
| 313 |
start = evaluate_slice_index(start) |
| 314 |
if start < 0: |
| 315 |
start += length |
| 316 |
if start < lower: |
| 317 |
start = lower |
| 318 |
else: |
| 319 |
if start > upper: |
| 320 |
start = upper |
| 321 |
|
| 322 |
# Compute stop. |
| 323 |
if stop is None: |
| 324 |
stop = lower if step < 0 else upper |
| 325 |
else: |
| 326 |
stop = evaluate_slice_index(stop) |
| 327 |
if stop < 0: |
| 328 |
stop += length |
| 329 |
if stop < lower: |
| 330 |
stop = lower |
| 331 |
else: |
| 332 |
if stop > upper: |
| 333 |
stop = upper |
| 334 |
|
| 335 |
return (start, stop, step) |
| 336 |
|
| 337 |
def indices(start, stop, step, length): |
| 338 |
length = index(length) |
| 339 |
if length < 0: |
| 340 |
raise ValueError("length should not be negative") |
| 341 |
return _getlongindices(start, stop, step, length) |
| 342 |
|
| 343 |
""", filename=__file__) |
| 344 |
|
| 345 |
app_indices = app.interphook("indices") |