Skip to content

gh-136052: Do not generate empty encoded-words #136099

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Lib/email/_header_value_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -3011,7 +3011,10 @@ def _fold_as_ew(to_encode, lines, maxlen, last_ew, ew_combine_allowed, charset,
to_encode_word = to_encode_word[:-1]
encoded_word = _ew.encode(to_encode_word, charset=encode_as)
excess = len(encoded_word) - remaining_space
lines[-1] += encoded_word
# If encoding a single character pushes this line over the limit,
# give up on it and go to the next line.
if to_encode_word != "":
lines[-1] += encoded_word
to_encode = to_encode[len(to_encode_word):]
leading_whitespace = ''

Expand Down
8 changes: 8 additions & 0 deletions Lib/test/test_email/test__header_value_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -3084,6 +3084,14 @@ def test_ews_combined_before_wrap(self):
"mich. And that's\n"
" all I'm sayin.\n")

def test_unicode_near_end_of_line(self):
self._test(parser.get_unstructured("Mein Kaktus ist sehr attraktiv. Er "
"hat viele Stacheln und liebt "
"übriggebliebenes Eigelb."),
"Mein Kaktus ist sehr attraktiv. Er hat viele "
"Stacheln und liebt \n"
" =?utf-8?q?=C3=BCbriggebliebenes?= Eigelb.\n")

def test_unicode_after_unknown_not_combined(self):
self._test(parser.get_unstructured("=?unknown-8bit?q?=A4?=\xa4"),
"=?unknown-8bit?q?=A4?==?utf-8?q?=C2=A4?=\n")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Do not create empty MIME encoded-words when preparing e-mail headers.
Loading