Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where does Python's interactive prompt ">>>" output to?

I've run into a somewhat unusual situation. I'm trying to script the interactive console (for teaching/testing purposes), and I tried the following:

$ 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 isn't printed, so clearly everything else was on stderr. So far so good. But then we redirect stderr:

$ python 2> /dev/null
>>> print 3
3
>>> 

How can the prompt be printed in both cases?

EDIT: Redirecting both stdout and stderr causes absolutely nothing to be printed. So Python's clearly "choosing" one of stdout or stderr. Is that documented to happen? I couldn't figure out how this is actually done in the Python source code.

like image 564
nneonneo Avatar asked Aug 24 '13 14:08

nneonneo


People also ask

In which mode of Python we can see >>> prompt?

In the script mode, a python program can be written in a file. This file can then be saved and executed using the command prompt. We can view the code at any time by opening the file and editing becomes quite easy as we can open and view the entire code as many times as we want.

What is an interactive prompt in Python?

The Python interactive console (also called the Python interpreter or Python shell) provides programmers with a quick way to execute commands and try out or test code without creating a file.

How do you use interactive in Python?

To start a Python interactive session, just open a command-line or terminal and then type in python , or python3 depending on your Python installation, and then hit Enter .

Which command is used to exit from the interactive mode in Python?

Exiting the Interpreter In Linux or macOS, type Ctrl + D . The interpreter terminates immediately; pressing Enter is not needed.


1 Answers

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.

like image 162
Bakuriu Avatar answered Oct 06 '22 14:10

Bakuriu