freeeyes 发表于 2015-12-31 08:48:47

c++调用python函数

因为工作需要,调用Python脚本,在这里记录一下。
1.当取多个返回值时,如下:http://www.cppblog.com/Images/OutliningIndicators/None.gifdef mix(a, b) :
http://www.cppblog.com/Images/OutliningIndicators/None.gif    r1 = a + b
http://www.cppblog.com/Images/OutliningIndicators/None.gif    r2 = a - b
http://www.cppblog.com/Images/OutliningIndicators/None.gif    return (r1, r2) # (7,3)

c++代码如下:http://www.cppblog.com/Images/OutliningIndicators/None.gif#include "python.h"
http://www.cppblog.com/Images/OutliningIndicators/None.gifint _tmain(int argc, _TCHAR* argv[])
http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif{
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif string filename = "cal"; // cal.py
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif string methodname_mix = "mix"; // function name
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif Py_Initialize();
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif // load the module
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif PyObject * pyFileName = PyString_FromString(filename.c_str());
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif PyObject * pyMod = PyImport_Import(pyFileName);
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif // load the function
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif PyObject * pyFunc_mix = PyObject_GetAttrString(pyMod, methodname_mix.c_str());
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif // test the function is callable
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif if (pyFunc_mix && PyCallable_Check(pyFunc_mix))
http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif {
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif PyObject * pyParams = PyTuple_New(2);
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif PyTuple_SetItem(pyParams, 0, Py_BuildValue("i", 5));
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif PyTuple_SetItem(pyParams, 1, Py_BuildValue("i", 2));
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif // ok, call the function
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif int r1 = 0, r2 = 0;
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif PyObject * pyValue = PyObject_CallObject(pyFunc_mix, pyParams);
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif PyArg_ParseTuple(pyValue, "i|i", &r1, &r2);
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif if (pyValue)
http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif {
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif printf("%d,%d\n", r1, r2); //output is 7,3
http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif }
http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif }
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif Py_Finalize();
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif return 0;
http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif}

2.如果返回一个值时:http://www.cppblog.com/Images/OutliningIndicators/None.gifimport string
http://www.cppblog.com/Images/OutliningIndicators/None.gif
http://www.cppblog.com/Images/OutliningIndicators/None.gifdef AddMult(a, b):
http://www.cppblog.com/Images/OutliningIndicators/None.gif c = a + b
http://www.cppblog.com/Images/OutliningIndicators/None.gif print c
http://www.cppblog.com/Images/OutliningIndicators/None.gif return c


c++代码如下: http://www.cppblog.com/Images/OutliningIndicators/None.gifPyObject* pModule =NULL;
http://www.cppblog.com/Images/OutliningIndicators/None.gif PyObject* pFunc = NULL;
http://www.cppblog.com/Images/OutliningIndicators/None.gif PyObject* pArgs = NULL;
http://www.cppblog.com/Images/OutliningIndicators/None.gif PyObject* pRet = NULL;
http://www.cppblog.com/Images/OutliningIndicators/None.gif
http://www.cppblog.com/Images/OutliningIndicators/None.gif Py_Initialize();
http://www.cppblog.com/Images/OutliningIndicators/None.gif
http://www.cppblog.com/Images/OutliningIndicators/None.gif PyRun_SimpleString("import sys");
http://www.cppblog.com/Images/OutliningIndicators/None.gif PyRun_SimpleString("sys.path.append('./')");
http://www.cppblog.com/Images/OutliningIndicators/None.gif
http://www.cppblog.com/Images/OutliningIndicators/None.gif pModule = PyImport_ImportModule("httpsend");
http://www.cppblog.com/Images/OutliningIndicators/None.gif pFunc = PyObject_GetAttrString(pModule, "AddMult");
http://www.cppblog.com/Images/OutliningIndicators/None.gif
http://www.cppblog.com/Images/OutliningIndicators/None.gif //pArgs = Py_BuildValue("s, s", "This is ", "a python code");
http://www.cppblog.com/Images/OutliningIndicators/None.gif
http://www.cppblog.com/Images/OutliningIndicators/None.gif pArgs = Py_BuildValue("ii", 12,34);
http://www.cppblog.com/Images/OutliningIndicators/None.gif
http://www.cppblog.com/Images/OutliningIndicators/None.gif pRet = PyObject_CallObject(pFunc,pArgs);
http://www.cppblog.com/Images/OutliningIndicators/None.gif int pyResult=0,pyResult2 = 0;
http://www.cppblog.com/Images/OutliningIndicators/None.gif char *str;
http://www.cppblog.com/Images/OutliningIndicators/None.gif int argRet = PyArg_Parse(pRet,"i", &pyResult);
http://www.cppblog.com/Images/OutliningIndicators/None.gif if (argRet)
http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif {
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif printf("yes!\n");
http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif }
http://www.cppblog.com/Images/OutliningIndicators/None.gif else
http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif {
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif //错误
http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif printf("error!\n");
http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif }
http://www.cppblog.com/Images/OutliningIndicators/None.gif
http://www.cppblog.com/Images/OutliningIndicators/None.gif if(pModule)
http://www.cppblog.com/Images/OutliningIndicators/None.gif Py_DECREF(pModule);
http://www.cppblog.com/Images/OutliningIndicators/None.gif if(pFunc)
http://www.cppblog.com/Images/OutliningIndicators/None.gif Py_DECREF(pFunc);
http://www.cppblog.com/Images/OutliningIndicators/None.gif if(pArgs)
http://www.cppblog.com/Images/OutliningIndicators/None.gif Py_DECREF(pArgs);
http://www.cppblog.com/Images/OutliningIndicators/None.gif if(pRet)
http://www.cppblog.com/Images/OutliningIndicators/None.gif Py_DECREF(pRet);
http://www.cppblog.com/Images/OutliningIndicators/None.gif

页: [1]
查看完整版本: c++调用python函数