New submission from Michael JasonSmith:
The email.utils.formataddr function is used to encode a name-address 2-tuple
for use as an email message. If the name contains a non-ASCII character it
needs to be encoded. This happens correctly in Python 3.3.2, but incorrectly in
Python 2.7.5. Ideally Python 2 would acquire the Python 3 behaviour, as this
should make porting easier.
In the following Python 3.3.2 example the name is encoded because of the
non-ASCII ☢ character:
>>> import email.utils
>>> name = 'Me ☢'
>>> addr = 'mp...@onlinegroups.net'
>>> email.utils.formataddr((name, addr))
'=?utf-8?b?TWUg4pii?= '
In Python 2.7.5 the same name is incorrectly left unaltered:
>>> import email.utils
>>> name = u'Me ☢'
>>> addr = 'mp...@onlinegroups.net'
>>> email.utils.formataddr((name, addr))
u'Me \u2622 '
However, calling the email.header.Header.encode method works around this issue
in Python 2:
>>> import email.utils
>>> import email.header
>>> name = u'Me ☢'
>>> addr = 'mp...@onlinegroups.net'
>>> h = email.header.Header(name)
>>> email.utils.formataddr((h.encode(), addr))
'=?utf-8?b?TWUg4pii?= '
The example code immediately above also works in Python 3; it is the current
work-around in GroupServer. However, ideally instances of Unicode objects
passed to email.utils.formataddr will work the same in both Python 2 and Python
3.
--
components: Library (Lib)
messages: 212648
nosy: Michael.JasonSmith
priority: normal
severity: normal
status: open
title: email.utils.formataddr encodes incorrectly
type: behavior
versions: Python 2.7, Python 3.2
___
Python tracker
<http://bugs.python.org/issue20845>
___
___
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com