Skip to content

Conversation

@pareshjoshij
Copy link
Contributor

Description

This PR fixes gh-142006 where email.policy.default would raise a HeaderWriteError when folding certain long address headers.

The Issue:
In _header_value_parser.py, the function _steal_trailing_WSP_if_exists moves trailing whitespace from the previous line to the current one. However, if the previous line contained only that whitespace, it was left as an empty string ''. When joined, this resulted in a double newline (\n\n), which is illegal in headers and triggered the error.

The Fix:
Modified _steal_trailing_WSP_if_exists to remove the line entirely (lines.pop()) if it becomes empty after stealing the whitespace.

Verification:
Added a regression test test_fold_address_list_with_stolen_whitespace to Lib/test/test_email/test_policy.py which reproduces the reported crash and verifies the fix.

Linked Issue

Fixes gh-142006

Copy link
Member

@bitdancer bitdancer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for finding and fixing this bug.

@bedevere-app
Copy link

bedevere-app bot commented Nov 27, 2025

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@pareshjoshij
Copy link
Contributor Author

pareshjoshij commented Nov 28, 2025

Thanks @bitdancer! I have made the requested changes; please review again.

  1. Updated the comment style in _header_value_parser.py.
  2. Moved the test to test__header_value_parser.py inside TestFolding, refactored to use max_line_length, and switched to the file's native self._test style.
  3. Updated the NEWS entry text as suggested.

Please review again.

@pareshjoshij
Copy link
Contributor Author

I have made the requested changes; please review again.

I have made the requested changes; please review again.

@bedevere-app
Copy link

bedevere-app bot commented Nov 28, 2025

Thanks for making the requested changes!

@bitdancer: please review the changes made to this pull request.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

email.policy.default adds an extra newline char to email address headers and raises HeaderWriteError on stringify

2 participants