This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Use WinAPI GetFileType() in is_valid_fd()
Type: enhancement Stage: resolved
Components: Interpreter Core, Windows Versions: Python 3.11
process
Status: closed Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: corona10, eryksun, paul.moore, steve.dower, tim.golden, zach.ware
Priority: normal Keywords: easy (C), patch

Created on 2021-11-28 19:21 by eryksun, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 30082 merged corona10, 2021-12-13 09:53
PR 30090 merged corona10, 2021-12-13 14:29
Messages (3)
msg407221 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2021-11-28 19:21
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
msg408449 - (view) Author: Dong-hee Na (corona10) * (Python committer) Date: 2021-12-13 12:58
New changeset 191c431de7d9b23484dd16f67e62c6e85a1fac7f by Dong-hee Na in branch 'main':
bpo-45919: Use WinAPI GetFileType() in is_valid_fd() (GH-30082)
https://github.com/python/cpython/commit/191c431de7d9b23484dd16f67e62c6e85a1fac7f
msg408511 - (view) Author: Dong-hee Na (corona10) * (Python committer) Date: 2021-12-14 01:31
New changeset 9130a4d62032468e0d4949aaa49c29afb0d854ca by Dong-hee Na in branch 'main':
bpo-45919: Remove out of date comment (GH-30090)
https://github.com/python/cpython/commit/9130a4d62032468e0d4949aaa49c29afb0d854ca
History
Date User Action Args
2022-04-11 14:59:52adminsetgithub: 90077
2021-12-14 01:31:45corona10setmessages: + msg408511
2021-12-13 14:29:32corona10setpull_requests: + pull_request28313
2021-12-13 13:06:50corona10setstatus: open -> closed
stage: patch review -> resolved
2021-12-13 12:58:03corona10setmessages: + msg408449
2021-12-13 09:53:17corona10setkeywords: + patch
nosy: + corona10

pull_requests: + pull_request28303
stage: needs patch -> patch review
2021-11-28 19:21:47eryksuncreate