Skip to content
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

only write to stdout buffer when buffer object is available #1428

Conversation

thijskramer
Copy link

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 the buffer attribute, and sometimes it doesn't.
The Python documentation about stdout states:

When interactive, the stdout stream is line-buffered. Otherwise, it is block-buffered like regular text files.

And also:

[...] However, if you are writing a library (and do not control in which context its code will be executed), be aware that the standard streams may be replaced with file-like objects like io.StringIO which do not support the buffer attribute.

And that seems to be the case!

I've checked this by putting a breakpoint() inside the _write method (the method from which the AttributeError was raised), and printed the result of type(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 and sys.stdout.write should work.

@thijskramer thijskramer force-pushed the write-to-stdout-buffer-only-when-available branch from 810f982 to 9cde73f Compare October 19, 2021 18:55
@asottile
Copy link
Member

dupe #1422

@asottile asottile closed this Oct 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants