[New-bugs-announce] [issue45919] Use WinAPI GetFileType() in is_valid_fd()

Eryk Sun report at bugs.python.org
Sun Nov 28 14:21:47 EST 2021


New submission from Eryk Sun <eryksun at gmail.com>:

During startup, is_valid_fd() in Python/pylifecycle.c is called to validate stdin, stdout, and stderr. Performance isn't critical here, but every bit helps in reducing startup time. In my tests, implementing this check in Windows via GetFileType((HANDLE)_get_osfhandle(fd)) is 5-6 times faster than close(dup(fd)). For example:

#if defined(MS_WINDOWS)
    HANDLE hfile;
    _Py_BEGIN_SUPPRESS_IPH
    hfile = (HANDLE)_get_osfhandle(fd);
    _Py_END_SUPPRESS_IPH
    return (hfile != INVALID_HANDLE_VALUE &&
              GetFileType(hfile) != FILE_TYPE_UNKNOWN);
#endif

----------
components: Interpreter Core, Windows
keywords: easy (C)
messages: 407221
nosy: eryksun, paul.moore, steve.dower, tim.golden, zach.ware
priority: normal
severity: normal
stage: needs patch
status: open
title: Use WinAPI GetFileType() in is_valid_fd()
type: enhancement
versions: Python 3.11

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue45919>
_______________________________________


More information about the New-bugs-announce mailing list