Eryk Sun added the comment:

An exception (not a crash) is possible if a standard handle is invalid and a 
Popen call tries to replace one or two of the other standard handles (e.g. 
stdin is invalid and you pass the argument stdout=PIPE). Note that subprocess 
uses the standard handles directly, so this problem is unrelated to Python's 
sys.std* streams. IMO, the presence of an invalid handle in the standard 
handles should not cause Popen to fail. This is an old problem, and it should 
have been addressed a long time ago by substituting a handle for \\.\NUL.

In Windows 8+ this isn't particular to running pythonw.exe. In older versions 
such as Windows 7, if you start pythonw.exe from a console application, then 
the parent's standard handle values are copied to the pythonw.exe child. But 
these handles are invalid because pythonw.exe doesn't automatically attach to 
its parent's console. (You have to manually call AttachConsole(-1) or 
AllocConsole(), but that's complicated because you may need to reset the CRT's 
standard file descriptors and FILE streams and Pythons sys.std* streams.)

Note that pythonw.exe doesn't delete or close the standard streams. They're 
normally set to None because, in the default case, the Windows standard handles 
aren't valid in pythonw.exe, and thus the CRT maps its standard file 
descriptors (0, 1, and 2) to INVALID_HANDLE_VALUE (i.e. -1 cast as a pointer). 
For example:

    C:\Temp>pyw -2 -c "import msvcrt; open('test.txt', 
'w').write(str(msvcrt.get_osfhandle(1)))"

    C:\Temp>type test.txt
    18446744073709551614

You can override this by starting pythonw.exe with explicit standard handles. 
For example, the following redirects stderr (2) to stdout (1) and redirects 
stdout to a pipe:

    C:\>set "script=import sys; print(sys.executable); print(sys.stdout); 
print(sys.stderr)"

    C:\>2>&1 pyw -2 -c "%script%" | more
    C:\Program Files\Python27\pythonw.exe
    <open file '<stdout>', mode 'w' at 0x00000000021FB0C0>
    <open file '<stderr>', mode 'w' at 0x00000000021FB150>

    C:\>2>&1 pyw -3 -c "%script%" | more
    C:\Program Files\Python36\pythonw.exe
    <_io.TextIOWrapper name='<stdout>' mode='w' encoding='cp1252'>
    <_io.TextIOWrapper name='<stderr>' mode='w' encoding='cp1252'>

----------
components: +Windows
nosy: +eryksun, paul.moore, steve.dower, tim.golden, zach.ware

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue29829>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to