static int pyjlist_set_subscript(PyObject* self, PyObject* item, PyObject* value) { if (PyInt_Check(item)) { long i = (long) PyInt_AS_LONG(item); if (i < 0) { i += (long) PyObject_Size(self); } return pyjlist_setitem(self, (Py_ssize_t) i, value); } else if (PyLong_Check(item)) { long i = PyLong_AsLong(item); if (i == -1 && PyErr_Occurred()) { return -1; } if (i < 0) { i += (long) PyObject_Size(self); } return pyjlist_setitem(self, (Py_ssize_t) i, value); } 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 -1; } #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 -1; } #endif if (slicelength <= 0) { return 0; } else if (step != 1) { PyErr_SetString(PyExc_TypeError, "pyjlist slices must have step of 1"); return -1; } else { return pyjlist_setslice(self, start, stop, value); } } else { PyErr_SetString(PyExc_TypeError, "list indices must be integers, longs, or slices"); return -1; } }
static int pyjlist_set_subscript(PyObject* self, PyObject* item, PyObject* value) { if(PyInt_Check(item)) { long i = PyInt_AS_LONG(item); if (i < 0) i += (long) PyObject_Size(self); return pyjlist_setitem(self, (Py_ssize_t) i, value); } else if(PyLong_Check(item)) { long i = PyLong_AsLong(item); if (i == -1 && PyErr_Occurred()) return -1; if (i < 0) i += (long) PyObject_Size(self); return pyjlist_setitem(self, (Py_ssize_t) i, value); } 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 -1; } if(slicelength <= 0) { return 0; } else if(step != 1) { PyErr_SetString(PyExc_TypeError, "pyjlist slices must have step of 1"); return -1; } else { return pyjlist_setslice(self, start, stop, value); } } else { PyErr_SetString(PyExc_TypeError, "list indices must be integers, longs, or slices"); return -1; } }