Google Mail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Weirdness with python and stdin redirection under Win32
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  10 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Follow-up To:
Add Cc | Add Follow-up to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers that you hear
 
Jonathan M. Gilligan  
View profile   Translate to Translated (View Original)
 More options 4 Dec 2002, 03:30
Newsgroups: comp.lang.python
From: "Jonathan M. Gilligan" <jonathan.gilli...@vanderbilt.edu>
Date: Tue, 3 Dec 2002 20:56:31 -0600
Local: Wed 4 Dec 2002 02:56
Subject: Weirdness with python and stdin redirection under Win32
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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Bengt Richter  
View profile   Translate to Translated (View Original)
 More options 4 Dec 2002, 05:24
Newsgroups: comp.lang.python
From: b...@oz.net (Bengt Richter)
Date: 4 Dec 2002 05:24:54 GMT
Local: Wed 4 Dec 2002 05:24
Subject: Re: Weirdness with python and stdin redirection under Win32
On Tue, 3 Dec 2002 20:56:31 -0600, "Jonathan M. Gilligan" <jonathan.gilli...@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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Norbert Klamann  
View profile   Translate to Translated (View Original)
 More options 4 Dec 2002, 11:25
Newsgroups: comp.lang.python
From: Norbert.Klam...@klamann-software.de (Norbert Klamann)
Date: 4 Dec 2002 03:25:57 -0800
Local: Wed 4 Dec 2002 11:25
Subject: Re: Weirdness with python and stdin redirection under Win32
"Jonathan M. Gilligan" <jonathan.gilli...@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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Thomas Heller  
View profile   Translate to Translated (View Original)
 More options 4 Dec 2002, 12:29
Newsgroups: comp.lang.python
From: Thomas Heller <thel...@python.net>
Date: 04 Dec 2002 13:29:54 +0100
Local: Wed 4 Dec 2002 12:29
Subject: Re: Weirdness with python and stdin redirection under Win32

Norbert.Klam...@klamann-software.de (Norbert Klamann) writes:
> "Jonathan M. Gilligan" <jonathan.gilli...@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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Piet van Oostrum  
View profile   Translate to Translated (View Original)
 More options 4 Dec 2002, 13:21
Newsgroups: comp.lang.python
From: Piet van Oostrum <p...@cs.uu.nl>
Date: 04 Dec 2002 13:53:59 +0100
Local: Wed 4 Dec 2002 12:53
Subject: Re: Weirdness with python and stdin redirection under Win32

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

BR> On Tue, 3 Dec 2002 20:56:31 -0600, "Jonathan M. Gilligan" <jonathan.gilli...@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 <p...@cs.uu.nl>
URL: http://www.cs.uu.nl/~piet [PGP]
Private email: P.van.Oost...@hccnet.nl


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Piet van Oostrum  
View profile   Translate to Translated (View Original)
 More options 4 Dec 2002, 13:21
Newsgroups: comp.lang.python
From: Piet van Oostrum <p...@cs.uu.nl>
Date: 04 Dec 2002 13:56:19 +0100
Local: Wed 4 Dec 2002 12:56
Subject: Re: Weirdness with python and stdin redirection under Win32

>>>>> Piet van Oostrum <p...@cs.uu.nl> (PvO) writes:
>>>>> b...@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.
--
Piet van Oostrum <p...@cs.uu.nl>
URL: http://www.cs.uu.nl/~piet [PGP]
Private email: P.van.Oost...@hccnet.nl


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Bengt Richter  
View profile   Translate to Translated (View Original)
 More options 4 Dec 2002, 19:03
Newsgroups: comp.lang.python
From: b...@oz.net (Bengt Richter)
Date: 4 Dec 2002 19:03:39 GMT
Local: Wed 4 Dec 2002 19:03
Subject: Re: Weirdness with python and stdin redirection under Win32
On 04 Dec 2002 13:53:59 +0100, Piet van Oostrum <p...@cs.uu.nl> wrote:

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

Regards,
Bengt Richter


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Piet van Oostrum  
View profile   Translate to Translated (View Original)
 More options 4 Dec 2002, 21:13
Newsgroups: comp.lang.python
From: Piet van Oostrum <p...@cs.uu.nl>
Date: 04 Dec 2002 22:07:33 +0100
Local: Wed 4 Dec 2002 21:07
Subject: Re: Weirdness with python and stdin redirection under Win32

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

BR> On 04 Dec 2002 13:53:59 +0100, Piet van Oostrum <p...@cs.uu.nl> wrote:
>>>>>>> b...@oz.net (Bengt Richter) (BR) writes:

BR> On Tue, 3 Dec 2002 20:56:31 -0600, "Jonathan M. Gilligan" <jonathan.gilli...@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!!

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.
--
Piet van Oostrum <p...@cs.uu.nl>
URL: http://www.cs.uu.nl/~piet [PGP]
Private email: P.van.Oost...@hccnet.nl


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Tim Roberts  
View profile   Translate to Translated (View Original)
 More options 6 Dec 2002, 07:18
Newsgroups: comp.lang.python
From: Tim Roberts <t...@probo.com>
Date: Thu, 05 Dec 2002 23:19:36 -0800
Local: Fri 6 Dec 2002 07:19
Subject: Re: Weirdness with python and stdin redirection under Win32

Thomas Heller <thel...@python.net> wrote:
>Norbert.Klam...@klamann-software.de (Norbert Klamann) writes:

>> "Jonathan M. Gilligan" <jonathan.gilli...@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).

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

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jonathan M. Gilligan  
View profile   Translate to Translated (View Original)
 More options 6 Dec 2002, 23:40
Newsgroups: comp.lang.python
From: "Jonathan M. Gilligan" <jonathan.gilli...@vanderbilt.edu>
Date: Wed, 4 Dec 2002 11:15:49 -0600
Local: Wed 4 Dec 2002 17:15
Subject: Re: Weirdness with python and stdin redirection under Win32
The bug is NOT fixed in Win2k. That's where I am seeing it (Win2K Pro SP3).

Jonathan

"Thomas Heller" <thel...@python.net> wrote in message

news:fzteezlp.fsf@python.net...


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google