GeezerVison is showing github.com — gh-151454: Accept Header values in email.message.Message.add_header by harjothkhara · Pull Request #151458 · python/cpython
Conversation
Under the compat32 policy, Message.items() yields a Header object (not a str) for any header carrying 8-bit data. You can assign that back with msg[name] = value, but msg.add_header(name, value) — documented as equivalent — raised TypeError: sequence item 0: expected str instance, Header found, because it always did SEMISPACE.join(parts). So the natural "copy every header" loop breaks:
for h, v in oldmsg.items():
newmsg.add_header(h, v) # TypeError on a Header value
Fix: when no extra parameters are passed, add_header() now stores the value the same way __setitem__ does, so a Header (or str) is stored unchanged — matching the documented equivalence. None still becomes ''. The parameterized branch is left as-is: it has to build a string, and silently stringifying an unknown-8bit Header would corrupt its bytes to U+FFFD, so it still raises there.
Added a regression test and a versionchanged note. ./python -m test test_email passes.
Disclosure: I used AI assistance writing this; I've reviewed the change and can explain it.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
…ader
Message.items() can yield (name, Header) pairs under the compat32 policy
(for a header carrying 8-bit data), and Message[name] = value accepts such
Header values, but add_header() raised "TypeError: sequence item 0: expected
str instance, Header found" because it assembled the value with
SEMISPACE.join().
When no extra parameters are given, add_header() now mirrors __setitem__ and
stores the value (Header or str) unmodified, fixing the reported header-copy
loop. None is still coerced to '' to preserve add_header()'s historical
behavior. The parameterized branch is unchanged: it still builds the header
as a string and so continues to require a str value.
Co-Authored-By: Claude Opus 4.8 <[email protected]>
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
@picnixz, small email-related fix in message.py. Since you've worked in this area recently, would you mind taking a quick look when you have a chance?
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
@picnixz gentle follow-up — you triaged gh-151454 (added the email labels). This patch makes add_header accept a Header value by delegating to __setitem__ when there are no params, with a test and NEWS. Mind a quick review when you have time?
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
Sorry, but no. I just added labels (which I can do easily) but reviewing takes time. I would suggest waiting for someone from the email to pick this up (but please be patient as it may require to wait more than just one or two weeks).
|
harjothkhara reacted with thumbs up emoji
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
Ok so I still took a bit of my time. I'm going to comment on the issue.
|
harjothkhara reacted with thumbs up emoji
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
…with params
Combining a Header value with parameters was already rejected, but with
the confusing internal TypeError from SEMISPACE.join(). Raise a clear
one instead.
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Milestone
No milestone
Development
Successfully merging this pull request may close these issues.
Uh oh!
There was an error while loading. Please reload this page.
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.
|