2

Python を C++ アプリケーションに埋め込んでいます。

タイムスタンプを返す次の C++ コードを実行すると、正常に動作します。

Py_Initialize();    

std::string strModule = "time"; // module to be loaded
pName = PyString_FromString(strModule.c_str());
pModule = PyImport_Import(pName); // import the module

pDict = PyModule_GetDict(pModule); // get all the symbols in the module
pFunc = PyDict_GetItemString(pDict, "time"); // get the function we want to call

// Call the function and get the return in the pValue
pValue = PyObject_CallObject(pFunc, NULL);
if (pValue == NULL){
    printf('Something is wrong !');
    return 0;
}
printf("Return of python call : %d\n", PyInt_AsLong(pValue)); // I get the correct timestamp

Py_Finalize();

今私は取得したいsys.path。しかし、同様のコードでエラーがスローされます:

Py_Initialize();    

std::string strModule = "sys"; // module to be loaded
pName = PyString_FromString(strModule.c_str());
pModule = PyImport_Import(pName); // import the module

pDict = PyModule_GetDict(pModule); // get all the symbols in the module
pFunc = PyDict_GetItemString(pDict, "path"); // get the function we want to call

// Call the function and get the return in the pValue
pValue = PyObject_CallObject(pFunc, NULL);
if (pValue == NULL){
    printf('Something is wrong !'); // I end up here, why pValue is NULL?
    return 0;
}
printf("Return of python call : %d\n", PyInt_AsLong(pValue));

Py_Finalize();

問題は、変数であるのtime.time()に対し、関数呼び出しであると思います。sys.pathその場合:

  1. 変数の結果を取得する方法は?
  2. 結果 (この場合は a list) を文字列の配列など、C++ で意味のあるものに適切に変換するにはどうすればよいですか?

そうでない場合、どのように進めますか?私はPython 2.7.6を使用しています

ありがとう。

4

1 に答える 1