Run an python method from C++
Mark Doberenz
mark.doberenz at righthemisphere.com
Tue Nov 30 17:59:21 EST 2004
--- I'm trying to run a method in a python file called pyFile.py...
class dc:
def proc1(var1):
print "Proc1 was run:",var1
def main():
pass
if __name__ == "__main__":
main()
--- and here's my c program:
int main(){
printf("Hello World!\n");
PyObject *mainModule,*globals,*pyRunFile,*runMethod;
FILE *pythonFileP;
Py_Initialize();
pyProxy *dc = new pyProxy();
char *filename = "pyFile.py";
pythonFileP = fopen(filename,"r+");
if (pythonFileP != NULL){
mainModule = PyImport_AddModule("__main__");
globals = PyModule_GetDict(mainModule);
pyRunFile =
PyRun_File(pythonFileP,filename,Py_file_input,globals,globals);
runMethod = PyObject_CallMethod(pyRunFile,"proc1","s","Blah");
fclose(pythonFileP);
Py_Finalize();
}else{
printf("File not opened!\n");
}
return 0;
}
--- It's crashing when I try to do the PyObject_CallMethod. How do I
get at this method? I'm not sure what the python file needs to look
like either. I just put it in the class, but that didn't help either.
Thanks for any help!
Mark
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20041201/39a47da3/attachment.html>
More information about the Python-list
mailing list