如何让我的Python C API返回一个值? [英] How do I make my Python C API return a value?
问题描述
我目前正在使用以下代码通过C ++访问我的Python文件:
std :: string Main( std :: string url)
{
char * paras [] = { 4, 5} ;
Py_Arg( test, 乘以,paras);
return ;
}
int Py_Arg( char * fileName, char * funcName, char * args [])
{
PyObject * pName,* pMod,* pDict,* pFunc,* pVal;
Py_Initialize();
pName = PyString_FromString(fileName);
pMod = PyImport_Import(pName);
pDict = PyModule_GetDict(pMod);
pFunc = PyDict_GetItemString(pDict,funcName);
int argc = std :: strlen(( char *)args);
// std :: cout<< ! << argc;
if (PyCallable_Check(pFunc))
{
PyObject * pArgs = PyTuple_New(argc - 1 跨度>);
for ( int i = 0 ; i< = argc - 1 ; i ++)
{
pVal = PyInt_FromLong(atoi(args [i]));
if (!pVal)
{
PyErr_Print();
return 1 ;
}
PyTuple_SetItem(pArgs,i,pVal);
}
pVal = PyObject_CallObject(pFunc,pArgs);
if (pArgs!= NULL)
{
Py_DECREF(pArgs);
}
if (pVal!= NULL)
{
std :: printf( 恢复通话:%d \ n,PyInt_AsLong(pVal));
Py_DECREF(pVal);
}
else
{
PyErr_Print();
}
}
else
{
PyErr_Print();
}
Py_DECREF(pMod);
Py_DECREF(pName);
Py_Finalize();
return 0 ;
}
这是Python代码:
def multiply(num1,num2):
c = num1 * num2
print''结果为:'',c
return
一切正常。但是,现在我希望代码返回整数产品而不是仅仅打印它。然而第二个我在我的Python代码的末尾添加return c,我在Visual Studio(2010)中得到以下错误:
Microsoft Visual Studio C运行时库在FunctTest.exe中检测到致命错误。
按Break调试程序或继续终止程序。
我该如何使用该功能将值返回到我的C ++应用程序?我知道它将涉及使用API进行转换,但我不知道从哪里开始。我在网上关注的教程仍然给了我这个错误。任何帮助,将不胜感激。提前谢谢!
我不是python pro,但我的理解是它应该按照你的建议工作。另外,根据文档:
def multiply(num1,num2):
multiply = num1 * num2
和
def multiply(num1,num2):
返回num1 * num2
I''m currently using the following code to access my Python files via C++:
std::string Main(std::string url)
{
char* paras[] = {"4", "5"};
Py_Arg("test", "multiply", paras);
return "";
}
int Py_Arg(char* fileName, char* funcName, char* args[])
{
PyObject *pName, *pMod, *pDict, *pFunc, *pVal;
Py_Initialize();
pName = PyString_FromString(fileName);
pMod = PyImport_Import(pName);
pDict = PyModule_GetDict(pMod);
pFunc = PyDict_GetItemString(pDict, funcName);
int argc = std::strlen((char*)args);
//std::cout << "!" << argc;
if(PyCallable_Check(pFunc))
{
PyObject *pArgs = PyTuple_New(argc - 1);
for(int i = 0 ; i <= argc - 1; i++)
{
pVal = PyInt_FromLong(atoi(args[i]));
if(!pVal)
{
PyErr_Print();
return 1;
}
PyTuple_SetItem(pArgs, i, pVal);
}
pVal = PyObject_CallObject(pFunc, pArgs);
if(pArgs != NULL)
{
Py_DECREF(pArgs);
}
if(pVal != NULL)
{
std::printf("Return of call: %d\n", PyInt_AsLong(pVal));
Py_DECREF(pVal);
}
else
{
PyErr_Print();
}
}
else
{
PyErr_Print();
}
Py_DECREF(pMod);
Py_DECREF(pName);
Py_Finalize();
return 0;
}
And here is the Python code:
def multiply(num1, num2): c = num1 * num2 print ''The result is:'', c return
Everything works fine. However, now I want the code to return an integer product instead of just printing it. Yet the second I add "return c" to the end of my Python code, I get the following error in Visual Studio (2010):
Microsoft Visual Studio C Runtime Library has detected a fatal error in FunctTest.exe.
Press Break to debug the program or Continue to terminate the program.
How can I have the function return a value to my C++ app? I know it will involve conversion using the API, but I have no idea where to start. The tutorials I followed online still gave me this error. Any help would be appreciated. Thanks in advance!
I am not a python pro but my understanding is that it should work as you suggest. Also, according to the docs:
def multiply(num1, num2): multiply = num1 * num2
and
def multiply(num1, num2): return num1 * num2
这篇关于如何让我的Python C API返回一个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!