Where does Python's interactive prompt “>>>” output to?

天大地大妈咪最大 提交于 2019-12-05 00:40:45

It seems like python checks whether stdout is a 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);

Sourcecode from Parser/myreadline.c around line 194.

It's possible that the interpreter imports the readline module at startup, in which case PyOS_ReadlineFunctionPointer will be set to call_readline, which uses the readline library. In particular it calls rl_callback_handler_install. The documentation of this function doesn't state where the prompt is printed, but it's possible that it checks whether stdout/stderr are ttys.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!