Skip to content

Fix BodyPartReader.read() returning bytearray instead of bytes#12434

Closed
terminalchai wants to merge 1 commit intoaio-libs:masterfrom
terminalchai:fix/multipart-read-returns-bytes
Closed

Fix BodyPartReader.read() returning bytearray instead of bytes#12434
terminalchai wants to merge 1 commit intoaio-libs:masterfrom
terminalchai:fix/multipart-read-returns-bytes

Conversation

@terminalchai
Copy link
Copy Markdown

Fixes #12404

Problem

\BodyPartReader.read()\ internally accumulates data into a \�ytearray\ but returned it directly, violating the documented return type of \�ytes.

Both code paths were affected:

\\python

decode=False path

return data # bytearray, not bytes

decode=True path

return decoded_data # bytearray, not bytes
\\

This causes a silent type contract violation. Downstream code doing \isinstance(result, bytes)\ would fail, and attempting to JSON-serialize the result raises \TypeError.

Fix

Wrap both return values in \�ytes():

\\python
return bytes(data)
return bytes(decoded_data)
\\

Tests

Added two regression tests in \ ests/test_multipart.py:

  • \ est_read_returns_bytes_not_bytearray\ — decode=False path
  • \ est_read_decode_returns_bytes_not_bytearray\ — decode=True path

Changelog

Added \CHANGES/12404.bugfix.rst.

BodyPartReader.read() accumulated data into a bytearray internally
but returned it directly, violating the documented return type of
bytes. This caused downstream TypeError when trying to JSON-serialize
the result, e.g. json.dumps({'filename': data}).

The same defect existed on both code paths in read():
- decode=False: returned the raw accumulation bytearray
- decode=True:  returned the decoded accumulation bytearray

Fix by wrapping both return values in bytes().

Fixes aio-libs#12404
@psf-chronographer psf-chronographer Bot added the bot:chronographer:provided There is a change note present in this PR label Apr 28, 2026
@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented Apr 28, 2026

Merging this PR will not alter performance

✅ 67 untouched benchmarks
⏩ 4 skipped benchmarks1


Comparing terminalchai:fix/multipart-read-returns-bytes (3dda5cb) with master (1842c3e)2

Open in CodSpeed

Footnotes

  1. 4 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

  2. No successful run was found on master (573313e) during the generation of this report, so 1842c3e was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:chronographer:provided There is a change note present in this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: BodyPartReader.filename and read() leak bytearray instead of str/bytes, violating API contract and breaking JSON serialization

2 participants