GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-151454: Accept Header values in email.message.Message.add_header by harjothkhara · Pull Request #151458 · python/cpython

Conversation

@harjothkhara

@harjothkhara harjothkhara commented Jun 13, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

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.

  • Issue: email.message.Message.add_header behavior differs from Message[header]=v #151454

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@harjothkhara @claude
pythongh-151454: Accept Header values in email.message.Message.add_he…
dcb7d25
…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]>
@harjothkhara
harjothkhara requested a review from a team as a code owner June 13, 2026 22:34
@bedevere-app bedevere-app Bot added the awaiting review label Jun 13, 2026
@bedevere-app bedevere-app Bot mentioned this pull request Jun 13, 2026
email.message.Message.add_header behavior differs from Message[header]=v #151454
Open
@read-the-docs-community

read-the-docs-community Bot commented Jun 13, 2026

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
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@harjothkhara

harjothkhara commented Jun 17, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

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?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@harjothkhara

harjothkhara commented Jun 27, 2026

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?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

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

harjothkhara reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

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

harjothkhara reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@harjothkhara
pythongh-151454: Raise a clear error when add_header() gets a Header …
1ac364a
…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

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

awaiting review

Projects

None yet

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.

2 participants

@harjothkhara @picnixz
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.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.