classification
Title: email.utils.getaddresses does not handle Header objects
Type: behavior Stage:
Components: email Versions: Python 3.6, Python 3.5
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: barry, frispete, r.david.murray, xtreak
Priority: normal Keywords:

Created on 2016-07-14 10:50 by frispete, last changed 2018-09-22 18:23 by xtreak.

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
Messages (3)
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.
History
Date User Action Args
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