raw_input(), STRANGE behaviour

D

Dox33

I ran into a very strange behaviour of raw_input().
I hope somebody can tell me how to fix this.
(Or is this a problem in the python source?)

I will explain the problem by using 3 examples. (Sorry, long email)
The first two examples are behaving normal, the thirth is
strange.......

I wrote the following flabbergasting code:
#-------------------------------------------------------------
print "1: This is the print statement."

# Now halt the program until someone hits enter
raw_input("2: This is the raw_input prompt.")

import sys
print "3: This is a possible solution. ",
sys.stdout.flush()
command = sys.stdin.readline()
#-------------------------------------------------------------
and saved it in the file 'script.py'.

*** First, normal behaviour to stdout
Now, I open a command line and run the following command:
python script.py

On screen appears, after a few 'enter' keys:
1: This is the print statement.
2: This is the raw_input prompt.
3: This is a possible solution.
All works fine......

*** Second, redirected stdout to file, normal behaviour.
From the command prompt I run:
python script.py > stdout_catch.txt
The screen stays empty, and after a few 'enter' keys the prompt
reapears.

In the file 'stdout_catch.txt' are the lines:
1: This is the print statement.
2: This is the raw_input prompt.
3: This is a possible solution.
And again, all works fine......

*** Thirst, redirect stderr to file, STRANGE behaviour......
From the command prompt I run:
python script.py 2> stderr_catch.txt
This should redirect strerr to the file and stdout should stay on the
screen.

But..... What happens?
After a few 'enter' keys, on screen apears:
1: This is the print statement.
3: This is a possible solution.

WHERE IS THE SECOND LINE?
It is in the file stderr_catch.txt!!!

**** See the problem?
Please Tell me? Why is the prompt produced by raw_input() printed to
the error channel? It should be stdout, just as the print statement
does.

Looking at the python source code on
http://svn.python.org/view/python/tags/r251/Python/bltinmodule.c?rev=54864&view=auto
[found: 'builtin_raw_input(PyObject *self, PyObject *args)'].

One thing that I notice is that the code checks whether stdin and
stdout are connected to a terminal ("isatty" function), but I do not
know why the prompt ends up on stderr then.....

**** What is the solution?
I have noticed this behaviour under DOS in Windows XP, Windows 2000,
Windows 2003 Server, Windows vista, and Linux. How do I get
raw_input() to send it's prompt to stdout?

Friendly greetings

Rens
 
M

Mike Kent

I ran into a very strange behaviour of raw_input().
I hope somebody can tell me how to fix this. ===CUT===
*** Thirst, redirect stderr to file, STRANGE behaviour......
From the command prompt I run:
python script.py 2> stderr_catch.txt
This should redirect strerr to the file and stdout should stay on the
screen.

But..... What happens?
After a few 'enter' keys, on screen apears:
1: This is the print statement.
3: This is a possible solution.

WHERE IS THE SECOND LINE?
It is in the file stderr_catch.txt!!!

**** See the problem?
Please Tell me? Why is the prompt produced by raw_input() printed to
the error channel? It should be stdout, just as the print statement
does.

I recently ran into this behaviour myself, and reported it both here
and to the python-dev mailing list. See
http://groups.google.com/group/comp...nk=gst&q=unexpected+behavior#6f8b7c47873a4a1e

It turns out that *if* you don't have GNU readline installed, Python
falls back to its own implementation of readline, which is hard-coded
to send the prompt output to stderr. Guido agreed that this is wrong,
and a bug for it has been entered into the Python bug tracking
database.

Your workaround until this bug is fixed is to install GNU readline,
and rebuild your python installation to use it rather than the fall-
back version.
 
D

Dox33

Hello Mike,

Thanks for your reply.
Since I momentarily do not have the ability to build a new python
executable, I would like to ask for your help in this case.
Are you able to supply me with a corrected version?

Friendly greetings

Rens Duijsens
 
T

Terry Reedy

I believe a workaround to the bug of raw_input sending the prompt to stderr
is
print 'prompt:',
a = raw_input()

Not nice, but possibly better that waiting for a corrected binary.
 
H

Hrvoje Niksic

Dox33 said:
Thanks for your reply. Since I momentarily do not have the ability
to build a new python executable, I would like to ask for your help
in this case. Are you able to supply me with a corrected version?

You can simply choose not to use raw_input, and use sys.stdin.readline
instead. Or define your own corrected raw_input:

def my_raw_input(msg):
print msg,
return raw_input()
 
D

Dox33

Yes, I know.
There are several ways to work around the problem.
(Look at the innitial code I provided in this discussion start)
Fact is, every time I'm getting a script from somewhere or someone, I
have to search and replace all the affected code.
Not very conveniant.
That's why I rather would have a correct working version.
 
G

Gabriel Genellina

Yes, I know.
There are several ways to work around the problem.
(Look at the innitial code I provided in this discussion start)
Fact is, every time I'm getting a script from somewhere or someone, I
have to search and replace all the affected code.
Not very conveniant.
That's why I rather would have a correct working version.

Add this on your sitecustomize.py module (or create one)

import sys

def raw_input(prompt=None):
if prompt: sys.stdout.write(prompt)
return original_raw_input()

import __builtin__
original_raw_input = __builtin__.raw_input
__builtin__.raw_input = raw_input

It just replaces the builtin raw_input with a custom function.
 
S

sevenbark

On Sat, 26 Jan 2008 04:23:36 -0800 (PST), Dox33

WHERE IS THE SECOND LINE?
It is in the file stderr_catch.txt!!!

**** See the problem?
Please Tell me? Why is the prompt produced by raw_input() printed to
the error channel? It should be stdout, just as the print statement
does.

Isn't this the norm in Unix shells also? In bash the "read" command
prints the prompt on stderr and the "select" command prints both the
menu and prompt on stderr.

sb
 
D

Dox33

YES!
This is what I was looking for.
Great! All works fine now.

Thank you very much Gabriel.

Gabriel Genellina schreef:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top