Embedding Python in C

mistabean at gmail.com mistabean at gmail.com
Mon Jun 4 10:58:38 EDT 2007


Hello,

first of all, I am a programming newbie, especially in python...

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 from http://docs.python.org/ext/pure-embedding.html as 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);
        pArg = PyList_New(1);
        for (i = 0; i < MAX_ELEMENT; ++i)
        {
             pValue = Py_BuildValue("d", result[i]);
             PyTuple_SetItem(pArgs, i, pValue);

             if (!(*pArgs).ob_refcnt)
             {
                    Py_DECREF(pArgs);
                    Py_DECREF(pModule);
                    fprintf(stderr, "Cannot convert argument\n");
                    return;
             }
         }

         PyList_SetItem(pArg, 0, pArgs);
         pValue = PyObject_CallFunctionObjArgs(pFunc,pArg,NULL);
   /*Some more checking and DECREFing occurs here*/
 }

Error I have been getting is:

Traceback
       if x.ndim != 1; /*x is the input array, checking if it's a 1D*/
AttributeError: 'list' object has no attribute 'ndim'

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.

Thanks in advance...




More information about the Python-list mailing list