Issue 1367628: use PyOS_ReadlineFunctionPointer in non-interractive input (original) (raw)

Python provide a nice input hook for overiding it's input mechanism: User can define set PyOS_ReadlineFunctionPointer to point to an input function, and this function will be used instead of the default input function (PyOS_StdioReadline, or vms__StdioReadline for VMS).

This mechanism can be very handy when implementing a parallel python interpreter: the main process can simply brodcast to listening input functions its own input strings. However, the current implementation does not use the hook (PyOS_ReadlineFunctionPointer) when it detect that the input or output are non-interractive (non-tty), but instead call directly the default implementation PyOS_StdioReadline. A snippet of "Parser/myreadline.c":

...

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); ...

This makes impossible to override the input behavior for non-interractive input. In particular, this prevent implementing parallel python interpreter because only the main python interpreter process have tty input/output, but even in non-parralel context I feel this uneccessarily limit the hook mechanism...

I submitted a patch some time ago [id 955928], and Lisandro Dalcin submitted his own patch [id 1232343]to solve the same problem in the same context (parallel python interpreter).

Since then, we collaborated for producing a single patch, cleaner than the previous ones. We also used input from the python-dev list [http://mail.python.org/pipermail/python-dev/2005-August/055761.html] and from the author of the VMS patch in PyOS_Readline (Jean-François Piéronne). This new patch thus replace the 2 precedent ones that should be marked as "duplicate".

This patch passes 'Lib/test/regrtest.py' (of course, all those tests are non-interactive!). Lisandro and my company use this patched Python interpreter every day in the standard way and also in MPI-parallelized interactive sessions, and we never had any problem.

The patch is relative to the current svn repository, and implement modification in Include/pythonrun.h, Parser/myreadline.c, Modules/readline.c and Python/bltinmodule.c.

This last modification is needed for correcting the current behavior (not use the hook for non-interractive input/output) for the raw_input builtin. It is not needed for using the standard python interractive mode, but is needed if one want to use the ipython interpreter (or any interpreter implementing the input loop in python itself).