This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: email.utils.getaddresses does not handle Header objects
Type: behavior Stage: resolved
Components: email Versions: Python 3.11, Python 3.10
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: Jeffrey.Kintscher, ZackerySpytz, barry, frispete, lukasz.langa, miss-islington, r.david.murray, xtreak
Priority: normal Keywords: patch

Created on 2016-07-14 10:50 by frispete, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
iso-8859-1-encoded-from-header.mail frispete, 2016-07-14 10:50 test mail
decode_from_header.py frispete, 2016-07-14 10:56 test program, that demonstrates the problem
Pull Requests
URL Status Linked Edit
PR 13797 merged ZackerySpytz, 2019-06-04 05:59
PR 27242 merged miss-islington, 2021-07-19 16:08
PR 27245 merged miss-islington, 2021-07-19 16:56
Messages (6)
msg270399 - (view) Author: Hans-Peter Jansen (frispete) * Date: 2016-07-14 10:50
An unfortunate combination of get_all and getaddresses results in a Traceback:

Traceback (most recent call last):
  File "misc/decode_from_header.py", line 17, in <module>
    print('From: %s' % email.utils.getaddresses(val))
  File "/usr/lib64/python3.4/email/utils.py", line 112, in getaddresses
    all = COMMASPACE.join(fieldvalues)
TypeError: sequence item 0: expected str instance, Header found

Here's the relevant part of it:
Content-type: text/html;charset=iso-8859-1
From: Itaú Uniclass. <comunicado.com.br@atendimento.gotdns.ch>

Obviously, the From header is iso-8859-1 encoded as well, and violates RFC 2822 as such. But making it crash in the usual combination of

    val = msg.get('from')
    email.utils.getaddresses([val])

isn't the real McCoy either..
msg270401 - (view) Author: Hans-Peter Jansen (frispete) * Date: 2016-07-14 10:56
message.get cannot decode the header correctly, and returns a Header instance instead, which makes email.utils.getaddresses stumble upon...

A much better behavior for getaddresses in this case would be returning the perfectly valid address, and ignoring the invalid dtext part.
msg270425 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2016-07-14 16:56
This appears to be something that was overlooked when btyes support was added in 3.2.  getaddresses should indeed be able to handle a Header object if handed one.
msg397812 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2021-07-19 16:08
New changeset 89f4c34797de2f0e5045da2b97c1c8cbbb42fbb2 by Zackery Spytz in branch 'main':
bpo-27513: email.utils.getaddresses() now handles Header objects (#13797)
https://github.com/python/cpython/commit/89f4c34797de2f0e5045da2b97c1c8cbbb42fbb2
msg397815 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2021-07-19 16:55
New changeset 8c43bf1a923754fa6d97772151c6ac23c48759d3 by Miss Islington (bot) in branch '3.10':
bpo-27513: email.utils.getaddresses() now handles Header objects (GH-13797) (GH-27242)
https://github.com/python/cpython/commit/8c43bf1a923754fa6d97772151c6ac23c48759d3
msg397820 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2021-07-19 17:28
New changeset 9ee12cf325d4da2c07919e5e56545feb7e005e08 by Miss Islington (bot) in branch '3.9':
bpo-27513: email.utils.getaddresses() now handles Header objects (GH-13797) (#27245)
https://github.com/python/cpython/commit/9ee12cf325d4da2c07919e5e56545feb7e005e08
History
Date User Action Args
2022-04-11 14:58:33adminsetgithub: 71700
2021-07-19 17:28:59lukasz.langasetmessages: + msg397820
2021-07-19 16:56:41miss-islingtonsetpull_requests: + pull_request25793
2021-07-19 16:55:54lukasz.langasetstatus: open -> closed
stage: patch review -> resolved
resolution: fixed
versions: + Python 3.10, Python 3.11, - Python 3.7, Python 3.8
2021-07-19 16:55:03lukasz.langasetmessages: + msg397815
2021-07-19 16:08:03miss-islingtonsetnosy: + miss-islington
pull_requests: + pull_request25791
2021-07-19 16:08:01lukasz.langasetnosy: + lukasz.langa
messages: + msg397812
2019-06-04 06:01:40ZackerySpytzsetnosy: + ZackerySpytz

versions: + Python 3.7, Python 3.8, - Python 3.5, Python 3.6
2019-06-04 05:59:30ZackerySpytzsetkeywords: + patch
stage: patch review
pull_requests: + pull_request13682
2019-05-31 07:11:56Jeffrey.Kintschersetnosy: + Jeffrey.Kintscher
2018-09-22 18:23:30xtreaksetnosy: + xtreak
2018-07-11 07:44:17serhiy.storchakasettype: crash -> behavior
2016-07-14 16:56:41r.david.murraysettitle: email.utils.getaddresses raises exception from erroneous message get_all input -> email.utils.getaddresses does not handle Header objects
messages: + msg270425
versions: + Python 3.5, Python 3.6, - Python 3.4
2016-07-14 10:56:08frispetesetfiles: + decode_from_header.py

messages: + msg270401
2016-07-14 10:50:10frispetecreate