gracefully update the behavior of the buffering argument to open to support buffered text streams too #139566
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.
Problem
Mansoor Ahmed wrote one of the best bug reports I've ever seen in my entire life at https://bugs.python.org/issue30718, regarding how the
buffering
argument to the standard builtinopen()
i/o method is unusable for text buffers as a result of double-buffering. @slateny then followed up to modify the documentation in #32351.This began because I didn't understand the current language used to refer to double buffering, and after having dived into it a bit I have come to agree with Mansoor that there is no useful way to apply the double buffering that results when specifying a positive number greater than 1 for a text stream.
Solution
Since this issue was first raised, there have been a few changes to the C-level API of
TextIOWrapper
objects. In particular, they now support specifying thewrite_through
option directly in the constructor. I believe this fixes a very uncharacteristic "sharp edge" that users can run into working with python.I spent some more time advancing the documentation and included the code sample @izbyshev provided way back in 2017, as I thought it made the operations happening in C code make much more sense.
I believe however that the list layout I chose which uses asterisks is not quite right and may break the docs. I'm not fully sure because I found the docs extremely difficult to generate and am working on fixing that separately.
Thank you for your time. I hope to work further with CPython in the future.
📚 Documentation preview 📚: https://cpython-previews--139566.org.readthedocs.build/