Skip to content

Fix bug introduced in c615478e

Mikael Davranche requested to merge email.message.Message into master

In c615478e, we moved from email.policy.default to email.policy.compat32.

As told in the documentation, email.policy.compat32 represents an email using an email.message.Message class:

>>> type(email.parser.BytesParser(policy=email.policy.compat32).parsebytes("Hey".encode("utf-8")))
<class 'email.message.Message'>

Whereas email.policy.default represents an email using an email.message.EmailMessage class:

>>> type(email.parser.BytesParser(policy=email.policy.default).parsebytes("Hey".encode("utf-8")))
<class 'email.message.EmailMessage'>

So, the unit tests should not test erine_email Python package using email.message.EmailMessage but email.message.Message.

Also, we should consider that, in a email.message.Message, a header can be either a string or an email.header.Header:

>>> m = email.message.Message()
>>> m["Subject"] = "Hey"
>>> h = email.header.Header()
>>> h.append("Hey".encode("utf-8"), "utf-8")
>>> m["Subject"] = h

Contrarily to the email.message.EmailMessage:

>>> m = email.message.EmailMessage()
>>> m["Subject"] = h
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.9/email/message.py", line 409, in __setitem__
    self._headers.append(self.policy.header_store_parse(name, val))
  File "/usr/lib/python3.9/email/policy.py", line 148, in header_store_parse
    return (name, self.header_factory(name, value))
  File "/usr/lib/python3.9/email/headerregistry.py", line 601, in __call__
    return self[name](name, value)
  File "/usr/lib/python3.9/email/headerregistry.py", line 196, in __new__
    cls.parse(value, kwds)
  File "/usr/lib/python3.9/email/headerregistry.py", line 271, in parse
    kwds['parse_tree'] = cls.value_parser(value)
  File "/usr/lib/python3.9/email/_header_value_parser.py", line 1112, in get_unstructured
    if value[0] in WSP:
TypeError: 'Header' object is not subscriptable

Note that part of the code proposed in this merge request come from before 3eb58026, when we moved from email.message.Message() (generated by email.message_from_binary_file()) to email.message.EmailMessage() (generated by email.parser.BytesParser(policy=default).parse()).

Unit tests passed. The coverage code on the erine_email Python package is still 100%.

Merge request reports