c++ - 传递元组嵌入Python

标签 c++ python c python-c-api

所以我正在编写一个 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 那么它就可以完美工作。

我似乎无法弄清楚将超过 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() 为 true,PyErr_Print() 会将错误打印到 stderr。

我要么需要修复传递实际参数的问题,要么(我最终要做的)是在使用“PyObject_CallObject”调用函数之前使用“PySys_SetArgv”,并通过在 sys.argv 上建立索引来访问 Python 中所需的参数。

关于c++ - 传递元组嵌入Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11450808/

相关文章:

c++ - Poco C++ Websocket 服务器 - 防止它在 60 秒后关闭

c++ - 使用 TextureArray 优于一组纹理的性能优势?

python - 删除列表中的空白行和多余空间 - python

python - 无法提取所需的文本部分并丢弃某些元素中的其余部分

python - 如何使用 pandas 读取 .txt 数据列并将其提供给 TF

objective-c - 返回 C 结构数组

c - 使用 OpenCV 检测小圆圈(图像质量差)

c++ - 全屏中的 SDL OpenGL Alt-tab 具有不可预测的结果

c - 指向整数转换错误的不兼容指针

c++ - 成员访问运算符生成的左值/右值示例