Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/magento/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python';s互动提示“&燃气轮机&燃气轮机&燃气轮机&引用;输出到?_Python_Python Interactive - Fatal编程技术网

Python';s互动提示“&燃气轮机&燃气轮机&燃气轮机&引用;输出到?

Python';s互动提示“&燃气轮机&燃气轮机&燃气轮机&引用;输出到?,python,python-interactive,Python,Python Interactive,我遇到了一个有点不寻常的情况。我正在尝试编写交互式控制台脚本(用于教学/测试目的),并尝试了以下操作: $ python > /dev/null Python 2.7.3 (v2.7.3:70274d53c1dd, Apr 9 2012, 20:52:43) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin Type "help", "copyright", "credits" or "license" for more inf

我遇到了一个有点不寻常的情况。我正在尝试编写交互式控制台脚本(用于教学/测试目的),并尝试了以下操作:

$ python > /dev/null
Python 2.7.3 (v2.7.3:70274d53c1dd, Apr  9 2012, 20:52:43) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> print 3
>>> 
3
未打印,因此其他所有内容显然都在
stderr
上。到现在为止,一直都还不错。然后我们重定向
stderr

$ python 2> /dev/null
>>> print 3
3
>>> 
在这两种情况下,如何打印提示


编辑:重定向
stdout
stderr
会导致完全不打印任何内容。因此,Python显然在“选择”一个
stdout
stderr
。有文件证明会发生这种情况吗?我无法理解Python源代码中是如何做到这一点的。

Python似乎在检查
stdout
是否是
tty

/* This is needed to handle the unlikely case that the
 * interpreter is in interactive mode *and* stdin/out are not
 * a tty.  This can happen, for example if python is run like
 * this: python -i < test1.py
 */
if (!isatty (fileno (sys_stdin)) || !isatty (fileno (sys_stdout)))
    rv = PyOS_StdioReadline (sys_stdin, sys_stdout, prompt);
else
    rv = (*PyOS_ReadlineFunctionPointer)(sys_stdin, sys_stdout,
                                         prompt);
/*这是处理
*解释器处于交互模式*和*stdin/out不处于交互模式
*一辆出租车。这可能发生,例如,如果python像
*这个:python-i
第194行附近
Parser/myreadline.c
中的源代码


解释器可能在启动时导入
readline
模块,在这种情况下
PyOS\u ReadlineFunctionPointer
将设置为
call\u readline
,它使用
readline
库。特别是它呼吁。这个函数的文档没有说明提示符的打印位置,但它可能会检查
stdout
/
stderr
是否为
tty
s.

哈,这很奇怪。也许它会检查isatty
?这是我的猜测,但我似乎不知道Python在哪里真正做到了这一点。最奇怪的是,
PyOS\u StdioReadline
,这个似乎负责读取输入的函数,被硬编码为将提示打印到
stderr
。如果将
stderr
重定向到
stdout
,如第二个示例所示,我们应该看到横幅。使用专为Python教学设计的工具可能更容易,例如。不过,就在上面,它似乎将
PyOS\u ReadlineFunctionPointer
设置为
PyOS\u StdioReadline
(除了在虚拟机上,我肯定不在虚拟机上)@nneonneo是的,我有点困惑,但这是我能找到的唯一可以解释您看到的行为的参考。您有readline库吗?因为在文件开头它提到它在可用时使用它。@nneonneo它只在它不是
NULL
时设置它。如果解释器导入e> readline在启动过程中,
readline
模块将设置其功能(参见
Modules/readline.c
中的init函数),因此,
PyOS\u-Readline
将不会使用
PyOS\u-StdioReadline
。啊哈!我错过了那个细节。我想这意味着Python将提示符输出到
stderr
只是作为一个后备…很好!谢谢你的回答。