summaryrefslogtreecommitdiff
path: root/Objects/sliceobject.c
diff options
context:
space:
mode:
authorMichael W. Hudson <mwh@python.net>2002-07-19 15:47:06 +0000
committerMichael W. Hudson <mwh@python.net>2002-07-19 15:47:06 +0000
commit2b7d014fa95d4a97f4006edb57e652d93840b878 (patch)
treee992dd611189136eb8fd8a869dd11d838a43ef71 /Objects/sliceobject.c
parent172b5d5f8a5db8ce5bc7e36fc1be7a54dcd4675c (diff)
downloadcpython-2b7d014fa95d4a97f4006edb57e652d93840b878.tar.gz
A few days ago, Guido said (in the thread "[Python-Dev] Python
version of PySlice_GetIndicesEx"): > OK. Michael, if you want to check in indices(), go ahead. Then I did what was needed, but didn't check it in. Here it is.
Diffstat (limited to 'Objects/sliceobject.c')
-rw-r--r--Objects/sliceobject.c35
1 files changed, 34 insertions, 1 deletions
diff --git a/Objects/sliceobject.c b/Objects/sliceobject.c
index a43644d18f..f2d84dad65 100644
--- a/Objects/sliceobject.c
+++ b/Objects/sliceobject.c
@@ -221,6 +221,39 @@ static PyMemberDef slice_members[] = {
{0}
};
+static PyObject*
+slice_indices(PySliceObject* self, PyObject* len)
+{
+ int ilen, start, stop, step, slicelength;
+
+ ilen = PyInt_AsLong(len);
+
+ if (ilen == -1 && PyErr_Occurred()) {
+ return NULL;
+ }
+
+ if (PySlice_GetIndicesEx(self, ilen, &start, &stop,
+ &step, &slicelength) < 0) {
+ return NULL;
+ }
+
+ return Py_BuildValue("(lll)", start, stop, step);
+}
+
+PyDoc_STRVAR(slice_indices_doc,
+"S.indices(len) -> (start, stop, stride)\n\
+\n\
+Assuming a sequence of length len, calculate the start and stop\n\
+indices, and the stride length of the extended slice described by\n\
+S. Out of bounds indices are clipped in a manner consistent with the\n\
+handling of normal slices.");
+
+static PyMethodDef slice_methods[] = {
+ {"indices", (PyCFuntion)slice_indices,
+ METH_O, slice_indices_doc},
+ {NULL, NULL}
+};
+
static int
slice_compare(PySliceObject *v, PySliceObject *w)
{
@@ -271,7 +304,7 @@ PyTypeObject PySlice_Type = {
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
- 0, /* tp_methods */
+ slice_methods, /* tp_methods */
slice_members, /* tp_members */
0, /* tp_getset */
0, /* tp_base */