Weirdness with python and stdin redirection under Win32

577 views
Skip to first unread message

Jonathan M. Gilligan

unread,
Dec 3, 2002, 9:56:31 PM12/3/02
to
I am getting an error reading stdin only when I invoke a python script by
typing "bar | foo.py" or "foo.py < bar", but not when typing "bar | python
foo.py" or "python foo.py < bar". The error arises ecause when input is
redirected, stdin has fileno = -1 if the script is invoked as "foo.py", but
correctly has fileno = 0 if it is invoked as "python foo.py"

Consider the following script, which I want to use as a prototype for a
simple stdin --> stdout filter:

# foo.py
import sys

print "fileno = ", sys.stdin.fileno()

lines = sys.stdin.xreadlines()
for line in lines:
sys.stdout.write(line)


On win32 (Win2k pro, SP3), using ActiveState ActivePython 2.2.1 build 222, I
can execute foo.py either by typing "foo.py" or "python foo.py" on the
command line (the relevant registry key for opening .py files has command
'C:\Python22\python.exe "%1" %*').

If I type "echo bar | foo.py", I get

C:\>echo bar | foo.py
The process tried to write to a nonexistent pipe.
fileno = -1
Traceback (most recent call last):
File "C:\foo.py", line 7, in ?
lines = sys.stdin.readlines()
IOError: [Errno 9] Bad file descriptor

whereas if I type "echo bar | python foo.py", I get

C:\>echo bar | python foo.py
fileno = 0
bar

Other tests yield:

C:\>foo.py < foo.py
fileno = -1
Traceback (most recent call last):
File "C:\Documents and Settings\Jonathan\My Documents\Programming\Omap
VU\Fast
CameraDriver\BF_LabView\Leo\foo.py", line 7, in ?
for line in lines:
IOError: [Errno 9] Bad file descriptor

C:\>python foo.py < foo.py
fileno = 0
# foo.py
import sys

print "fileno = ", sys.stdin.fileno()

lines = sys.stdin.xreadlines()
for line in lines:
sys.stdout.write(line)


If I just type "foo.py" or "python foo.py" on the command line, I get
"fileno = 0" in both cases and the script works identically in both cases.

Can anyone explain what's happening? Why the file descriptor for redirected
stdin has a fileno of -1 if the python script is invoked using "foo.py" but
not when it's invoked with "python foo.py"?

Thanks,
Jonathan


Bengt Richter

unread,
Dec 4, 2002, 12:24:54 AM12/4/02
to
On Tue, 3 Dec 2002 20:56:31 -0600, "Jonathan M. Gilligan" <jonathan...@vanderbilt.edu> wrote:

>I am getting an error reading stdin only when I invoke a python script by
>typing "bar | foo.py" or "foo.py < bar", but not when typing "bar | python
>foo.py" or "python foo.py < bar". The error arises ecause when input is
>redirected, stdin has fileno = -1 if the script is invoked as "foo.py", but
>correctly has fileno = 0 if it is invoked as "python foo.py"
>

[...]

You have bumped into a well-known bug present in many but not all versions
of windows.

A number of versions of windows do not correctly set up for redirected i/o
when a script is run based on running an interpreter selected by automatic
association via the script's file extension. This will affect e.g., perl also.

The workaround is to invoke the interpreter explicitly, as in python foo.py
(as you did) or to put that in a .cmd or .bat file and then use that file
as the executable, and redirect i/o wrt to that. There are also a couple
of tricky ways you can put a single batch command line as the first line
of your python source, and giving it a .cmd extension, and being able to
get the python part interpreted by python and the whole to do redirection ok.
I'm not sure whether/how a .pyc file may get generated and used that way though.
Maybe it works.

There should be a FAQ by now, I would think.

Regards,
Bengt Richter

Norbert Klamann

unread,
Dec 4, 2002, 6:25:57 AM12/4/02
to
"Jonathan M. Gilligan" <jonathan...@vanderbilt.edu> wrote in message news:<asjset$n3l$1...@news.vanderbilt.edu>...

> I am getting an error reading stdin only when I invoke a python script by
> typing "bar | foo.py" or "foo.py < bar", but not when typing "bar | python
> foo.py" or "python foo.py < bar". The error arises ecause when input is
> redirected, stdin has fileno = -1 if the script is invoked as "foo.py", but
> correctly has fileno = 0 if it is invoked as "python foo.py"
>

[nearly everything snipped]

> Can anyone explain what's happening? Why the file descriptor for redirected
> stdin has a fileno of -1 if the python script is invoked using "foo.py" but
> not when it's invoked with "python foo.py"?
>
> Thanks,
> Jonathan

If I remember correctly this is a Windows bug, it was mentioned sometimes on this
list.

HTH

Norbert Klamann

Thomas Heller

unread,
Dec 4, 2002, 7:29:54 AM12/4/02
to
Norbert...@klamann-software.de (Norbert Klamann) writes:

> "Jonathan M. Gilligan" <jonathan...@vanderbilt.edu> wrote in message news:<asjset$n3l$1...@news.vanderbilt.edu>...
>
> > I am getting an error reading stdin only when I invoke a python script by
> > typing "bar | foo.py" or "foo.py < bar", but not when typing "bar | python
> > foo.py" or "python foo.py < bar". The error arises ecause when input is
> > redirected, stdin has fileno = -1 if the script is invoked as "foo.py", but
> > correctly has fileno = 0 if it is invoked as "python foo.py"
> >
>

> If I remember correctly this is a Windows bug, it was mentioned sometimes on this
> list.
>

Right, but it is fixed in Win2k (and hopefully also on XP).

Thomas

Piet van Oostrum

unread,
Dec 4, 2002, 7:53:59 AM12/4/02
to
>>>>> bo...@oz.net (Bengt Richter) (BR) writes:

BR> On Tue, 3 Dec 2002 20:56:31 -0600, "Jonathan M. Gilligan" <jonathan...@vanderbilt.edu> wrote:
>> I am getting an error reading stdin only when I invoke a python script by
>> typing "bar | foo.py" or "foo.py < bar", but not when typing "bar | python
>> foo.py" or "python foo.py < bar". The error arises ecause when input is
>> redirected, stdin has fileno = -1 if the script is invoked as "foo.py", but
>> correctly has fileno = 0 if it is invoked as "python foo.py"
>>

BR> [...]

BR> You have bumped into a well-known bug present in many but not all versions
BR> of windows.

BR> A number of versions of windows do not correctly set up for redirected i/o
BR> when a script is run based on running an interpreter selected by automatic
BR> association via the script's file extension. This will affect e.g., perl also.

BR> The workaround is to invoke the interpreter explicitly, as in python foo.py
BR> (as you did) or to put that in a .cmd or .bat file and then use that file
BR> as the executable, and redirect i/o wrt to that.

There are also Windows versions where batch files have that same bug!!
--
Piet van Oostrum <pi...@cs.uu.nl>
URL: http://www.cs.uu.nl/~piet [PGP]
Private email: P.van....@hccnet.nl

Piet van Oostrum

unread,
Dec 4, 2002, 7:56:19 AM12/4/02
to
>>>>> Piet van Oostrum <pi...@cs.uu.nl> (PvO) writes:

>>>>> bo...@oz.net (Bengt Richter) (BR) writes:

BR> The workaround is to invoke the interpreter explicitly, as in python foo.py
BR> (as you did) or to put that in a .cmd or .bat file and then use that file
BR> as the executable, and redirect i/o wrt to that.

PvO> There are also Windows versions where batch files have that same bug!!

I would like to add that a good solution is to use a special program like
PythonLauncher to create real (small) executables that in fact just call
the interpreter with the proper script name.

Bengt Richter

unread,
Dec 4, 2002, 2:03:39 PM12/4/02
to

Ick. I didn't know that ;-/
Can you mention a specific example?

Regards,
Bengt Richter

Piet van Oostrum

unread,
Dec 4, 2002, 4:07:33 PM12/4/02
to
>>>>> bo...@oz.net (Bengt Richter) (BR) writes:

BR> Ick. I didn't know that ;-/
BR> Can you mention a specific example?

E.g. on Windows 98, make a file test1.bat, containing just:
DIR

and then run from a command prompt:
test1 > out

The output just goes to your screen, and the file out will be empty.

Tim Roberts

unread,
Dec 6, 2002, 2:19:36 AM12/6/02
to
Thomas Heller <the...@python.net> wrote:

I'm afraid not. The bug certainly DOES exist in Win2K, and I suspect it
also exists in XP.
--
- Tim Roberts, ti...@probo.com
Providenza & Boekelheide, Inc.

Jonathan M. Gilligan

unread,
Dec 4, 2002, 12:15:49 PM12/4/02
to
The bug is NOT fixed in Win2k. That's where I am seeing it (Win2K Pro SP3).

Jonathan

"Thomas Heller" <the...@python.net> wrote in message
news:fzteez...@python.net...

dexter...@gmail.com

unread,
Jan 18, 2013, 5:30:52 AM1/18/13
to
Guys,

I am trying my hands on Python, and i bump into an error and woah! when you google it up, it's an bug in windows.

The bug which you people are discussing about is still not patched up in Win XP. I have updated all the my windows update and have installed all kinds of patch.

regards
dexter
Reply all
Reply to author
Forward
0 new messages