【发布时间】:2016-10-01 15:45:06
【问题描述】:
使用带有PyImport_Import() 的Python C API 从文件中导入Python 模块相对容易,但是我需要使用存储在字符串中的函数。有没有办法从字符串导入 python 模块(澄清:没有文件;代码在字符串中)还是我必须将字符串保存为临时文件?
【问题讨论】:
标签: python c python-c-api cross-language
使用带有PyImport_Import() 的Python C API 从文件中导入Python 模块相对容易,但是我需要使用存储在字符串中的函数。有没有办法从字符串导入 python 模块(澄清:没有文件;代码在字符串中)还是我必须将字符串保存为临时文件?
【问题讨论】:
标签: python c python-c-api cross-language
无需使用临时文件。使用此代码:
const char *MyModuleName = "blah";
const char *MyModuleCode = "print 'Hello world!'";
PyObject *pyModule = PyModule_New(MyModuleName);
// Set properties on the new module object
PyModule_AddStringConstant(pyModule, "__file__", "");
PyObject *localDict = PyModule_GetDict(pyModule); // Returns a borrowed reference: no need to Py_DECREF() it once we are done
PyObject *builtins = PyEval_GetBuiltins(); // Returns a borrowed reference: no need to Py_DECREF() it once we are done
PyDict_SetItemString(localDict, "__builtins__", builtins);
// Define code in the newly created module
PyObject *pyValue = PyRun_String(MyModuleCode, Py_file_input, localDict, localDict);
if (pyValue == NULL) {
// Handle error
}
else
Py_DECREF(pyValue);
这是取自真实商业应用程序的代码(我通过删除错误处理和其他不需要的细节对其进行了轻微修改)。
只需在MyModuleName 中设置想要的模块名称,在MyModuleCode 中设置Python 代码即可!
【讨论】:
如果我的理解是正确的,你可以使用PyImport_ImportModule,它需要一个const char* name来指定要导入的模块。
由于我的理解不正确:
通常最好将内容转储到 .py 文件,然后使用 PyRun_File 执行它们,但是,如果您有字符串并且想要使用这些字符串,我猜您可以使用Py_CompileString 将其编译为代码对象,然后将其提供给 PyEval_EvalCode 进行评估。
【讨论】:
我按照 Dimitirs 制定的策略取得了成功。
Py_Initialize();
PyObject *module = Py_CompileString(
// language: Python
R"EOT(
fake_policy = {
"maxConnections": 4,
"policyDir": "/tmp",
"enableVhostPolicy": True,
"enableVhostNamePatterns": False,
})EOT",
"test_module", Py_file_input);
REQUIRE(module != nullptr);
PyObject *pModuleObj = PyImport_ExecCodeModule("test_module", module);
REQUIRE(pModuleObj != nullptr);
// better to check with an if, use PyErr_Print() or such to read the error
PyObject *pAttrObj = PyObject_GetAttrString(pModuleObj, "fake_policy");
REQUIRE(pAttrObj != nullptr);
auto *entity = reinterpret_cast<qd_entity_t *>(pAttrObj);
REQUIRE(qd_entity_has(entity, "enableVhostNamePatterns")); // sanity check for the test input
// call Py_DecRef s
在写这篇文章时,我使用了https://awasu.com/weblog/embedding-python/calling-python-code-from-your-program/ 作为另一个参考。
【讨论】: