-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
gh-121940: Reduce checking isatty on Windows write() #121941
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
485473d
TODO: Stop checking isatty on Windows write()
cmaloney 835eea6
remove more cases
cmaloney 76cc70b
gh-121940: Stop checking isatty on Windows write()
cmaloney 7059c6f
Merge branch 'cmaloney/windows_no_isatty' of ssh://github.com/cmalone…
cmaloney 7adcb0d
gh-121940: Update so write size is limited larger
cmaloney 4b27cdb
Update news
cmaloney fc8cad2
Fix typo
cmaloney File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
Misc/NEWS.d/next/Windows/2024-07-17-15-07-24.gh-issue-121940.M70eyc.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Default buffer size :func:`os.write` on Windows no longer or splits the write. | ||
Writing to the Windows console is still split to maintain responsiveness of | ||
interrupts, but at a much larger size. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
C
isatty()
is true for a file opened for any character device. It excludes pipes and disk files, but it's actually true for most devices, including "NUL". I'd rather only apply this limit to console files._PyIO_get_console_type()
could be moved from "Modules/_io/winconsoleio.c" to "Python/fileutils.c" and renamed_Py_get_console_type()
.The helper function
_get_console_type()
could also get a minor improvement. It depends on WinAPIGetConsoleMode()
, which requires read access. Unfortunately, "CON" can only be opened for writing if read access isn't requested. Fortunately, however, the device driver has to first dereference the handle to access the kernel File object, and it fails immediately if the file is opened for some other device. In this case, the error code isERROR_INVALID_HANDLE
. Otherwise, if it's a console file that simply lacks read access, the error code isERROR_ACCESS_DENIED
. We should check for the latter ifGetConsoleMode()
fails. For example:I'd also prefer to first check
isatty()
in_Py_get_console_type()
, before getting the handle and calling_get_console_type()
. Theisatty()
call is a quick check for a valid file descriptor and the presence of theFDEV
flag. If it's true, then it's worth doing the extra work to actually check whether it's a console file.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm concerned that making the check a lot more complicated will increase the runtime cost more than the savings of not having a
write
split. This PR currently doesn't change the check that is made, just makes it less commonly called.In general I would really like to eliminate the size + isatty check from
write
altogether, but I think to do that need to figure out why people are usingPYTHONLEGACYWINDOWSSTDIO
to avoid the newer WinConsoleIO and fix the issues underlying that. I'm potentially open to that line of work in the future, but is a much larger scope project than what this PR is focused on.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
os.write()
also calls_Py_write()
. It still needs Ctrl+C interrupt support, regardless of "PYTHONLEGACYWINDOWSSTDIO".As to why someone would use multibyte (legacy) console I/O, one reason would be that it actually works correctly in some cases nowadays1, which is overall simpler and more consistent than using
io._WindowsConsoleIO
.All we really need to check here is
isatty()
and whetherGetConsoleMode()
mode succeeds or fails withERROR_ACCESS_DENIED
. The IOCTL forGetConsoleMode()
poses no perceptible cost for interactive console I/O. The appreciable relative cost would be for other character devices, such as "NUL". But it's a fixed cost of a single system call, which fails immediately before doing any real work if it's not a console file2.Note that the CRT's
read()
andwrite()
functions themselves sometimes callisatty()
andGetConsoleMode()
, if the file is opened in text mode3. So it's not like callingGetConsoleMode()
is unprecedented in terms of determining how a read or write is handled.Footnotes
The console host finally has functional UTF-8 (codepage 65001) support for both reads and writes -- or at least "openconsole.exe" does in recent releases of Windows Terminal. The system console host, "conhost.exe", still doesn't support UTF-8 reads correctly. But since "conhost.exe" is based on the same code as "openconsole.exe", I expect it will be fixed in the next release of Windows 11 later this year. ↩
Console API functions that aren't direct I/O requests are implemented by sending an IOCTL to a console connection file, opened on "\Device\ConDrv\Connect". A handle argument, if any, gets packed in the IOCTL input data. Thus, even for a file opened on another device such as "NUL", the device-type check for
GetConsoleMode(hfile, &mode)
is always handled by the "condrv.sys" driver, and not the driver for the actual device ofhfile
, such as "null.sys". ↩The C runtime's text mode isn't used by builtin
open()
, but it's the default foros.open()
, unless theO_BINARY
flag is used. In some cases, such as an open in UTF-16 text mode, the C runtime switches to usingReadConsoleW()
andWriteConsoleW()
for a console file, instead ofReadFile()
andWriteFile()
. Note that this is just an in principle example since the CRT's UTF-8 and UTF-16 text modes aren't compatible with Python's I/O stack. They require reading and writing an even number of UTF-16 encoded bytes, which Python's raw and buffered I/O layers don't support. ↩There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@eryksun I am not okay making the changes you're requesting here.
I agree that Ctrl-C working well for all Python Windows I/O is a valuable feature. I believe this PR makes some cases better, and doesn't hurt more general I/O cases. I believe there is no change in behavior with this PR on I/O where
isatty
returns False other than it doesn't callisatty
anymore if the write is sufficiently small (< 1,000,000 bytes). Large individual write calls to fds whereisatty
returns False were not split before this PR, and are not split after it. I agree those may not be really responsive to Ctrl-C. This PR keeps existing behavior whenisatty
would return False.My motivation for this PR was that every
write()
on Windows over a relatively small threshold calledisatty
and that seemed like unnecessary work. Increasing the number of system calls / instructions being run by the processor to do bothisatty
andGetConsoleMode
doesn't align with that initial motivation. I understand that it may be more precise, and that both those calls are relatively quick, but my motivation for this PR was removing an individualisatty
call most the time. My personal perspective is that the fastest thing to do is less. Code still needs to meet API guarantees, and in this case splitting unnecessarily is okay and something the previous code did significantly more often. In CPythonmain
todayisatty
seems to be sufficiently precise today.When
isatty
returns True, there are two behavior changes with this PR:isatty
is never called, and the write is never split whereas inmain
it is always split. Testing under Windows Terminal,cmd.exe
,PowerShell.exe
, and the Visual Studio integrated shell on my Windows 11 box a singleprint()
from python of a extremely long string Ctrl-C feels responsive with the1,000,000
cutoff to me. The behavior change here is this cap is raised from 32,766 to 1,000,000 bytes.isatty
is called for general I/O but not for _WinConsoleIO (matching existing behavior, just the higher cap from change 1). Thewrite()
will always be capped to at most 1,000,000 bytes. The code will try to find the end of a utf-8 character by searching backwards up to 4 bytes now, which is a behavior change (Previously only_WinConsoleIO
did the back search)I'm open to a path forward but I don't currently see one I am comfortable implementing. If the changes you're requesting in this review comment are required, I'll close this PR as I'm not going to make those changes. In that case, if someone else would like to pick up these changes, adopt, and make the requested changes they're welcome to, I'm just not interested in that work.