-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
fix: Writing to bytes TextIO stream in StreamWriter causing AttributeError on buffer member #6034
fix: Writing to bytes TextIO stream in StreamWriter causing AttributeError on buffer member #6034
Conversation
- The TextIO's binary buffer member is not guaranteed to exist according to the python docs. (https://docs.python.org/3.8/library/io.html#io.TextIOBase.buffer) - This change decodes the bytes to a string so that we can use the write method on the TextIO stream.
Looking at the users of this function, for example aws-sam-cli/samcli/local/docker/container.py Lines 476 to 478 in 8126aaa
write_str instead?
|
If that's true, then that would make sense but it looks to also be used in here: aws-sam-cli/samcli/local/docker/container.py Lines 367 to 368 in 8126aaa
|
Sure, but does that make sense? In that case, if this PR is merged, the bytes are decoded to a string anyway, it's just hidden from the caller and could surprise them if they assumed the bytes wouldn't be interpreted in any specific encoding. Rather, in my opinion, either this abstraction should offer a way to write raw bytes with no interpretation (decoding) or it should only offer a way to write strings. Hiding these bytes to string conversions will shoot the user in the foot eventually. |
That's a great point, I'll update the PR. |
I'm adding both @jfuss and @mildaniel as reviewers given their previous experience in bytes/str operation. My take is that we are explicitly changing behavior from bytes to str and there could be side-effects. There was an explicit PR to decode - replace carriage returns - reencode. That behavior seems to be removed at this point. |
It might be a good idea to bisect which commit triggered this bug. That might help with figuring out what the right thing to do here is. FWIW, I think this PR looks logical, but I'm unfamiliar with this codebase and its history. This is fairly high in priority in my opinion as I simply can't use the latest version SAM CLI right now due to this bug. |
I did some digging here and it seems as though the issue is that the
Although this PR is more of a solution to the symptom rather than root cause, I'm inclined to go with it so I don't really see any negatives here and it allows us to patch an important issue. Another solution is to update the type in the @sriram-mv what are your thoughts here? |
Which issue(s) does this change fix?
#6033
Why is this change necessary?
Fixes the bug raise in #6033 where the instance of the stream member in StreamWriter does not have the buffer attribute.
How does it address the issue?
Rather than using the buffer attribute to write bytes, the bytes are converted to a string first with decode and then written with the write method (which accepts strings)
What side effects does this change have?
Unsure.
Mandatory Checklist
PRs will only be reviewed after checklist is complete
make pr
passesmake update-reproducible-reqs
if dependencies were changedBy submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.