Eryk Sun <eryk...@gmail.com> added the comment:

> Should we add a version check as well [1]? I'm not totally comfortable 
> with bitmasking a handle here, but if we only do this additional 
> check on Win7 then I'm okay with it.

A version check should not be necessary. For file objects, setting both handle 
tag bits has always been a reserved signature for console pseudohandles. Here's 
the definition in the published console code:

https://github.com/microsoft/terminal/blob/b726a3d05d35928becc8313f6ac5536c9f503645/dep/Console/winconp.h#L460

It's mostly vestigial in Windows 8+ due to the addition of the ConDrv console 
device. Code that routed calls to LPC-based implementations when passed a 
console pseudohandle has all been removed. The only remaining use I know of is 
in the console attach code. If a process is started with STARTF_USESTDHANDLES, 
then AllocConsole() will only replace handles that are NULL or console 
pseudohandles. The docs for GetStdHandle explain this:

    Attach/detach behavior

    When attaching to a new console, standard handles are always replaced
    with console handles unless STARTF_USESTDHANDLES was specified during
    process creation.

    If the existing value of the standard handle is NULL, or the existing
    value of the standard handle looks like a console pseudohandle, the
    handle is replaced with a console handle.

    When a parent uses both CREATE_NEW_CONSOLE and STARTF_USESTDHANDLES to
    create a console process, standard handles will not be replaced unless
    the existing value of the standard handle is NULL or a console
    pseudohandle.

I confirmed that this is still relevant in Windows 10 by testing an inherited 
pipe handle in stdout and stderr and manually setting the tag bits in the 
hStdError handle value in STARTUPINFO. The console attach code preserved the 
regular handle in stdout but replaced the console pseudohandle in stderr. So 
the API is still reserving these tag bits in file handles, at least in one 
place, which means they're still reserved in general, even though setting them 
will no longer break a ReadFile call, for one example, like it would in Windows 
7 (in which the API would route the call to the internal ReadConsole 
implementation). 

We've been relying on checking the console pseudohandle tag bits without a 
version check in subprocess.Popen._filter_handle_list for a while now. Console 
pseudohandles have to be removed from the handle list, else CreateProcessW will 
fail.

----------

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

Reply via email to