Interactive scripts (back on topic for once) [was Re: The "loop and a half"]

eryk sun eryksun at gmail.com
Fri Oct 6 16:40:02 EDT 2017


On Fri, Oct 6, 2017 at 4:27 PM, Thomas Jollans <tjol at tjol.eu> wrote:
> On 2017-10-06 17:01, eryk sun wrote:
>>
>> POSIX defines STDIN_FILENO as 0, and the Windows C runtime reserves FD
>> 0 to map to the native StandardInput handle. But as I noted in a
>> previous message, on Windows isatty(0) returns true if a process isn't
>> executed with a valid StandardInput. In this case sys.stdin will be
>> None, so call sys.stdin.fileno() and handle the exception if that
>> fails.
>
> Seriously? sys.stdin can be None? That's terrifying.

Just check for None or handle the exception.

It could be worse in Windows -- like inheriting broken standard I/O.
This can happen if a console application is run without a console
(i.e. dwCreationFlags=DETACHED_PROCESS) yet console handles are
inherited in the standard handles via STARTUPINFO. isatty() will
return True, but using the handles will fail. To avoid this problem
with subprocess.Popen, if you use creationflags=DETACHED_PROCESS, make
sure to set unused standard handles to subprocess.DEVNULL.



More information about the Python-list mailing list