Martin Panter added the comment:

I agree it would be good to document when the Readline library is invoked. Yes, 
the “readline‭” module is only designed to work with the original sys.stdin and 
sys.stdout. Python’s “open” function does not use <stdio.h> FILE objects, but 
Python does use <stdio.h> FILE objects internally for its Readline hook, and 
passes them to the Readline library. Python falls back to dumber 
implementations, which may not implement history nor completion, when it 
decides not to use Readline.

The “readline” module is implemented in Modules/readline.c and uses 
“rl_instream” 
<https://cnswww.cns.cwru.edu/php/chet/readline/readline.html#IDX228> and 
“rl_outstream” to specify <stdio.h> FILE objects that the Readline library 
uses. These FILE objects are passed through the PyOS_ReadlineFunctionPointer 
hook 
<https://docs.python.org/3.4/c-api/veryhigh.html#c.PyOS_ReadlineFunctionPointer>
 from the PyOS_Readline function in Parser/myreadline.c. They are required to 
be terminals (checked with the Posix “isatty” call).

The implementation of Python’s “input” function is in the “builtin_input” C 
function in Python/bltinmodule.c. Before calling PyOS_Readline, it requires 
that sys.stdin.fileno() and sys.stdout.fileno() match the <stdio.h> stdin and 
stdout FILE objects. It also does its own isatty checks.

You might have some luck calling “fopen” with the “ctypes” module, or writing 
your own Readline module, but it wouldn’t be straightforward. You might be able 
to fool the check by reopening file descriptors 0 and 1, but that seems rather 
hacky. Or you might rely on the OS to provide history and/or completion (I 
think the Windows console does this in a limited way), or an external Readline 
wrapper program (e.g. search for “rlwrap”).

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue29396>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to