-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
fix: ensure BodyPartReader.read() returns bytes, not bytearray #12413
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
base: master
Are you sure you want to change the base?
Changes from 2 commits
a833e2b
e31bc14
c3f30a1
e650dc4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| Fixed ``BodyPartReader.read()`` and ``BodyPartReader.read(decode=True)`` returning | ||
| ``bytearray`` instead of ``bytes``, violating the documented API contract. | ||
| -- by :user:`CrepuscularIRIS`. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -185,6 +185,29 @@ async def test_read(self) -> None: | |
| assert b"Hello, world!" == result | ||
| assert obj.at_eof() | ||
|
|
||
| async def test_read_returns_bytes_not_bytearray(self) -> None: | ||
| # Regression test for https://github.com/aio-libs/aiohttp/issues/12404 | ||
| # read() must return bytes (not bytearray) to honour the documented API contract. | ||
| with Stream(b"Hello, world!\r\n--:") as stream: | ||
| d = CIMultiDictProxy[str](CIMultiDict()) | ||
| obj = aiohttp.BodyPartReader(BOUNDARY, d, stream) | ||
| result = await obj.read() | ||
| assert isinstance( | ||
| result, bytes | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need to have the assertion in the CM.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done in e650dc4 — the |
||
| ), f"Expected bytes, got {type(result).__name__}" | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need to have a custom assertion text, the test runner already reveals the local vars better.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done in e650dc4 — the custom match string is removed; pytest's default output is enough. |
||
|
|
||
| async def test_read_decode_returns_bytes_not_bytearray(self) -> None: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use parametrization instead of duplicating the same check with only small difference but exactly the same semantics.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done in e650dc4 — the two copies are now a single |
||
| # Regression test for https://github.com/aio-libs/aiohttp/issues/12404 | ||
| # read(decode=True) must return bytes (not bytearray) even when no | ||
| # Content-Transfer-Encoding / Content-Encoding header is present. | ||
| with Stream(b"Hello, world!\r\n--:") as stream: | ||
| d = CIMultiDictProxy[str](CIMultiDict()) | ||
| obj = aiohttp.BodyPartReader(BOUNDARY, d, stream) | ||
| result = await obj.read(decode=True) | ||
| assert isinstance( | ||
| result, bytes | ||
| ), f"Expected bytes, got {type(result).__name__}" | ||
|
|
||
| async def test_read_chunk_at_eof(self) -> None: | ||
| with Stream(b"--:") as stream: | ||
| d = CIMultiDictProxy[str](CIMultiDict()) | ||
|
|
||
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.
Use RST roles to link the types.
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.
Done in e650dc4 — both types are now linked with
:class:\bytearray`and:class:`bytes``.