EmailMessage.set_content() accepts memoryview through the raw data manager's registered bytes handler, but the 7bit, 8bit, and binary branches call a decode() method that memoryview does not provide.
Decode the buffer directly with str(data, encoding, errors). This preserves ASCII validation for 7bit and surrogateescape handling for 8bit and binary, without an intermediate bytes copy.
Regression tests cover bytes, bytearray, and memoryview payloads with all three transfer encodings, including binary data round trips and rejection of non-ASCII 7bit input.
Validation:
The new regression tests produce four AttributeError errors before the change and pass afterwards.
All 1,822 email tests pass (two skipped).
Full CPython suite: SUCCESS, 52,161 tests run, 2,945 skipped; duration 6 min 57 sec.
pre-commit checks pass for both changed Python files.
Rechecked the unchanged implementation from current main (4c73f50) on October 20; the bug still reproduces in all three cases. All 118 content manager tests pass with this fix.
Includes a Library NEWS entry.
AI assistance: Codex helped prepare the fix and tests and run validation.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Fixes gh-159858.
EmailMessage.set_content() accepts memoryview through the raw data manager's registered bytes handler, but the 7bit, 8bit, and binary branches call a decode() method that memoryview does not provide.
Decode the buffer directly with str(data, encoding, errors). This preserves ASCII validation for 7bit and surrogateescape handling for 8bit and binary, without an intermediate bytes copy.
Regression tests cover bytes, bytearray, and memoryview payloads with all three transfer encodings, including binary data round trips and rejection of non-ASCII 7bit input.
Validation:
Includes a Library NEWS entry.
AI assistance: Codex helped prepare the fix and tests and run validation.