Back to index

python3.2  3.2.2
Functions | Variables
sliceobject.c File Reference
#include "Python.h"
#include "structmember.h"

Go to the source code of this file.

Functions

static PyObjectellipsis_repr (PyObject *op)
PyObjectPySlice_New (PyObject *start, PyObject *stop, PyObject *step)
PyObject_PySlice_FromIndices (Py_ssize_t istart, Py_ssize_t istop)
int PySlice_GetIndices (PyObject *_r, Py_ssize_t length, Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step)
int PySlice_GetIndicesEx (PyObject *_r, Py_ssize_t length, Py_ssize_t *start, Py_ssize_t *stop, Py_ssize_t *step, Py_ssize_t *slicelength)
static PyObjectslice_new (PyTypeObject *type, PyObject *args, PyObject *kw)
 PyDoc_STRVAR (slice_doc,"slice([start,] stop[, step])\n\ \n\ Create a slice object. This is used for extended slicing (e.g. a[0:10:2]).")
static void slice_dealloc (PySliceObject *r)
static PyObjectslice_repr (PySliceObject *r)
static PyObjectslice_indices (PySliceObject *self, PyObject *len)
 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 PyObjectslice_reduce (PySliceObject *self)
 PyDoc_STRVAR (reduce_doc,"Return state information for pickling.")
static PyObjectslice_richcompare (PyObject *v, PyObject *w, int op)

Variables

PyTypeObject PyEllipsis_Type
PyObject _Py_EllipsisObject
static PyMemberDef slice_members []
static PyMethodDef slice_methods []
PyTypeObject PySlice_Type

Function Documentation

Definition at line 83 of file sliceobject.c.

{
    PyObject *start, *end, *slice;
    start = PyLong_FromSsize_t(istart);
    if (!start)
        return NULL;
    end = PyLong_FromSsize_t(istop);
    if (!end) {
        Py_DECREF(start);
        return NULL;
    }

    slice = PySlice_New(start, end, NULL);
    Py_DECREF(start);
    Py_DECREF(end);
    return slice;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static PyObject* ellipsis_repr ( PyObject op) [static]

Definition at line 20 of file sliceobject.c.

{
    return PyUnicode_FromString("Ellipsis");
}

Here is the call graph for this function:

PyDoc_STRVAR ( slice_doc  ,
"slice([start,] stop[, step])\n\\n\Create a slice object. This is used for extended slicing (e.g. a[0:10:2])."   
)
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."   
)
PyDoc_STRVAR ( reduce_doc  ,
"Return state information for pickling."   
)
int PySlice_GetIndices ( PyObject _r,
Py_ssize_t  length,
Py_ssize_t start,
Py_ssize_t stop,
Py_ssize_t step 
)

Definition at line 102 of file sliceobject.c.

{
    PySliceObject *r = (PySliceObject*)_r;
    /* XXX support long ints */
    if (r->step == Py_None) {
        *step = 1;
    } else {
        if (!PyLong_Check(r->step)) return -1;
        *step = PyLong_AsSsize_t(r->step);
    }
    if (r->start == Py_None) {
        *start = *step < 0 ? length-1 : 0;
    } else {
        if (!PyLong_Check(r->start)) return -1;
        *start = PyLong_AsSsize_t(r->start);
        if (*start < 0) *start += length;
    }
    if (r->stop == Py_None) {
        *stop = *step < 0 ? -1 : length;
    } else {
        if (!PyLong_Check(r->stop)) return -1;
        *stop = PyLong_AsSsize_t(r->stop);
        if (*stop < 0) *stop += length;
    }
    if (*stop > length) return -1;
    if (*start >= length) return -1;
    if (*step == 0) return -1;
    return 0;
}

Here is the call graph for this function:

int PySlice_GetIndicesEx ( PyObject _r,
Py_ssize_t  length,
Py_ssize_t start,
Py_ssize_t stop,
Py_ssize_t step,
Py_ssize_t slicelength 
)

Definition at line 134 of file sliceobject.c.

{
    PySliceObject *r = (PySliceObject*)_r;
    /* this is harder to get right than you might think */

    Py_ssize_t defstart, defstop;

    if (r->step == Py_None) {
        *step = 1;
    }
    else {
        if (!_PyEval_SliceIndex(r->step, step)) return -1;
        if (*step == 0) {
            PyErr_SetString(PyExc_ValueError,
                            "slice step cannot be zero");
            return -1;
        }
        /* Here *step might be -PY_SSIZE_T_MAX-1; in this case we replace it
         * with -PY_SSIZE_T_MAX.  This doesn't affect the semantics, and it
         * guards against later undefined behaviour resulting from code that
         * does "step = -step" as part of a slice reversal.
         */
        if (*step < -PY_SSIZE_T_MAX)
            *step = -PY_SSIZE_T_MAX;
    }

    defstart = *step < 0 ? length-1 : 0;
    defstop = *step < 0 ? -1 : length;

    if (r->start == Py_None) {
        *start = defstart;
    }
    else {
        if (!_PyEval_SliceIndex(r->start, start)) return -1;
        if (*start < 0) *start += length;
        if (*start < 0) *start = (*step < 0) ? -1 : 0;
        if (*start >= length)
            *start = (*step < 0) ? length - 1 : length;
    }

    if (r->stop == Py_None) {
        *stop = defstop;
    }
    else {
        if (!_PyEval_SliceIndex(r->stop, stop)) return -1;
        if (*stop < 0) *stop += length;
        if (*stop < 0) *stop = (*step < 0) ? -1 : 0;
        if (*stop >= length)
            *stop = (*step < 0) ? length - 1 : length;
    }

    if ((*step < 0 && *stop >= *start)
        || (*step > 0 && *start >= *stop)) {
        *slicelength = 0;
    }
    else if (*step < 0) {
        *slicelength = (*stop-*start+1)/(*step)+1;
    }
    else {
        *slicelength = (*stop-*start-1)/(*step)+1;
    }

    return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

PyObject* PySlice_New ( PyObject start,
PyObject stop,
PyObject step 
)

Definition at line 61 of file sliceobject.c.

{
    PySliceObject *obj = PyObject_New(PySliceObject, &PySlice_Type);

    if (obj == NULL)
        return NULL;

    if (step == NULL) step = Py_None;
    Py_INCREF(step);
    if (start == NULL) start = Py_None;
    Py_INCREF(start);
    if (stop == NULL) stop = Py_None;
    Py_INCREF(stop);

    obj->step = step;
    obj->start = start;
    obj->stop = stop;

    return (PyObject *) obj;
}

Here is the caller graph for this function:

static void slice_dealloc ( PySliceObject r) [static]

Definition at line 229 of file sliceobject.c.

static PyObject* slice_indices ( PySliceObject self,
PyObject len 
) [static]

Definition at line 251 of file sliceobject.c.

{
    Py_ssize_t ilen, start, stop, step, slicelength;

    ilen = PyNumber_AsSsize_t(len, PyExc_OverflowError);

    if (ilen == -1 && PyErr_Occurred()) {
        return NULL;
    }

    if (PySlice_GetIndicesEx((PyObject*)self, ilen, &start, &stop,
                             &step, &slicelength) < 0) {
        return NULL;
    }

    return Py_BuildValue("(nnn)", start, stop, step);
}

Here is the call graph for this function:

static PyObject* slice_new ( PyTypeObject type,
PyObject args,
PyObject kw 
) [static]

Definition at line 202 of file sliceobject.c.

{
    PyObject *start, *stop, *step;

    start = stop = step = NULL;

    if (!_PyArg_NoKeywords("slice()", kw))
        return NULL;

    if (!PyArg_UnpackTuple(args, "slice", 1, 3, &start, &stop, &step))
        return NULL;

    /* This swapping of stop and start is to maintain similarity with
       range(). */
    if (stop == NULL) {
        stop = start;
        start = NULL;
    }
    return PySlice_New(start, stop, step);
}

Here is the call graph for this function:

static PyObject* slice_reduce ( PySliceObject self) [static]

Definition at line 278 of file sliceobject.c.

{
    return Py_BuildValue("O(OOO)", Py_TYPE(self), self->start, self->stop, self->step);
}

Here is the call graph for this function:

static PyObject* slice_repr ( PySliceObject r) [static]

Definition at line 238 of file sliceobject.c.

{
    return PyUnicode_FromFormat("slice(%R, %R, %R)", r->start, r->stop, r->step);
}
static PyObject* slice_richcompare ( PyObject v,
PyObject w,
int  op 
) [static]

Definition at line 294 of file sliceobject.c.

{
    PyObject *t1;
    PyObject *t2;
    PyObject *res;

    if (!PySlice_Check(v) || !PySlice_Check(w)) {
        Py_INCREF(Py_NotImplemented);
        return Py_NotImplemented;
    }

    if (v == w) {
        /* XXX Do we really need this shortcut?
           There's a unit test for it, but is that fair? */
        switch (op) {
        case Py_EQ:
        case Py_LE:
        case Py_GE:
            res = Py_True;
            break;
        default:
            res = Py_False;
            break;
        }
        Py_INCREF(res);
        return res;
    }

    t1 = PyTuple_New(3);
    t2 = PyTuple_New(3);
    if (t1 == NULL || t2 == NULL)
        return NULL;

    PyTuple_SET_ITEM(t1, 0, ((PySliceObject *)v)->start);
    PyTuple_SET_ITEM(t1, 1, ((PySliceObject *)v)->stop);
    PyTuple_SET_ITEM(t1, 2, ((PySliceObject *)v)->step);
    PyTuple_SET_ITEM(t2, 0, ((PySliceObject *)w)->start);
    PyTuple_SET_ITEM(t2, 1, ((PySliceObject *)w)->stop);
    PyTuple_SET_ITEM(t2, 2, ((PySliceObject *)w)->step);

    res = PyObject_RichCompare(t1, t2, op);

    PyTuple_SET_ITEM(t1, 0, NULL);
    PyTuple_SET_ITEM(t1, 1, NULL);
    PyTuple_SET_ITEM(t1, 2, NULL);
    PyTuple_SET_ITEM(t2, 0, NULL);
    PyTuple_SET_ITEM(t2, 1, NULL);
    PyTuple_SET_ITEM(t2, 2, NULL);

    Py_DECREF(t1);
    Py_DECREF(t2);

    return res;
}

Here is the call graph for this function:


Variable Documentation

Initial value:

Definition at line 48 of file sliceobject.c.

Initial value:
 {
    PyVarObject_HEAD_INIT(&PyType_Type, 0)
    "ellipsis",                         
    0,                                  
    0,                                  
    0,                  
    0,                                  
    0,                                  
    0,                                  
    0,                                  
    ellipsis_repr,                      
    0,                                  
    0,                                  
    0,                                  
    0,                                  
    0,                                  
    0,                                  
    PyObject_GenericGetAttr,            
    0,                                  
    0,                                  
    Py_TPFLAGS_DEFAULT,                 
}

Definition at line 25 of file sliceobject.c.

Definition at line 349 of file sliceobject.c.

Initial value:

Definition at line 243 of file sliceobject.c.

Initial value:
 {
    {"indices",         (PyCFunction)slice_indices,
     METH_O,            slice_indices_doc},
    {"__reduce__",      (PyCFunction)slice_reduce,
     METH_NOARGS,       reduce_doc},
    {NULL, NULL}
}

Definition at line 285 of file sliceobject.c.