问题描述
我写了Python中嵌入一个小的C程序。我正确设置它使用Py_Initialize()和Py_Finalize()和我能够运行或者使用PyRun_SimpleString或PyRun_SimpleFile脚本。但是,我不知道打印变量时,如何模仿Python的跨preTER的行为。
I have written a small C program that embeds Python. I'm setting it up correctly using Py_Initialize() and Py_Finalize(), and am able to run scripts either using PyRun_SimpleString or PyRun_SimpleFile. However, I don't know how mimic the behavior of Python's own interpreter when printing variables.
具体做法是:
a = (1, 2, 3)
print a
对我的作品罚款:它打印出(1,2,3)
Works fine for me: it prints out (1, 2, 3)
不过:
a = (1, 2, 3)
a
打印出什么都没有。在Python自身间preTER,这将打印出(1,2,3)为好。我怎样才能让我的code做什么用户期望并打印出价值?
Prints out nothing at all. In Python's own interpreter, this would print out (1, 2, 3) as well. How can I make my code do what users would expect and print out the value?
在此先感谢!
推荐答案
要运行跨preters互动循环,你应该使用功能的。否则,code的行为就好像它是写在一个Python脚本文件,而不是交互输入。
To run the interpreters interactive loop, you should use the function PyRun_InteractiveLoop()
. Otherwise, your code will behave as if it were written in a Python script file, not entered interactively.
修改:下面是一个简单的互动间preTER充分code:
Edit: Here's the full code of a simple interactive interpreter:
#include <Python.h>
int main()
{
Py_Initialize();
PyRun_InteractiveLoop(stdin, "<stdin>");
Py_Finalize();
}
EDIT2 :实现一个完整的互动间$ P $在GUI PTER是有点项目。可能得到它的权利的最简单的方法是写连接到一个伪终端设备一个基本的终端仿真器,并使用上述code在该设备上。这将自动获得所有细微之处的权利。
Edit2: Implementing a full interactive interpreter in a GUI is a bit of a project. Probably the easiest way to get it right is to write a basic terminal emulator connected to a pseudo-terminal device, and use the above code on that device. This will automatically get all subtleties right.
如果你的目标是不是一个全面的交互式编辑器,一个选择可能是使用 PyRun_String()
与 Py_single_input
作为起始令牌。这将允许你运行一些Python code作为在交互间preTER,如果该code恰好是一个前pression不计算为无
,其价值的重新presentation印 - 以课程标准输出。下面是一些例子code(不带错误检查简单):
If your aim isn't a full-blown interactive editor, an option might be to use PyRun_String()
with Py_single_input
as start token. This will allow you to run some Python code as in the interactive interpreter, and if that code happened to be a single expression that doesn't evaluate to None
, a representation of its value is printed -- to stdout of course. Here is some example code (without error checking for simplicity):
#include <Python.h>
int main()
{
PyObject *main, *d;
Py_Initialize();
main = PyImport_AddModule("__main__");
d = PyModule_GetDict(main);
PyRun_String("a = (1, 2, 3)", Py_single_input, d, d);
PyRun_String("a", Py_single_input, d, d);
Py_Finalize();
}
这将打印(1,2,3)
。
还有很多问题:
- 没有错误处理和追溯打印。
- 像在互动间preTER没有增量投入块命令。输入需要是完全的。
- 输出到标准输出。
- 如果输入的多行给出,没有打印。
要真正复制交互式间preTER的行为是不容易的。这就是为什么我最初的建议是写在你的GUI一个基本的终端仿真器,它应该不会太辛苦 - 或者甚至还有一个可用
To really replicate the behaviour of the interactive interpreter is not easy. That's why my initial recommendation was to write a basic terminal emulator in your GUI, which shouldn't be too hard -- or maybe there's even one available.
这篇关于在嵌入式Python间preTER打印一个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!