[Numpy-discussion] Extending C with Python

Jialin Liu jalnliu at lbl.gov
Wed Jan 31 01:25:03 EST 2018

I'm extending C with python (which is opposite way of what people usually
do, extending python with C), I'm currently stuck in passing a C array to
python layer, could anyone plz advise?

I have a C buffer in my C code and want to pass it to a python function. In
the C code, I have:

npy_intp  dims [2];
> dims[0] = 10;
> dims[1] = 20;
> import_array();
> npy_intp m=2;
> PyObject * py_dims = PyArray_SimpleNewFromData(1, &m, NPY_INT16 ,(void
> *)dims ); // I also tried NPY_INT
> PyObject_CallMethod(pInstance, method_name, "O", py_dims);

In the Python code, I want to just print that array:

def f(self, dims):

   print ("np array:%d,%d"%(dims[0],dims[1]))

But it only prints the first number correctly, i.e., dims[0]. The second
number is always 0.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20180130/1f976ffc/attachment.html>

More information about the NumPy-Discussion mailing list