Can someone explain this unexpected raw_input behavior?
Gabriel Genellina
gagsl-py2 at yahoo.com.ar
Wed Jan 23 18:14:07 EST 2008
En Wed, 23 Jan 2008 18:27:56 -0200, Mike Kent <mrmakent at cox.net> escribió:
> It's often useful for debugging to print something to stderr, and to
> route the error output to a file using '2>filename' on the command
> line.
>
> However, when I try that with a python script, all prompt output from
> raw_input goes to stderr. Consider the following test program:
> [...]
> This indicates to me that the prompt output of raw_input is being sent
> to stderr. I did check the source code for raw_input, and it appears
> to be sending it to stdout as expected.
Surely you've seen that in bltinmodule.c, builtin_raw_input calls
PyOS_Readline(PyFile_AsFile(fin), PyFile_AsFile(fout), prompt);
where fin and fout are sys.stdin and sys.stdout respectively.
That function is defined in Parser/myreadline.c, and eventually calls
PyOS_StdioReadline with the same arguments. But PyOS_StdioReadline doesn't
use its sys_stdout parameter to output the prompt; instead, it always uses
stderr, no matter what arguments it receives.
Looking at the svn history, PyOS_StdioReadline always has used stderr; got
its current signature in rev 29400 (2002).
Perhaps that behavior is based on the reverse of your use case, which may
be more common. A script writes most of its output to stdout, and you
capture that using >filename. If raw_input prompted the user using stdout,
that would not be visible in this case, so using stderr is more useful.
--
Gabriel Genellina
More information about the Python-list
mailing list