Ejemplo n.º 1
0
Archivo: pyjlist.c Proyecto: mrj0/jep
static PyObject* pyjlist_subscript(PyObject *self, PyObject *item)
{
    if (PyInt_Check(item)) {
        long i = (long) PyInt_AS_LONG(item);
        if (i < 0) {
            i += (long) PyObject_Size(self);
        }
        return pyjlist_getitem(self, (Py_ssize_t) i);
    } else if (PyLong_Check(item)) {
        long i = PyLong_AsLong(item);
        if (i == -1 && PyErr_Occurred()) {
            return NULL;
        }
        if (i < 0) {
            i += (long) PyObject_Size(self);
        }
        return pyjlist_getitem(self, (Py_ssize_t) i);
    } else if (PySlice_Check(item)) {
        Py_ssize_t start, stop, step, slicelength;

#if PY_MAJOR_VERSION >= 3
        if (PySlice_GetIndicesEx(item, PyObject_Size(self), &start, &stop, &step,
                                 &slicelength) < 0) {
            // error will already be set
            return NULL;
        }
#else
        /*
         * This silences a compile warning on PySlice_GetIndicesEx by casting
         * item.  Python fixed the method signature in 3.2 to take item as a
         * PyObject*
         */
        if (PySlice_GetIndicesEx((PySliceObject *) item, PyObject_Size(self), &start,
                                 &stop, &step, &slicelength) < 0) {
            // error will already be set
            return NULL;
        }
#endif

        if (slicelength <= 0) {
            return pyjlist_getslice(self, 0, 0);
        } else if (step != 1) {
            PyErr_SetString(PyExc_TypeError, "pyjlist slices must have step of 1");
            return NULL;
        } else {
            return pyjlist_getslice(self, start, stop);
        }
    } else {
        PyErr_SetString(PyExc_TypeError,
                        "list indices must be integers, longs, or slices");
        return NULL;
    }
}
Ejemplo n.º 2
0
static PyObject* pyjlist_subscript(PyObject *self, PyObject *item) {
    if(PyInt_Check(item)) {
        long i = PyInt_AS_LONG(item);
        if (i < 0)
            i += (long) PyObject_Size(self);
        return pyjlist_getitem(self, (Py_ssize_t) i);
    }
    else if(PyLong_Check(item)) {
        long i = PyLong_AsLong(item);
        if (i == -1 && PyErr_Occurred())
            return NULL;
        if (i < 0)
            i += (long) PyObject_Size(self);
        return pyjlist_getitem(self, (Py_ssize_t) i);
    } else if(PySlice_Check(item)) {
        Py_ssize_t start, stop, step, slicelength;
        /*
         * ignore compile warning on the next line, they fixed the
         * method signature in python 3.2
         */
        if(PySlice_GetIndicesEx(item, PyObject_Size(self), &start, &stop, &step, &slicelength) < 0) {
            // error will already be set
            return NULL;
        }

        if(slicelength <= 0) {
            return pyjlist_getslice(self, 0, 0);
        } else if(step != 1) {
            PyErr_SetString(PyExc_TypeError, "pyjlist slices must have step of 1");
            return NULL;
        } else {
            return pyjlist_getslice(self, start, stop);
        }
    } else {
        PyErr_SetString(PyExc_TypeError, "list indices must be integers, longs, or slices");
        return NULL;
    }
}