473,574 Members | 2,307 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

raw_input(), STRANGE behaviour

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.flus h()
command = sys.stdin.readl ine()
#-------------------------------------------------------------
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.tx t
The screen stays empty, and after a few 'enter' keys the prompt
reapears.

In the file 'stdout_catch.t xt' 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 2stderr_catch.t xt
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.tx t!!!

**** 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/ta...4864&view=auto
[found: 'builtin_raw_in put(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
Jan 26 '08 #1
8 5287
On Jan 26, 7:23 am, Dox33 <Rens.Duijs...@ gmail.comwrote:
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 2stderr_catch.t xt
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.tx t!!!

**** 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....8b7c47873a4a1e

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.
Jan 26 '08 #2
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

On 26 jan, 16:50, Mike Kent <mrmak...@cox.n etwrote:
On Jan 26, 7:23 am, Dox33 <Rens.Duijs...@ gmail.comwrote:


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 2stderr_catch.t xt
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.tx t!!!
**** 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. *Seehttp://groups.google.c om/group/comp.lang.pytho n/browse_thread/thread/...

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.- Tekst uit oorspronkelijk bericht niet weergeven -

- Tekst uit oorspronkelijk bericht weergeven -
Jan 27 '08 #3
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.


Jan 27 '08 #4
Dox33 <Re***********@ gmail.comwrites :
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.readl ine
instead. Or define your own corrected raw_input:

def my_raw_input(ms g):
print msg,
return raw_input()
Jan 27 '08 #5
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.

On 27 jan, 04:20, Hrvoje Niksic <hnik...@xemacs .orgwrote:
Dox33 <Rens.Duijs...@ gmail.comwrites :
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.readl ine
instead. *Or define your own corrected raw_input:

def my_raw_input(ms g):
* * print msg,
* * return raw_input()
Jan 27 '08 #6
En Sun, 27 Jan 2008 12:51:51 -0200, Dox33 <Re***********@ gmail.com>
escribi�:
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.p y module (or create one)

import sys

def raw_input(promp t=None):
if prompt: sys.stdout.writ e(prompt)
return original_raw_in put()

import __builtin__
original_raw_in put = __builtin__.raw _input
__builtin__.raw _input = raw_input

It just replaces the builtin raw_input with a custom function.

--
Gabriel Genellina

Jan 27 '08 #7
On Sat, 26 Jan 2008 04:23:36 -0800 (PST), Dox33
<Re***********@ gmail.comwrote:

<snip>
>WHERE IS THE SECOND LINE?
It is in the file stderr_catch.tx t!!!

**** 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
Jan 28 '08 #8
YES!
This is what I was looking for.
Great! All works fine now.

Thank you very much Gabriel.

Gabriel Genellina schreef:
Add this on your sitecustomize.p y module (or create one)

import sys
def raw_input(promp t=None):
if prompt: sys.stdout.writ e(prompt)
return original_raw_in put()

import __builtin__
original_raw_in put = __builtin__.raw _input
__builtin__.raw _input = raw_input

It just replaces the builtin raw_input with a custom function.
Jan 28 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2991
by: Helmut Jarausch | last post by:
Hi when using an interactive Python script, I'd like the prompt given by raw_input to go to stderr since stdout is redirected to a file. How can I change this (and suggest making this the default behaviour) Many thanks for a hint, Helmut Jarausch
0
1813
by: dale | last post by:
Python newbie disclaimer on I am running an app with Tkinter screen in one thread and command-line input in another thread using raw_input(). First question - is this legal, should it run without issue? If not can you point me to a description of why. While updating objects on the screen I get a segfault after an indeterminate number...
2
1954
by: Paul Drummond | last post by:
Hi all, I am developing software for Linux Redhat9 and I have noticed some very strange behaviour when throwing exceptions within a shared library. All our exceptions are derived from std::exception. We have a base class which all processes derive from which is always instantiated in main surrounded by a try/catch(std::exception) which...
1
2938
by: JerryKreps | last post by:
Hi, folks -- I'm a Python pup. As you can see from the session copied at the end of this post, I have the latest version of Python, and I've been using the Editor-Shell of the latest version of Boa Constructor while going through some Python tutorials. Everything was working as expected until I started using the raw_input built-in...
3
4862
by: Sebastian C. | last post by:
Hello everybody Since I upgraded my Office XP Professional to SP3 I got strange behaviour. Pieces of code which works for 3 years now are suddenly stop to work properly. I have Office XP Developer (SP3 for Office, SP1 for developer, JET40SP8) on Windows XP Home Edition (SP1). The same behaviour occurs on Windows 98 too.
6
2925
by: Edd Dawson | last post by:
Hi. I have a strange problem involving the passing of command line arguments to a C program I'm writing. I tried posting this in comp.programming yesterday but someone kindly suggested that I'd have better luck here. So here goes! My program ignores any command line arguments, or at least it's supposed to. However, when I pass any command...
6
2254
by: oliver | last post by:
Hey Everyone, This is probably going to sound like a bit of a stupid question - but why does (in the following code) the script just continue to run past the raw_input, when the user hasn't entered anything? if __name__ == "__main__": bucket_name = raw_input('Name of the bucket you wish the files to be placed into? ') update_s3()
4
2381
by: TP | last post by:
Hi everybody, When using raw_input(), the input of the user ends when he types Return on his keyboard. How can I change this behavior, so that another action is needed to stop the input? For example, CTRL-G. It would allow the user to input several lines. Thanks Julien
20
2209
by: Pilcrow | last post by:
This behavior seems very strange to me, but I imagine that someone will be able to 'explain' it in terms of the famous C standard. -------------------- code ----------------------------------- #include <stdio.h> int main (void) { char xx="abcd"; char * p1 = xx;
0
7831
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7746
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8262
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8129
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6497
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5641
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
1
2258
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1364
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1092
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.