将参数传递给 PyRun_File(***) [英] Pass argument to PyRun_File(***)
本文介绍了将参数传递给 PyRun_File(***)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在用 C 和 Python 编写一些代码.
I am writing some code in C and in Python.
我有一个名为 sample.py
的 python 文件,它接受两个字符串参数.
I have a python file called sample.py
which accepts two string parameters.
我的 C 程序使用 PyRun_SimpleString()
调用 python 函数.这样我就无法传递任何参数.
My C program calls the python function using the PyRun_SimpleString()
. In this way I am not able to pass any parameters.
通过PyRun_SimpleString()
:
int main(int argc,char *argv[])
{
......
PyRun_SimpleString("import sample\nsample.mainfunc("argv[1]",'Isolated_domU_t')\n");
.....
}
如何使用PyRun_SimpleString(*)
传递参数argv[1]
?
推荐答案
要设置参数,请执行以下代码:
To set arguments do like the following code:
wchar_t** wargv = new wchar_t*[argc];
for(int i = 0; i < argc; i++)
{
wargv[i] = Py_DecodeLocale(argv[i], nullptr);
if(wargv[i] == nullptr)
{
return EXIT_FAILURE;
}
}
Py_Initialize();
// char* py=...
PySys_SetArgv(argc, wargv);
PyRun_SimpleString(py);
Py_Finalize();
for(int i = 0; i < argc; i++)
{
PyMem_RawFree(wargv[i]);
wargv[i] = nullptr;
}
delete[] wargv;
wargv = nullptr;
这篇关于将参数传递给 PyRun_File(***)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文