Issue #4 invalid

Pipes

eryksun
created an issue

Is there any solution for using pipes without having to run 'py script.py'? For example, "[command] | py script.py" works fine, but "[command] | script.py" fails because sys.stdin is None.

Simple test script: {{{

!python

import sys

print type(sys.stdin)

if not sys.stdin.isatty(): print sys.stdin.read() }}}

My guess is that instead of cmd.exe creating the process and setting up the input pipe, the route through the registry calls CreateProcess with a STARTUPINFO in which hStdInput doesn't reference the pipe. But I'm far from an expert on such matters.

Comments (6)

  1. Vinay Sajip

    It works for me - See this screenshot.

    Screenshot of py working with a pipe

    This was cmd.exe on Windows 7 (64-bit). Your problem may be appearing on an older version of Windows and/or cmd.exe, but IMO it's not anything to do with the launcher's operation.

    I get the same results for

    echo Hello, world! | showver

    and

    echo Hello, world! | py showver.py

  2. eryksun reporter

    In Python 2, sys.stdin exists but the fileno is -2. In Python 3 sys.stdin is None. Repeated trials also cause cmd to report "The process tried to write to a nonexistent pipe" just before running the script. The is on Windows XP, service pack 3. Apparently, as Vinay's screenshot shows, the problem is fixed in newer versions. I doubt anything can be done, but it's good to have some info about it, as there are still a lot of XP systems around.

  3. Vinay Sajip

    Possibly relevant:

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

    Though that refers to Windows 2000, it may be that the problem still occurs in XP (although the post does say for Windows XP that the problem should be fixed in "the latest service pack for Windows XP", which would be SP3). It may be worth checking if a hotfix is available. It's also worth checking that InheritConsoleHandles key mentioned in the Microsoft post, to confirm that it's present and set to the expected value - however, I believe that the Microsoft fix only applies to output handles, but not to sys.stdin. This was Tim Peters' comment in 2004:

    http://bugs.python.org/issue1012692#msg22148

  4. eryksun reporter

    The system has service pack 3, but the registry setting wasn't enabled. The following policy setting fixed the problem:

    [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\Explorer]
    "InheritConsoleHandles"=dword:00000001
    

    Thanks

  5. Log in to comment