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: EmailMessage should throw an error if policy is set to compat32
Type: behavior Stage:
Components: email Versions: Python 3.5
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Matthieu Pepin, barry, r.david.murray
Priority: normal Keywords:

Created on 2015-11-16 21:48 by Matthieu Pepin, last changed 2022-04-11 14:58 by admin.

Messages (6)
msg254757 - (view) Author: Matthieu Pepin (Matthieu Pepin) Date: 2015-11-16 21:48
Message.is_attachment() throws the exception below:

 File "/usr/lib/python3.5/email/message.py", line 956, in is_attachment
    return False if c_d is None else c_d.content_disposition == 'attachment'
AttributeError: 'str' object has no attribute 'content_disposition'

It seems to work fine for me with:

return c_d == 'attachment'
msg254758 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2015-11-16 21:56
Can you provide a minimum reproducer?  It looks like you are somehow managing to use EmailMessage with the compat34 policy, which shouldn't happen unless you work at it, or I overlooked something.

Perhaps EmailMessage should disallow setting policy to compat32.
msg254759 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2015-11-16 21:57
I mean compat32.
msg254765 - (view) Author: Matthieu Pepin (Matthieu Pepin) Date: 2015-11-16 22:36
Ok I think the bug is not where I thought. First, my workaround does not work. Second, I use the following to generate my Message:

message_from_string(s, _class=EmailMessage)

Maybe the bug is in the parser?
msg254766 - (view) Author: Matthieu Pepin (Matthieu Pepin) Date: 2015-11-16 22:37
Yes, I will make a small reproducer.
msg254769 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2015-11-16 23:14
Ah, yes, that would do it.  EmailMessage should definitely throw an error if it gets compat32 as a policy, that would have told you what you were doing "wrong".

What you want to be doing is:

  msg = message_from_string(mystring, policy=default)

That will get you an EmailMessage object.  Note, however, that you almost never want message_from_string, because real email messages are bytes objects, not strings.  string only works if your message is ASCII only. (There's an open issue about documenting this properly...and a big doc revision in the offing.)
History
Date User Action Args
2022-04-11 14:58:23adminsetgithub: 69826
2018-07-11 07:41:35serhiy.storchakasettype: crash -> behavior
2015-11-16 23:15:44r.david.murraysettitle: Message.is_attachment() throws exception -> EmailMessage should throw an error if policy is set to compat32
2015-11-16 23:14:51r.david.murraysetmessages: + msg254769
2015-11-16 22:37:27Matthieu Pepinsetmessages: + msg254766
2015-11-16 22:36:40Matthieu Pepinsetmessages: + msg254765
2015-11-16 21:57:02r.david.murraysetmessages: + msg254759
2015-11-16 21:56:25r.david.murraysetmessages: + msg254758
2015-11-16 21:48:00Matthieu Pepincreate