email.message.Message.add_header behavior differs from Message[header]=v
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 77.2k
- Forks
- 35.9k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
Bug report
Bug description:
Attempting to copy headers from an existing message to a new (to-be-cleaned copy of the original) message, I iterated over the old message, using something like
for h, v in oldmsg.items():
newmsg.add_header(h, v)
which works for many of the test-messages in my test Maildir folder, but errors out on certain messages with
newmsg.add_header(h, v)
~~~~~~~~~~~~~~~~~~~~~~^^^^^^
File "/usr/local/lib/python3.13/email/message.py", line 587, in add_header
self[_name] = SEMISPACE.join(parts)
~~~~~~~~~~~~~~^^^^^^^
TypeError: sequence item 0: expected str instance, Header found
It appears that elements in msg.items() can produce a mixed-content list containing both (str, str) and (str, Header) elements; but add_header doesn't like receiving a Header object as a value, only str.
If I change it to use the .__setitem__() method function:
for h, v in oldmsg.items():
newmsg[h] = v
it appears to work fine, appending the header that .items() gave me.
According to the .add_header() documentation, it should behave similar to the .__setitem__() method:
This method is similar to
__setitem__()except that additional header parameters can be provided as keyword arguments._nameis the header field to add and_valueis the primary value for the header.
The fix would involve making add_header accept the same sorts of Header values as __setitem__, or at least align the output of msg.items() with the types that msg.set_header() accepts. If not aligned, at a bare minimum, the documentation should reflect the difference in accepted value types (str vs Header)
This may also have tie-ins with similar Issues here:
- #65294
- #91957
- #101090
(manifested on 3.13 on OpenBSD, but it's standard-library stuff that would likely manifest on any OS)
CPython versions tested on:
3.13
Operating systems tested on:
Other
Linked PRs
- gh-151458
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Prüfe zuerst den verknüpften PR gh-151458 und untersuche anschließend email/message.py im Umfeld von Message.add_header und vergleiche es mit Message.setitem und items(). Reproduziere den im Bericht beschriebenen gemischten str/Header-Fall; fertig ist es, wenn das Verhalten übereinstimmt oder der akzeptierte Unterschied bei den Werttypen dokumentiert ist.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- backend
- Issue-Typ
- Bug
- Schwierigkeit
- 3/5
- Geschätzter Aufwand
- 1-2 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 25/100