Calling Python from C code with subs as strings

Makhno mak at imakhno.freeserve.co.uk
Sun Apr 23 19:28:37 CEST 2000


Can somebody suggest why the following C/Python code does not work? It's
supposed to call a C function with a string argument that describes what
Python function to call. That C function then calls that Python function.

C:

void CallPythonSub(char *subname)
{
   if (subname[0]==0) return; //do check this....
   PyObject *moduleref=PyImport_ImportModule("__main__");
   PyObject *thesub=PyObject_GetAttrString(moduleref,subname);
   if (thesub==NULL) {printf("Error\n");return;}
   PyObject_CallFunction(thesub);
}



Python:

def thissub():
   print 'im working!'

CallPythonSub('thissub')



No Nulls are returned from the Py... calls. It just does nothing.





More information about the Python-list mailing list