所以我正在编写一个C++应用程序,它使用嵌入式调用来运行一些计算。 我有代码工作在指定目录中运行任何脚本,以及在C++应用程序和Python解释器之间传递和返回一个值。传递元组嵌入式Python
但是,我尝试将多个值作为参数传递给Python时遇到问题。 以下是我运行一个Python脚本代码:
double Script::run_script(string moduleName, string funcName, double *params,
int numberOfParams)
{
PyObject *pName, *pModule, *pDict, *pFunc;
PyObject *pArgs, *pValue;
int i;
// Set aside memory for the path to my scripts to add to sys.path
char dir[255];
memset(dir, 0, sizeof(dir));
/* ********************************************* */
sprintf(dir, "/home/bmalone/workspace/NumericalAnalysis/Testing"); //Put path into dir
/* ********************************************* */
Py_Initialize();
PyObject *dictModule = PyImport_GetModuleDict();
PyObject *sys = PyDict_GetItemString(dictModule, "sys");
// Sys NULL?
if (sys == NULL)
{
cerr << "Cannot find sys in dict!" << endl;
return -1;
}
dictModule = PyModule_GetDict(sys);
PyObject *path = PyDict_GetItemString(dictModule, "path");
PyObject *pPath = PyUnicode_FromString(dir);
PyList_Append(path, pPath);
if (pPath);
//cerr << "PATH > " << PyString_AsString(PyObject_Repr(path)) << endl;
else
cerr << "pPath IS NULL" << endl;
/* Import the module that contains the function/script */
pModule = PyImport_ImportModule(moduleName.c_str());
if (pModule);
//cerr << "pModule ~ " << PyString_AsString(PyObject_Repr(pModule)) << endl;
else
cerr << "pModule is NULL!!" << endl;
//cerr << "Getting dict for argv[1]..." << endl;
dictModule = PyModule_GetDict(pModule);
if (dictModule);
//cerr << "DictModule ~ " << PyString_AsString(PyObject_Repr(dictModule)) << endl;
else
cerr << "DictModule is NULL!!" << endl;
// CALL THE FUNCTION!
/*
* argv[1] is the MODULE name, but argv[2] SHOULD BE the function name!
*/
/* Get a PyObject referencing the function from the module 'pModule' */
//pFunc = PyObject_GetAttrString(pModule, funcName.c_str());
pFunc = PyDict_GetItemString(dictModule, funcName.c_str());
if (pFunc)
{
PyObject *objRep = PyObject_Repr(pFunc);
const char* str = PyString_AsString(objRep);
cerr << "Func is a real boy, too! - " << str << endl;
}
else
cerr << "pFunc is NULL!" << endl;
/* Setup PyArgs */
pArgs = PyTuple_New(numberOfParams);
// Add the data as doubles
cerr << "<parameters> ~ params size is " << numberOfParams << endl;
for (int i = 0; i < numberOfParams; i++)
{
if (params)
{
/* **************************** */
//pValue = PyDouble_AsDouble(params[i]);
pValue = PyFloat_FromDouble(params[i]);
/* **************************** */
cerr << " " << params[i] << endl;
PyObject* objRep = PyObject_Repr(pValue);
const char* strRep = PyString_AsString(objRep);
cerr << ">> pValue = " << strRep << endl;
PyTuple_SetItem(pArgs, i, pValue);
}
else
break;
}
cerr << "<Module> - " << moduleName << ", is null: " << (pModule == NULL) << endl;
cerr << "<Function> - " << funcName << ", is null: " << (pFunc == NULL) << endl;
// Get the retun value
cerr << "<Calling Function>" << endl;
//pValue = PyObject_CallFunction(pFunc, "f", 15.0); // **WORKS**
pValue = PyObject_CallObject(pFunc, pArgs); // **WORKS**
//pValue = PyObject_CallObject(pFunc, NULL);
//pValue = PyEval_CallObject(pFunc, NULL);
//pValue = PyObject_CallFunction(pFunc, NULL);
//pValue = PyObject_CallObject(pFunc, args);
//pValue = PyEval_CallObject(pFunc, args);
//cerr << "ANSWER ---> " << PyFloat_AsDouble(pValue) << endl;
return PyFloat_AsDouble(pValue);
Py_Finalize();
}
编辑: 在我原来的职位,我做了一个改变我忘了我做了,这是防止任何脚本运行错误。上述代码正确运行任何脚本,,但只有在pArgs具有--1--元素时才起作用。任何时候我尝试传递多个元素,即使是要打印出相同的脚本,脚本也不会运行。 但是,如果它只是1,那么它完美的作品。
我似乎无法弄清楚我做错了将多于1个参数传递给我的脚本。 这是剧本我想从C++运行(即1个参数的功能之一):
#!/usr/bin/python
'''
Created on Jul 12, 2012
@author: bmalone
'''
import sys
def doubleTwo(x, y):
print str(sys.argv)
print "Can haz doubling?\n"
if __name__ == '__main__':
doubleTwo()
任何帮助,将不胜感激!
对不起,如果我忽略了任何东西,但是您是否包含调用'Script :: run_script()'的C++代码?这个错误可能与你如何构造你以'params'传递的双精度数组有关。 – jogojapan 2012-07-13 03:22:35