如何在 C 中创建和分配 Python 变量?
我已经初始化了 Python 环境,因为
Py_Initialize();
我没有将外部 Python 模块导入到环境中,并且一切正常。但是当我需要将 C 字符串传递到这个环境中时,我迷失了......
我正在考虑在环境中添加一个函数来分配变量,就像下面的代码一样。
char *str;
str="\
def assign_var(str):\
global string\
string = str";
PyRun_SimpleString(str);
然后在 C 中调用此函数并将转换后的 C 字符串作为参数传递。
我不认为我上面提到的所有都是解决问题的好方法......
我怎样才能做到这一点?
解决方案:
最后,这是在 Peter Mortensen 的帮助下找到的解决方案。 (感谢 Peter Mortensen!)
由于我初始化的 python 环境是一个纯粹的空环境(没有任何导入的模块)。我用
py_main = PyImport_AddModule("__main__");
获得主环境的挂钩。然后调用
PyModule_AddStringConstant(py_main, "string_name", str);
将C字符串带入python环境。
要验证一切是否已完成,只需尝试:
PyRun_SimpleString("print dir()");
PyRun_SimpleString("print string_name");
您将看到“string_name”字符串出现在 dir() 列表中,并通过 python 打印它!
I've initialized the Python environment by
Py_Initialize();
I have no external Python module imported into the environment and everything works well. But when I need to pass a C string into this environment, I am lost...
I was thinking to add a function in the environment to assign the variable like the following code do.
char *str;
str="\
def assign_var(str):\
global string\
string = str";
PyRun_SimpleString(str);
And then call this function in C and pass the converted C string as the arguments.
I don't think all I mentioned above is a good way solve the problem...
How can I make this work?
Solution:
Finally, here's the solution with Peter Mortensen's help. (Thanks Peter Mortensen!)
As the python environment I've initialized is a pure empty environment(without any imported modules). I use
py_main = PyImport_AddModule("__main__");
to get a hook to the main environment. and then call
PyModule_AddStringConstant(py_main, "string_name", str);
to bring the C string into the python environment.
To verify everything is done, just try:
PyRun_SimpleString("print dir()");
PyRun_SimpleString("print string_name");
and you'll see you "string_name" string appears in the dir() list and make it print by python!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这应该做你想要的:
http://docs.python.org/c- api/arg.html#Py_BuildValue
当然,如果您使用的是 Python 3(或将来使用它),在某些情况下您可能想要使用“y”而不是“s”并得到
bytes
对象而不是str
。更新:糟糕,我忘记了更简单的方法。
http://docs.python.org/c-api/string.html#PyString_FromString
(Python 3 中为
PyBytes_FromString()
。)您可能需要查看 http://docs.python.org/extending/embedding.html 了解更多信息。
您可能还想尝试以下其他内容。请参阅
http://docs.python.org/c-api/module.html# PyModule_AddObject
或者可能
http://docs.python.org/c-api/module.html#PyModule_AddStringConstant
对于前者,它会像这样
对于后者,就像
This should do what you want:
http://docs.python.org/c-api/arg.html#Py_BuildValue
Of course if you're using Python 3 (or use it in the future), there may be situations where you'd want to use "y" instead of "s" and get a
bytes
object rather than astr
.UPDATE: Woops, I forgot the even easier way of doing it.
http://docs.python.org/c-api/string.html#PyString_FromString
(It'd be
PyBytes_FromString()
in Python 3.)You might want to take a look at http://docs.python.org/extending/embedding.html for some more information.
Here's something else you might want to try. See
http://docs.python.org/c-api/module.html#PyModule_AddObject
Or possibly
http://docs.python.org/c-api/module.html#PyModule_AddStringConstant
With the former it'd be something like
And with the latter, something like