-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
bpo-14156: Make argparse.FileType work correctly for binary file mode… #13165
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
Conversation
…s when argument is '-'
Update: Apparently this only occurs in high verbosity/warning levels, which I hadn't been using when running locally. I've tweaked the tests to fallback to using a unique sentinel value for each buffer when the buffer doesn't exist (it's not really about the buffer existing, it's just a convenient way to confirm that the buffer was used without leaving sys.stdout/sys.stderr patched, so using sentinels is fine). The tests now pass in both verbose and non-verbose modes, and are run in both in different automated tests, so the behavior is definitely being fully tested either way. |
…dXXX with io.StringIO, breaking our tests that assume a .buffer exists on the old stdout/stderr. Use a sentinel value for these cases to allow the tests to work (we don't really care about the specific buffer, we just need a way to indicate that the logical .buffers match)
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.
good job.
(See python/cpython#13165 for the actual fix in python)
As suggested in the linked Python bug, what about gracefully falling back to plain sys.stdin/sys.stdout if the buffer attribute is not available? That should hopefully reduce the need to add special cases to the test suite and result in simpler test code. The code looks reasonable otherwise. |
@MojoVampire are you still interested in shepherding this? If not, I can pick up from where you've left off and try to respond to feedback both here and on BPO. |
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.
LGTM!
Please add a |
Or maybe consider it a bugfix? |
As an end user: I think it's worth considering a bugfix! |
Thanks @MojoVampire for the PR, and @serhiy-storchaka for merging it 🌮🎉.. I'm working now to backport this PR to: 3.9, 3.10. |
Sorry, @MojoVampire and @serhiy-storchaka, I could not cleanly backport this to |
GH-31706 is a backport of this pull request to the 3.10 branch. |
…s when argument is '-' (pythonGH-13165) Also made modes containing 'a' or 'x' act the same as a mode containing 'w' when argument is '-' (so 'a'/'x' return sys.stdout like 'w', and 'ab'/'xb' return sys.stdout.buffer like 'wb'). (cherry picked from commit eafec26) Co-authored-by: MojoVampire <[email protected]>
…s when argument is '-' (GH-13165) Also made modes containing 'a' or 'x' act the same as a mode containing 'w' when argument is '-' (so 'a'/'x' return sys.stdout like 'w', and 'ab'/'xb' return sys.stdout.buffer like 'wb'). (cherry picked from commit eafec26) Co-authored-by: MojoVampire <[email protected]>
GH-31979 is a backport of this pull request to the 3.9 branch. |
…e modes when argument is '-' (pythonGH-13165) Also made modes containing 'a' or 'x' act the same as a mode containing 'w' when argument is '-' (so 'a'/'x' return sys.stdout like 'w', and 'ab'/'xb' return sys.stdout.buffer like 'wb').. (cherry picked from commit eafec26) Co-authored-by: MojoVampire <[email protected]>
…e modes when argument is '-' (GH-13165) (GH-31979) Also made modes containing 'a' or 'x' act the same as a mode containing 'w' when argument is '-' (so 'a'/'x' return sys.stdout like 'w', and 'ab'/'xb' return sys.stdout.buffer like 'wb'). (cherry picked from commit eafec26) Co-authored-by: MojoVampire <[email protected]>
…e modes when argument is '-' (pythonGH-13165) (pythonGH-31979) Also made modes containing 'a' or 'x' act the same as a mode containing 'w' when argument is '-' (so 'a'/'x' return sys.stdout like 'w', and 'ab'/'xb' return sys.stdout.buffer like 'wb'). (cherry picked from commit eafec26) Co-authored-by: MojoVampire <[email protected]>
…s when argument is '-'
Also made modes containing 'a' or 'x' act the same as a mode containing 'w' when argument is '-' (so 'a'/'x' return sys.stdout like 'w', and 'ab'/'xb' return sys.stdout.buffer like 'wb').
Expanded tests to cover binary mode, as well as covering mode 'x'/'xb' in general (we had no tests verifying that opening in exclusive write mode actually behaved correctly).
https://bugs.python.org/issue14156