only write to stdout buffer when buffer object is available #1428
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.
I've stumbled too on the issue mentioned by @Hebarusan in #1382, that Flake8 raises the error:
AttributeError: '_io.StringIO' object has no attribute 'buffer'
when a Flake8 check fails.I took a dive in and found that
sys.stdout
sometimes has thebuffer
attribute, and sometimes it doesn't.The Python documentation about
stdout
states:And also:
And that seems to be the case!
I've checked this by putting a
breakpoint()
inside the_write
method (the method from which theAttributeError
was raised), and printed the result oftype(sys.stdout)
, which was<class '_io.TextIOWrapper'
. However, without the breakpoint, it printed<class '_io.StringIO'>
.I therefore concluded that both
sys.stdout.buffer.write
andsys.stdout.write
should work.