473,566 Members | 2,908 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sys.stdin and two CTRL-Ds

Hi...

I encountered a problem that - according to my google search - other
people have found, too. Example code:

import sys
for line in sys.stdin:
print line

Running this code in Python 2.3 or 2.4 has the problem that I need to
send two EOFs (Ctrl-D) to break out of that loop.

Use case is a piped Python script that I run as a "CustomLog" pipe for
an Apache web server. I want to run all the log output through that pipe
but haven't had much luck. First it takes ages until the script
processes the input (even though I ran Python with "-u" to make it
unbuffered) and then the script needs one EOF to do something with the
input and a second EOF to break out of the loop.

Is this a bug? I'm close to write a Perl script for this case. :(

Regards
Christoph
Jul 1 '06 #1
8 3797
On 2/07/2006 5:02 AM, Christoph Haas wrote:
> Hi...

I encountered a problem that - according to my google search - other
people have found, too. Example code:

import sys
for line in sys.stdin:
print line

Running this code in Python 2.3 or 2.4 has the problem that I need to
send two EOFs (Ctrl-D) to break out of that loop.

Use case is a piped Python script that I run as a "CustomLog" pipe for
an Apache web server. I want to run all the log output through that pipe
but haven't had much luck. First it takes ages until the script
processes the input (even though I ran Python with "-u" to make it
unbuffered)
Which "it" did you expect to make unbuffered? -u unbuffers sys.stdout
and sys.stderr (and makes them binary, which wouldn't be a good idea on
a Windows box). If you are concerned that output shows up in real time,
use sys.stdXXX.flus h().
> and then the script needs one EOF to do something with the
input and a second EOF to break out of the loop.

Is this a bug? I'm close to write a Perl script for this case. :(
I can reproduce your problem on Windows with Python 2.4.2 (note ^Z is
EOF for Windows):

|>> import sys
|>> for line in sys.stdin:
.... print >> sys.stderr, "->", repr(line)
....
line1
line2
line3
^Z
-> 'line1\n'
-> 'line2\n'
-> 'line3\n'
^Z
|>>

However this worked for me :

|>> import sys
|>> while 1:
.... line = sys.stdin.readl ine()
.... if not line: break
.... print >> sys.stderr, "->", repr(line)
....
line1
-> 'line1\n'
line2
-> 'line2\n'
^Z
|>>

The Library Reference Manual, section 2.3.9 (File Objects) says:
"""
Files support the iterator protocol. Each iteration returns the same
result as file.readline() , and iteration ends when the readline() method
returns an empty string.
"""

So yes, looks like a bug to me, but perhaps you don't need use Perl :-)

HTH,
John
Jul 1 '06 #2
In article <44************ **@lexicon.net> ,
John Machin <sj******@lexic on.netwrote:
>-u unbuffers sys.stdout
and sys.stderr (and makes them binary, which wouldn't be a good idea on
a Windows box).
Why not?
Jul 2 '06 #3
On 2/07/2006 3:48 PM, Lawrence D'Oliveiro wrote:
In article <44************ **@lexicon.net> ,
John Machin <sj******@lexic on.netwrote:
>-u unbuffers sys.stdout
and sys.stderr (and makes them binary, which wouldn't be a good idea on
a Windows box).

Why not?
If binary, '\n' would appear as LF alone rather than CR LF.
Jul 2 '06 #4
In article <44******@news. eftel.com>,
John Machin <sj******@lexic on.netwrote:
>On 2/07/2006 3:48 PM, Lawrence D'Oliveiro wrote:
>In article <44************ **@lexicon.net> ,
John Machin <sj******@lexic on.netwrote:
>>-u unbuffers sys.stdout
and sys.stderr (and makes them binary, which wouldn't be a good idea on
a Windows box).

Why not?

If binary, '\n' would appear as LF alone rather than CR LF.
Why should that matter? I thought Windows (the NT line) was
POSIX-compliant.
Jul 3 '06 #5
On 3/07/2006 4:45 PM, Lawrence D'Oliveiro wrote:
In article <44******@news. eftel.com>,
John Machin <sj******@lexic on.netwrote:
>On 2/07/2006 3:48 PM, Lawrence D'Oliveiro wrote:
>>In article <44************ **@lexicon.net> ,
John Machin <sj******@lexic on.netwrote:

-u unbuffers sys.stdout
and sys.stderr (and makes them binary, which wouldn't be a good idea on
a Windows box).
Why not?
If binary, '\n' would appear as LF alone rather than CR LF.

Why should that matter?
Because the contents of the output would be different. Haven't you ever
wondered why (or read the manuals that explain why) Python, C , etc file
I/O libraries have a text mode and a binary mode?
I thought Windows (the NT line) was
POSIX-compliant.
What on earth gave you that idea?

Jul 3 '06 #6
I may be wrong, but I've never heard of Windows being fully posix
compliant. I guarentee you that they dont support pthreads.

It is possible that by "posix compliant" the marketting execs mean
"supports all posix commands which dont interfere with our way of doing
things"

Windows version of python always uses \r\n for line ends (which are
then combined into a single \n character when read). This bears strong
resemblance to the carriage return line feed combination required by
typerwriters before the computer era. Wordpad will read Unix line ends
(\n) if it has to, notepad wont even try. Mac used to use \r for line
ends, just to be the odd ball. I think they may have stopped that with
OSX. Any mac user want to cover this one?

Whats especially frustrating is the cygwin version of python. If you
configure things wrong with the cygwin line ends, you can have python
swear its outputing \n while its really coughing out \r\n's
Lawrence D'Oliveiro wrote:
Why should that matter? I thought Windows (the NT line) was
POSIX-compliant.
Jul 3 '06 #7
In article <44********@new s.eftel.com>,
John Machin <sj******@lexic on.netwrote:
>On 3/07/2006 4:45 PM, Lawrence D'Oliveiro wrote:
>I thought Windows (the NT line) was
POSIX-compliant.

What on earth gave you that idea?
<http://fuckinggoogleit .com/?query=windows+ nt+posix>
Jul 3 '06 #8
On 3/07/2006 9:14 PM, Lawrence D'Oliveiro wrote:
In article <44********@new s.eftel.com>,
John Machin <sj******@lexic on.netwrote:
>On 3/07/2006 4:45 PM, Lawrence D'Oliveiro wrote:
>>I thought Windows (the NT line) was
POSIX-compliant.
What on earth gave you that idea?

<http://fuckinggoogleit .com/?query=windows+ nt+posix>
Thank you for your elegant and witty response.

The POSIX sub-system of Windows NT 4+ is POSIX-compliant to some degree.
This is not the same as saying that the whole of Windows is
POSIX-compliant. It means that people who write applications using that
subsytem can achieve the same result as if the app was being run on a *x
platform. This is as wonderful and as useless as tits on a bull. So what
do you do if you redirect stdout to a file and a user complains that
Notepad won't grok it? Teach him/her to use vi[m]?
Jul 3 '06 #9

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

Similar topics

7
6144
by: Yandos | last post by:
Hello all, I have maybe a trivial question, but I cannot think out what is wrong :( How do i detect EOF correctly when i read from stdin? Am I doing it wrong? <pipetest.c> #include <stdio.h> int main(void) { char ch;
30
45755
by: Jonathan Neill | last post by:
I'm aware that there is no ANSI (or POSIX, or any standard, to my knowledge) way of flushing stdin or any other application-level input buffer, but if anyone knows a hack or a non-portable way to do this, no matter how obscure or arcane, I'd appreciate it.
2
4479
by: Dmitry Anikin | last post by:
I want to read stdin in chunks of fixed size until EOF I want to be able (also) to supply data interactively in console window and then to hit Ctrl+Z when finished So what I do is: while True: s = sys.stdin.read(chunk_size) if not s: break # do something with s
0
1975
by: Oliver Bleckmann | last post by:
hey guys, i have a little problem. my cgi routines are working so far, but i need to redirect to the cgi-programm and what the "posted" data to be cleared if redirected. i don't know how the post method exactly works. maybe i have to clear the stdin (tried fflush)!? maybe the is another reason that the former data is displayed. fist i...
1
3870
by: BenjaMinster | last post by:
I want to read and write unicode on stdin and stdout. I can't seem to find any way to force sys.stdin.encoding and sys.stdout.encoding to be utf-8, so I've got the following workaround: import codecs, sys out = codecs.getwriter("utf-8")(sys.stdout) def tricky(): return sys.stdin.readline().decode("utf-8"). That is, I wrap sys.stdout...
25
5831
by: 7stud | last post by:
I can't break out of the for loop in this example: ------ import sys lst = for line in sys.stdin: lst.append(line) break
2
1576
by: varaprasad yadav | last post by:
i have created an array print "Enter a string"; @list = <STDIN>; print @list; when i excute this it is always in input mode even though i go to new line by pressing enter or if i try bypressing ctrl z
4
3238
by: Adam Funk | last post by:
I'm using this sort of standard thing: for line in fileinput.input(): do_stuff(line) and wondering whether it reads until it hits an EOF and then passes lines (one at a time) into the variable line. This appears to be the behaviour when it's reading STDIN interactively (i.e. from the keyboard).
9
3480
by: Peter Blues | last post by:
I'm trying to write from keyboard input to a txt file. I don't understand why the while loop below doesn't achieve this. I.. 1: open the file for writing through FILE pointer outstream 2: I get chars one at a time as ints(ascii) through fd using getchar() 3: Copy from fd to FILE *outstream using fputc() 4: Finally close outstream. I feel...
209
8609
by: arnuld | last post by:
I searched the c.l.c archives provided by Google as Google Groups with "word input" as the key words and did not come up with anything good. C++ has std::string for taking a word as input from stdin. C takes input in 2 ways: 1) as a character, etchar() 2) as a whole line, fgets()
0
7673
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
7893
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8109
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
7953
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...
1
5485
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...
0
5213
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...
0
3643
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...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
926
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.