473,708 Members | 2,363 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 5312
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
2999
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
1821
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 of updates. It doesn't seem to matter how quickly the updates occur, but it does segfault faster...
2
1969
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 catches all exceptions that have not be handled at a higher level. The catch block cleans up and...
1
2947
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 function. There seems to be some unreliable behavior in Boa Constructor's Editor - Shell. If you look...
3
4872
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
2930
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 line arguments to the program, the behaviour of one of the functions changes mysteriously. I have...
6
2260
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
2387
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
2227
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
8792
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8699
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9163
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9064
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5941
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4456
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3151
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
2
2508
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2097
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.