Embedding Python in C

mistabean at gmail.com mistabean at gmail.com
Tue Jun 5 02:41:19 EDT 2007


On 5 Jun., 01:32, "Gabriel Genellina" <gagsl-... at yahoo.com.ar> wrote:
> En Mon, 04 Jun 2007 11:58:38 -0300, <mistab... at gmail.com> escribió:
>
>
>
>
>
> > Onwards to the problem, I have been having difficulty embedding a
> > python module into my C/C++ program. (just a test program before
> > moving on into the real thing). I have been making test runs using the
> > codes fromhttp://docs.python.org/ext/pure-embedding.htmlas a basic,
> > but modifiying it now as a function inside my C/C++ code.
>
> > Problem started when I started passing an array as an argument. The
> > module also need an array as an argument, but somehow I can't make
> > them to go pass the "input-error checking" of the module.
>
> > The code for argument building and calling are as follows:
>
> > void CallSnake(char ModName[], char FuncName[], double result[])
> > {
> >   ...
> >   /*Some operations to import modname, and preping FuncName, all is
> > ok*/
> >   ...
> >   /*Processing the result array and calling the function, problem
> > time*/
> >         pArgs = PyTuple_New(MAX_ELEMENT);
>
> Should check for a NULL return value.
>
> >         pArg = PyList_New(1);
>
> Same here.
>
> >         for (i = 0; i < MAX_ELEMENT; ++i)
> >         {
> >              pValue = Py_BuildValue("d", result[i]);
>
> I'd use PyFloat_FromDouble here.
>
> >              PyTuple_SetItem(pArgs, i, pValue);
>
> >              if (!(*pArgs).ob_refcnt)
>
> What do you expect from this? pArgs is a newly created tuple - unless you  
> DECREF it explicitely, ob_refcnt should never be 0. (Also, the -> operator  
> exists for exactly this usage).
>
> >          PyList_SetItem(pArg, 0, pArgs);
> >          pValue = PyObject_CallFunctionObjArgs(pFunc,pArg,NULL);
>
> You didn't show us how you got pFunc here. Just to make it clear, you are  
> calling pFunc with a single argument, which is a list that contains a  
> single element, which is a tuple containing exactly MAX_ELEMENT float  
> objects.
>
> > Traceback
> >        if x.ndim != 1; /*x is the input array, checking if it's a 1D*/
> > AttributeError: 'list' object has no attribute 'ndim'
>
> Python lists don't have a ndim attribute. Perhaps you want some kind of  
> Numeric array? (ndarray?)
>
> > I have been trying many call variations, but alas, I think the problem
> > lies in the list building process. I have no problems calling a non-
> > arrayed (albeit,still single) argument.
>
> Yes, it appears that you are building a plain list but your code is  
> expecting another kind of object. I'm unfamiliar with Numeric arrays, if  
> that is what you need; perhaps someone else can help, or ask again in a  
> Numeric-specific list.
>
> --
> Gabriel Genellina- Zitierten Text ausblenden -
>
> - Zitierten Text anzeigen -

yeah, i was looking at am array full of numbers. import numpy,scipy,
maybe pylab too while i was testing out the function over at a python
shell, make a linspace, sin it, and then call the function (which also
is from the scipy cookbook). Is numeric arrays (or ndarray) unlike
normal listing then? Since I started off programming in C, I am having
a bit of a confusion identifiying the different type of arrays,
listing and tuples and such... (since it's just "build an array of
type so and so, and pass it over" ^^;;)

the reason i didn't show pFunc is that because it is the result of
operations done to FuncName (ModName = module to be imported, FuncName
= function to be called), therefore i feel it would be a bit waste of
time/space to put in the pFunc function, since I didn't change the
operations concerning pFunc other than changing argv[1] to FuncName
from the link i gave (and not the core of my problem).

so yeah, maybe I am building and passing the array/calling the
function the wrong way... will have to take a look at numpy/numerical
specific C API and stuff now

thanks for your time and the help




More information about the Python-list mailing list