[Numpy-discussion] C-api to slicing?

Neal Becker ndbecker2@gmail....
Fri Feb 8 06:39:41 CST 2008

Robert Kern wrote:

> Neal Becker wrote:
>> Is there a C-api to array slicing?
> PyObject_GetItem(), PySlice_New(), and friends, for the most part.
I tried PySequence_GetItem on my array, and it seems the refcount isn't

inline object test_slice3 (object const& in_obj, int r) {
  if (!PyArray_Check (in_obj.ptr())) {
    throw std::runtime_error ("test_slice3: input must be numpy::array");
  PyArrayObject* ao = (PyArrayObject*)(in_obj.ptr());
  PyArrayObject* ro = (PyArrayObject*) PySequence_GetItem ((PyObject*)ao,
  return object (handle<PyArrayObject> (ro));

In the above, object is a boost::python::object, which is just a wrapper
around a PyArrayObject*.  I extract the PyArrayObject*.

Now test:
b = array ((2,3))
print sys.getrefcount (b)
c = test_slice3(a,0)
print sys.getrefcount (b), sys.getrefcount(c)

This prints:
2 2

Here's what it should do:

print sys.getrefcount (b)
c = b[0]
print sys.getrefcount (b), sys.getrefcount(c)
3 2

More information about the Numpy-discussion mailing list