传递Tuple嵌入式Python
Passing Tuple Embedded Python
所以我正在编写一个C++应用程序,它使用对Python的嵌入式调用来运行一些计算。我有在指定目录中运行任何脚本的代码,以及在C++应用程序和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,那么它就完美地工作了。
我似乎不知道我做错了什么,把一个以上的参数传递给我的脚本。这是我试图从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()
任何帮助都将不胜感激!
我发现了我遇到的问题。在我的函数中,我试图访问命令行参数,而在编写函数时,我指定它们是传递参数的——我没有这样做。
将来,对于任何发现嵌入式Python有问题的人来说,我发现Python-C API中有一个非常有用的错误部分,如果PyErr_Occurred()为真,PyErr_Print()将错误打印到stderr。
我要么需要修复传递的实际参数,要么(我最终使用的是)在使用"PyObject_CallObject"调用函数并通过在sys.argv.
相关文章:
- 为什么开发人员将C / C ++用于嵌入式系统,而不是像Python这样的高级语言与C相比?
- 从嵌入式解释器捕获 python 窗口输出C++
- 如何强制嵌入式python使用与命令行相同的sys.path?
- c++ 上的嵌入式 python.更改调试器时出错
- 多线程嵌入式 Python.这是对的吗?
- C++ 中的嵌入式 Python 无法执行 pygame
- 嵌入式python如何调用C++类的函数?
- import_array在嵌入式Python C API的调试版本中不起作用
- C++中的嵌入式python:importerror import numpy.core.multiarray
- 估计C 中嵌入式Python脚本的进度
- 程序在Windows中的DLL边界上使用嵌入式Python/C 代码崩溃
- 嵌入式Python问题
- 在嵌入式Python C 应用中导入Numpy
- 调度嵌入式 Python 进程
- 通过嵌入式 Python 调用C++代码
- C++中带有嵌入式python模块的多线程
- 如何使用C++的字节码优化初始化嵌入式 Python 解释器
- 将交互式控制台附加到嵌入式python脚本
- 向python嵌入式解释器公开c++类实例
- Clion 错误 cmake python 嵌入式