Skip to content

gh-151454: Accept Header values in email.message.Message.add_header - #151458

Open
harjothkhara wants to merge 2 commits into
python:mainfrom
harjothkhara:gh-151454-add-header-Header-value
Open

harjothkhara wants to merge 2 commits into
python:mainfrom
harjothkhara:gh-151454-add-header-Header-value

Conversation

@harjothkhara

@harjothkhara harjothkhara commented Jun 13, 2026 •

Copy link
Copy Markdown
Contributor

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.

…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 <noreply@anthropic.com>
@read-the-docs-community

Copy link
Copy Markdown

Documentation build overview

📚 cpython-previews | 🛠️ Build #33128551 | 📁 Comparing dcb7d25 against main (9ad6ba0)

  🔍 Preview build  

2 files changed
± library/email.compat32-message.html
± whatsnew/changelog.html

@harjothkhara

harjothkhara commented Jun 17, 2026 •

Copy link
Copy Markdown
Contributor Author

@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?

@harjothkhara

Copy link
Copy Markdown
Contributor Author

@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?

@picnixz

picnixz commented Jun 27, 2026

Copy link
Copy Markdown
Member

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).

@picnixz

picnixz commented Jun 27, 2026

Copy link
Copy Markdown
Member

Ok so I still took a bit of my time. I'm going to comment on the issue.

…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.
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.

2 participants