[ python-Bugs-1012692 ] Can't pipe input to a python program

SourceForge.net noreply at sourceforge.net
Sat Aug 21 21:50:25 CEST 2004


Bugs item #1012692, was opened at 2004-08-20 00:31
Message generated for change (Comment added) made by rivest
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1012692&group_id=5470

Category: Windows
Group: 3rd Party
Status: Closed
Resolution: Wont Fix
Priority: 5
Submitted By: Ronald L. Rivest (rivest)
Assigned to: Tim Peters (tim_one)
Summary: Can't pipe input to a python program

Initial Comment:
Details in attached file.  Basically, I want to be 
able (in Windows) to pipe input as in:
    type spam.txt | eggs.py
so that eggs can read the file spam.txt.  But I am
getting an  Errno 9: Bad file descriptor error message.

Thanks.

        Cheers,
        Ron Rivest
        rivest at mit.edu 
 [Please reply to rivest at mit.edu as well...]

----------------------------------------------------------------------

>Comment By: Ronald L. Rivest (rivest)
Date: 2004-08-21 14:50

Message:
Logged In: YES 
user_id=1090045

Thanks to tim_one!

Your analysis of the situation does seem correct, and
your proposed fix (to pipe to "python eggs.py" rather than
just to "eggs.py") works fine!

I agree that the "3rd Pary; Won't Fix" designation is
appropriate.

Thanks again!

        Cheers,
        Ronald L. Rivest

----------------------------------------------------------------------

Comment By: Tim Peters (tim_one)
Date: 2004-08-21 13:49

Message:
Logged In: YES 
user_id=31435

I see the same on my Win XP Pro SP1 box, but I expected 
to.  The problem is in cmd.exe (the native XP shell), and both 
MS shells (command.com and cmd.exe) have had multiple 
bugs with I/O redirection forever.  This somewhat confused 
MS KB article describes the current state for cmd.exe 
systems, in part incorrectly:

"STDIN/STDOUT Redirection May Not Work If Started from a 
File Association"

http://support.microsoft.com/default.aspx?kbid=321788

It works fine if you change the "eggs.py" part of your 
command line to "python eggs.py", where "python" is some 
string that resolves to your Python executable, or even to 
a .bat script that runs python.exe explicitly.  For example, I 
have a py.bat on my PATH, containing just

\python23\python.exe %1 %2 %3 %4 %5 %6 %7 %8 %9

and then

    type spam.txt | py eggs.py

works fine.  The point is that it doesn't work if Python gets 
started via .py *association*; any other way of getting 
Python started works fine.

Since this has nothing to do with Python (same thing if you 
use, e.g., Perl), I'm closing this as 3rd Party, Won't Fix.

BTW, the KB article above claims the problem was fixed in XP 
SP1.  It does indeed appear to have been fixed for stdout 
redirection, but not for stdin.

You could also use Cygwin on Windows, whose default bash 
shell has sane behavior in this respect.

Martin, it's sure curious that it works for you!  My ver output 
is also "Microsoft Windows XP [Version 5.1.2600]".  Is that 
enough to distinguish Home from Pro versions?  I have Pro 
here.  What do you have?  Is yours a German flavor of XP?

I'll note that I get one more line of output than Ron reported 
(the first output line, about "a nonexistent pipe"):

C:\Code\python\PCbuild>type tcap.txt | eggs.py
The process tried to write to a nonexistent pipe.
Traceback (most recent call last):
  File "C:\Code\python\PCbuild\eggs.py", line 4, in ?
    text = sys.stdin.read()
IOError: [Errno 9] Bad file descriptor

----------------------------------------------------------------------

Comment By: Ronald L. Rivest (rivest)
Date: 2004-08-21 12:43

Message:
Logged In: YES 
user_id=1090045

If I print out sys.stdin.fileno(), I get  -1 .
Ver.exe outputs: Microsoft Windows XP [Version 5.1.2600]
This is indeed the standard command interpreter; I set the
prompt envinronment variable to output just ">".

Thanks...

        Ron Rivest



----------------------------------------------------------------------

Comment By: Martin v. Löwis (loewis)
Date: 2004-08-21 10:09

Message:
Logged In: YES 
user_id=21627

What shell is this in? I cannot reproduce this in a cmd.exe
window. However, that you report the version as "Windows 5.1
(Build 2600)" and not "Microsoft Windows XP [Version
5.1.2600]" (as ver.exe does), and that you use just ">" as
the prompt suggests you might be using a different command
interpreter.

Can you please add

print sys.stdin.fileno()

into the script? On my system, it gives "0".

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1012692&group_id=5470


More information about the Python-bugs-list mailing list