Skip to content

Commit 9099083

Browse files
committed
Stop using NonASCIILocalPartDefect in email._header_value_parser
When parsing email messages from Unicode strings (but not bytes), get_local_part() recorded a NonASCIILocalPartDefect for non-ASCII characters. RFC 5322 permits such addresses. This change: - removes the parse-time detection for a non-ASCII local-part (and a related test) - adds tests for passing a non-ASCII addr_spec to email.headerregistry.Address.__init__() - marks the (undocumented) email.errors.NonASCIILocalPartDefect as unused and deprecated This affected parsing email messages from Unicode strings (but not from bytes), and also prevented
1 parent 22dd204 commit 9099083

File tree

5 files changed

+13
-26
lines changed

5 files changed

+13
-26
lines changed

Lib/email/_header_value_parser.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1493,11 +1493,6 @@ def get_local_part(value):
14931493
local_part.defects.append(errors.ObsoleteHeaderDefect(
14941494
"local-part is not a dot-atom (contains CFWS)"))
14951495
local_part[0] = obs_local_part
1496-
try:
1497-
local_part.value.encode('ascii')
1498-
except UnicodeEncodeError:
1499-
local_part.defects.append(errors.NonASCIILocalPartDefect(
1500-
"local-part contains non-ASCII characters)"))
15011496
return local_part, value
15021497

15031498
def get_obs_local_part(value):

Lib/email/errors.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,9 @@ class ObsoleteHeaderDefect(HeaderDefect):
109109
"""Header uses syntax declared obsolete by RFC 5322"""
110110

111111
class NonASCIILocalPartDefect(HeaderDefect):
112-
"""local_part contains non-ASCII characters"""
113-
# This defect only occurs during unicode parsing, not when
114-
# parsing messages decoded from binary.
112+
"""Unused. Note: this error is deprecated and may be removed in the future."""
113+
# RFC 6532 permits a non-ASCII local-part. _header_value_parser previously
114+
# treated this as a parse-time defect (when parsing Unicode, but not bytes).
115115

116116
class InvalidDateDefect(HeaderDefect):
117117
"""Header has unparsable or invalid date"""

Lib/email/headerregistry.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,8 @@ def __init__(self, display_name='', username='', domain='', addr_spec=None):
4545
raise ValueError("Invalid addr_spec; only '{}' "
4646
"could be parsed from '{}'".format(
4747
a_s, addr_spec))
48-
relevant_defects = [
49-
defect for defect in a_s.all_defects
50-
if not isinstance(defect, errors.NonASCIILocalPartDefect)
51-
]
52-
if relevant_defects:
53-
raise relevant_defects[0]
48+
if a_s.all_defects:
49+
raise a_s.all_defects[0]
5450
username = a_s.local_part
5551
domain = a_s.domain
5652
self._display_name = display_name

Lib/test/test_email/test__header_value_parser.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1235,17 +1235,6 @@ def test_get_local_part_valid_and_invalid_qp_in_atom_list(self):
12351235
'@example.com')
12361236
self.assertEqual(local_part.local_part, r'\example\\ example')
12371237

1238-
def test_get_local_part_unicode_defect(self):
1239-
# Currently this only happens when parsing unicode, not when parsing
1240-
# stuff that was originally binary.
1241-
local_part = self._test_get_x(parser.get_local_part,
1242-
1243-
'exámple',
1244-
'exámple',
1245-
[errors.NonASCIILocalPartDefect],
1246-
'@example.com')
1247-
self.assertEqual(local_part.local_part, 'exámple')
1248-
12491238
# get_dtext
12501239

12511240
def test_get_dtext_only(self):
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,8 @@
1-
Allow non-ASCII addr_spec in email.headerregistry.Address constructor
1+
The :mod:`email` module no longer treats email addresses with non-ASCII
2+
characters as defects when parsing a Unicode string or in the ``addr_spec``
3+
parameter to :class:`email.headerregistry.Address`. :rfc:`5322` permits such
4+
addresses, and they were already supported when parsing bytes and in the Address
5+
``username`` parameter.
6+
7+
The (undocumented) :exc:`!email.errors.NonASCIILocalPartDefect` is no longer
8+
used and should be considered deprecated.

0 commit comments

Comments
 (0)