I am running to some problems and would like some help. I have a piece code, which is used to embed a python script. This python script contains a function which will expect to receive an array as an argument (in this case I am using numpy array within the python script).
I would like to know how can I pass an array from C to the embedded python script as an argument for the function within the script. More specifically can someone show me a simple example of this.
I am running to some problems and would like some help. I have a
Share
Really, the best answer here is probably to use
numpyarrays exclusively, even from your C code. But if that’s not possible, then you have the same problem as any code that shares data between C types and Python types.In general, there are at least five options for sharing data between C and Python:
listor other object to pass.__getitem__, etc.).intptr_t, or to explicitctypestype, or just leave it un-cast; then usectypeson the Python side to access it.const char *and pass it as astr(or, in Py3,bytes), and usestructorctypeson the Python side to access it.bufferprotocol, and again usestructorctypeson the Python side.In your case, you want to use
numpy.arrays in Python. So, the general cases become:numpy.arrayto pass.ctypesto get it into a type thatnumpycan convert into an array.const char *and pass it as astr(or, in Py3,bytes), which is already a type thatnumpycan convert into an array.bufferprotocol, and which again I believenumpycan convert directly.For 1, here’s how to do it with a
list, just because it’s a very simple example (and I already wrote it…):And here’s the
numpy.arrayequivalent (assuming you can rely on the Carraynot to be deleted—see Creating arrays in the docs for more details on your options here):At any rate, however you do this, you will end up with something that looks like a
PyObject *from C (and has a single refcount), so you can pass it as a function argument, while on the Python side it will look like anumpy.array,list,bytes, or whatever else is appropriate.Now, how do you actually pass function arguments? Well, the sample code in Pure Embedding that you referenced in your comment shows how to do this, but doesn’t really explain what’s going on. There’s actually more explanation in the extending docs than the embedding docs, specifically, Calling Python Functions from C. Also, keep in mind that the standard library source code is chock full of examples of this (although some of them aren’t as readable as they could be, either because of optimization, or just because they haven’t been updated to take advantage of new simplified C API features).
Skip the first example about getting a Python function from Python, because presumably you already have that. The second example (and the paragraph right about it) shows the easy way to do it: Creating an argument tuple with
Py_BuildValue. So, let’s say we want to call a function you’ve got stored inmyfuncwith the listmylistreturned by thatmakelistfunction above. Here’s what you do:You can skip the callable check if you’re sure you’ve got a valid callable object, of course. (And it’s usually better to check when you first get
myfunc, if appropriate, because you can give both earlier and better error feedback that way.)If you want to actually understand what’s going on, try it without
Py_BuildValue. As the docs say, the second argument to[PyObject_CallObject][6]is a tuple, andPyObject_CallObject(callable_object, args)is equivalent toapply(callable_object, args), which is equivalent tocallable_object(*args). So, if you wanted to callmyfunc(mylist)in Python, you have to turn that into, effectively,myfunc(*(mylist,))so you can translate it to C. You can construct atuplelike this:But usually,
Py_BuildValueis easier (especially if you haven’t already packed everything up as Python objects), and the intention in your code is clearer (just as usingPyArg_ParseTupleis simpler and clearer than using explicittuplefunctions in the other direction).So, how do you get that
myfunc? Well, if you’ve created the function from the embedding code, just keep the pointer around. If you want it passed in from the Python code, that’s exactly what the first example does. If you want to, e.g., look it up by name from a module or other context, the APIs for concrete types likePyModuleand abstract types likePyMappingare pretty simple, and it’s generally obvious how to convert Python code into the equivalent C code, even if the result is mostly ugly boilerplate.Putting it all together, let’s say I’ve got a C array of integers, and I want to
import mymoduleand call a functionmymodule.myfunc(mylist)that returns an int. Here’s a stripped-down example (not actually tested, and no error handling, but it should show all the parts):If you’re using C++, you probably want to look into some kind of scope-guard/janitor/etc. to handle all those
Py_DECREFcalls, especially once you start doing proper error handling (which usually means earlyreturn NULLcalls peppered through the function). If you’re using C++11 or Boost,unique_ptr<PyObject, Py_DecRef>may be all you need.But really, a better way to reduce all that ugly boilerplate, if you plan to do a lot of C<->Python communication, is to look at all of the familiar frameworks designed for improving extending Python—Cython, boost::python, etc. Even though you’re embedding, you’re effectively doing the same work as extending, so they can help in the same ways.
For that matter, some of them also have tools to help the embedding part, if you search around the docs. For example, you can write your main program in Cython, using both C code and Python code, and
cython --embed. You may want to cross your fingers and/or sacrifice some chickens, but if it works, it’s amazingly simple and productive. Boost isn’t nearly as trivial to get started, but once you’ve got things together, almost everything is done in exactly the way you’d expect, and just works, and that’s just as true for embedding as extending. And so on.