PyObject * _PySlice_FromIndices(Py_ssize_t istart, Py_ssize_t istop) { PyObject *start, *end, *slice; start = PyInt_FromSsize_t(istart); if (!start) return NULL; end = PyInt_FromSsize_t(istop); if (!end) { Py_DECREF(start); return NULL; } slice = PySlice_New(start, end, NULL); Py_DECREF(start); Py_DECREF(end); return slice; }
static PyObject * slice_new(PyTypeObject *type, PyObject *args, PyObject *kw) { 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); }
slice_base::slice_base(PyObject* start, PyObject* stop, PyObject* step) : object(detail::new_reference( PySlice_New(start, stop, step))) { }
static PyObject *c2py(triqs::arrays::range const &r) { return PySlice_New(convert_to_python(r.first()), convert_to_python(r.last()), convert_to_python(r.step())); }
static PyObject *Py_FindObjects(PyObject *obj, PyObject *args) { PyArrayObject *input = NULL; PyObject *result = NULL, *tuple = NULL, *start = NULL, *end = NULL; PyObject *slc = NULL; int jj; #if PY_VERSION_HEX < 0x02050000 long max_label; #define FMT "l" #else npy_intp max_label; #define FMT "n" #endif npy_intp ii, *regions = NULL; if (!PyArg_ParseTuple(args, "O&" FMT, NI_ObjectToInputArray, &input, &max_label)) goto exit; #undef FMT if (max_label < 0) max_label = 0; if (max_label > 0) { if (input->nd > 0) { regions = (npy_intp*)malloc(2 * max_label * input->nd * sizeof(npy_intp)); } else { regions = (npy_intp*)malloc(max_label * sizeof(npy_intp)); } if (!regions) { PyErr_NoMemory(); goto exit; } } if (!NI_FindObjects(input, max_label, regions)) goto exit; result = PyList_New(max_label); if (!result) { PyErr_NoMemory(); goto exit; } for(ii = 0; ii < max_label; ii++) { npy_intp idx = input->nd > 0 ? 2 * input->nd * ii : ii; if (regions[idx] >= 0) { PyObject *tuple = PyTuple_New(input->nd); if (!tuple) { PyErr_NoMemory(); goto exit; } for(jj = 0; jj < input->nd; jj++) { #if PY_VERSION_HEX < 0x02060000 start = PyLong_FromLong(regions[idx + jj]); end = PyLong_FromLong(regions[idx + jj + input->nd]); #else start = PyLong_FromSsize_t(regions[idx + jj]); end = PyLong_FromSsize_t(regions[idx + jj + input->nd]); #endif if (!start || !end) { PyErr_NoMemory(); goto exit; } slc = PySlice_New(start, end, NULL); if (!slc) { PyErr_NoMemory(); goto exit; } Py_XDECREF(start); Py_XDECREF(end); start = end = NULL; PyTuple_SetItem(tuple, jj, slc); slc = NULL; } PyList_SetItem(result, ii, tuple); tuple = NULL; } else { Py_INCREF(Py_None); PyList_SetItem(result, ii, Py_None); } } Py_INCREF(result); exit: Py_XDECREF(input); Py_XDECREF(result); Py_XDECREF(tuple); Py_XDECREF(start); Py_XDECREF(end); Py_XDECREF(slc); if (regions) free(regions); if (PyErr_Occurred()) { Py_XDECREF(result); return NULL; } else { return result; } }
slice( Integer1 start, Integer2 stop, Integer3 stride) : boost::python::object( boost::python::detail::new_reference( PySlice_New( object(start).ptr(), object(stop).ptr(), object(stride).ptr()))) {}
static PyObject *Py_FindObjects(PyObject *obj, PyObject *args) { PyArrayObject *input = NULL; PyObject *result = NULL, *tuple = NULL, *start = NULL, *end = NULL; PyObject *slc = NULL; int jj; npy_intp max_label; npy_intp ii, *regions = NULL; if (!PyArg_ParseTuple(args, "O&n", NI_ObjectToInputArray, &input, &max_label)) goto exit; if (max_label < 0) max_label = 0; if (max_label > 0) { if (PyArray_NDIM(input) > 0) { regions = (npy_intp*)malloc(2 * max_label * PyArray_NDIM(input) * sizeof(npy_intp)); } else { regions = (npy_intp*)malloc(max_label * sizeof(npy_intp)); } if (!regions) { PyErr_NoMemory(); goto exit; } } if (!NI_FindObjects(input, max_label, regions)) goto exit; result = PyList_New(max_label); if (!result) { PyErr_NoMemory(); goto exit; } for(ii = 0; ii < max_label; ii++) { npy_intp idx = PyArray_NDIM(input) > 0 ? 2 * PyArray_NDIM(input) * ii : ii; if (regions[idx] >= 0) { PyObject *tuple = PyTuple_New(PyArray_NDIM(input)); if (!tuple) { PyErr_NoMemory(); goto exit; } for(jj = 0; jj < PyArray_NDIM(input); jj++) { start = PyLong_FromSsize_t(regions[idx + jj]); end = PyLong_FromSsize_t(regions[idx + jj + PyArray_NDIM(input)]); if (!start || !end) { PyErr_NoMemory(); goto exit; } slc = PySlice_New(start, end, NULL); if (!slc) { PyErr_NoMemory(); goto exit; } Py_XDECREF(start); Py_XDECREF(end); start = end = NULL; PyTuple_SetItem(tuple, jj, slc); slc = NULL; } PyList_SetItem(result, ii, tuple); tuple = NULL; } else { Py_INCREF(Py_None); PyList_SetItem(result, ii, Py_None); } } Py_INCREF(result); exit: Py_XDECREF(input); Py_XDECREF(result); Py_XDECREF(tuple); Py_XDECREF(start); Py_XDECREF(end); Py_XDECREF(slc); free(regions); if (PyErr_Occurred()) { Py_XDECREF(result); return NULL; } else { return result; } }