| 1 |
import sys |
| 2 |
from rpython.rtyper.lltypesystem import rffi, lltype |
| 3 |
from pypy.module.cpyext.api import ( |
| 4 |
cpython_api, cpython_struct, bootstrap_function, |
| 5 |
CANNOT_FAIL, Py_ssize_t, Py_ssize_tP, PyObjectFields, slot_function) |
| 6 |
from pypy.module.cpyext.pyobject import ( |
| 7 |
decref, PyObject, make_ref, make_typedescr) |
| 8 |
from pypy.module.cpyext.pyerrors import PyErr_BadInternalCall |
| 9 |
from pypy.interpreter.error import oefmt |
| 10 |
from pypy.objspace.std.sliceobject import W_SliceObject |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
PySliceObjectStruct = lltype.ForwardReference() |
| 16 |
PySliceObject = lltype.Ptr(PySliceObjectStruct) |
| 17 |
PySliceObjectFields = PyObjectFields + \ |
| 18 |
(("start", PyObject), ("step", PyObject), ("stop", PyObject), ) |
| 19 |
cpython_struct("PySliceObject", PySliceObjectFields, PySliceObjectStruct) |
| 20 |
|
| 21 |
@bootstrap_function |
| 22 |
def init_sliceobject(space): |
| 23 |
"Type description of PySliceObject" |
| 24 |
make_typedescr(W_SliceObject.typedef, |
| 25 |
basestruct=PySliceObject.TO, |
| 26 |
attach=slice_attach, |
| 27 |
dealloc=slice_dealloc) |
| 28 |
|
| 29 |
def slice_attach(space, py_obj, w_obj, w_userdata=None): |
| 30 |
""" |
| 31 |
Fills a newly allocated PySliceObject with the given slice object. The |
| 32 |
fields must not be modified. |
| 33 |
""" |
| 34 |
py_slice = rffi.cast(PySliceObject, py_obj) |
| 35 |
assert isinstance(w_obj, W_SliceObject) |
| 36 |
py_slice.c_start = make_ref(space, w_obj.w_start) |
| 37 |
py_slice.c_stop = make_ref(space, w_obj.w_stop) |
| 38 |
py_slice.c_step = make_ref(space, w_obj.w_step) |
| 39 |
|
| 40 |
@slot_function([PyObject], lltype.Void) |
| 41 |
def slice_dealloc(space, py_obj): |
| 42 |
"""Frees allocated PySliceObject resources. |
| 43 |
""" |
| 44 |
py_slice = rffi.cast(PySliceObject, py_obj) |
| 45 |
decref(space, py_slice.c_start) |
| 46 |
decref(space, py_slice.c_stop) |
| 47 |
decref(space, py_slice.c_step) |
| 48 |
from pypy.module.cpyext.object import _dealloc |
| 49 |
_dealloc(space, py_obj) |
| 50 |
|
| 51 |
|
| 52 |
@cpython_api([PyObject, PyObject, PyObject], PyObject) |
| 53 |
def PySlice_New(space, w_start, w_stop, w_step): |
| 54 |
"""Return a new slice object with the given values. The start, stop, and |
| 55 |
step parameters are used as the values of the slice object attributes of |
| 56 |
the same names. Any of the values may be NULL, in which case the |
| 57 |
None will be used for the corresponding attribute. Return NULL if |
| 58 |
the new object could not be allocated.""" |
| 59 |
if w_start is None: |
| 60 |
w_start = space.w_None |
| 61 |
if w_stop is None: |
| 62 |
w_stop = space.w_None |
| 63 |
if w_step is None: |
| 64 |
w_step = space.w_None |
| 65 |
return W_SliceObject(w_start, w_stop, w_step) |
| 66 |
|
| 67 |
@cpython_api([PyObject, Py_ssize_t, Py_ssize_tP, Py_ssize_tP, Py_ssize_tP, |
| 68 |
Py_ssize_tP], rffi.INT_real, error=-1) |
| 69 |
def PySlice_GetIndicesEx(space, w_slice, length, start_p, stop_p, step_p, |
| 70 |
slicelength_p): |
| 71 |
"""Usable replacement for PySlice_GetIndices(). Retrieve the start, |
| 72 |
stop, and step indices from the slice object slice assuming a sequence of |
| 73 |
length length, and store the length of the slice in slicelength. Out |
| 74 |
of bounds indices are clipped in a manner consistent with the handling of |
| 75 |
normal slices. |
| 76 |
|
| 77 |
Returns 0 on success and -1 on error with exception set.""" |
| 78 |
if not isinstance(w_slice, W_SliceObject): |
| 79 |
raise PyErr_BadInternalCall(space) |
| 80 |
start_p[0], stop_p[0], step_p[0], slicelength_p[0] = \ |
| 81 |
w_slice.indices4(space, length) |
| 82 |
return 0 |
| 83 |
|
| 84 |
@cpython_api([PyObject, Py_ssize_t, Py_ssize_tP, Py_ssize_tP, Py_ssize_tP], |
| 85 |
rffi.INT_real, error=-1) |
| 86 |
def PySlice_GetIndices(space, w_slice, length, start_p, stop_p, step_p): |
| 87 |
"""Retrieve the start, stop and step indices from the slice object slice, |
| 88 |
assuming a sequence of length length. Treats indices greater than |
| 89 |
length as errors. |
| 90 |
|
| 91 |
Returns 0 on success and -1 on error with no exception set (unless one of |
| 92 |
the indices was not None and failed to be converted to an integer, |
| 93 |
in which case -1 is returned with an exception set). |
| 94 |
|
| 95 |
You probably do not want to use this function. If you want to use slice |
| 96 |
objects in versions of Python prior to 2.3, you would probably do well to |
| 97 |
incorporate the source of PySlice_GetIndicesEx(), suitably renamed, |
| 98 |
in the source of your extension.""" |
| 99 |
if not isinstance(w_slice, W_SliceObject): |
| 100 |
raise PyErr_BadInternalCall(space) |
| 101 |
start_p[0], stop_p[0], step_p[0] = \ |
| 102 |
w_slice.indices3(space, length) |
| 103 |
return 0 |
| 104 |
|
| 105 |
@cpython_api([PyObject, Py_ssize_tP, Py_ssize_tP, Py_ssize_tP], |
| 106 |
rffi.INT_real, error=-1) |
| 107 |
def PySlice_Unpack(space, w_slice, start_p, stop_p, step_p): |
| 108 |
if not isinstance(w_slice, W_SliceObject): |
| 109 |
raise PyErr_BadInternalCall(space) |
| 110 |
|
| 111 |
if space.is_none(w_slice.w_step): |
| 112 |
step = 1 |
| 113 |
else: |
| 114 |
step = W_SliceObject.eval_slice_index(space, w_slice.w_step) |
| 115 |
if step == 0: |
| 116 |
raise oefmt(space.w_ValueError, "slice step cannot be zero") |
| 117 |
if step < -sys.maxint: |
| 118 |
step = -sys.maxint |
| 119 |
step_p[0] = step |
| 120 |
|
| 121 |
if space.is_none(w_slice.w_start): |
| 122 |
start = sys.maxint if step < 0 else 0 |
| 123 |
else: |
| 124 |
start = W_SliceObject.eval_slice_index(space, w_slice.w_start) |
| 125 |
start_p[0] = start |
| 126 |
|
| 127 |
if space.is_none(w_slice.w_stop): |
| 128 |
stop = -sys.maxint-1 if step < 0 else sys.maxint |
| 129 |
else: |
| 130 |
stop = W_SliceObject.eval_slice_index(space, w_slice.w_stop) |
| 131 |
stop_p[0] = stop |
| 132 |
|
| 133 |
return 0 |