Skip to content

Commit 764b1dc

Browse files
committed
Implement comments from code review
- _parsedate_tz should explicitely return None - parsedate_to_datetime should check for None as return value by _parsedate_tz and in that case raise ValueError - Fix documentation and tests accordingly
1 parent bc99204 commit 764b1dc

5 files changed

Lines changed: 13 additions & 14 deletions

File tree

‎Doc/library/email.utils.rst‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,9 @@ of the new API.
125125

126126
The inverse of :func:`format_datetime`. Performs the same function as
127127
:func:`parsedate`, but on success returns a :mod:`~datetime.datetime`;
128-
otherwise ``TypeError`` is raised if parsing fails because of an invalid input,
129-
or a ``ValueError`` raised if *date* contains an invalid value such as an hour
130-
greater than 23 or a timezone offset not between -24 and 24 hours. If
131-
the input date has a timezone of ``-0000``, the ``datetime`` will be a naive
128+
otherwise ``ValueError`` is raised if *date* contains an invalid value such
129+
as an hour greater than 23 or a timezone offset not between -24 and 24 hours.
130+
If the input date has a timezone of ``-0000``, the ``datetime`` will be a naive
132131
``datetime``, and if the date is conforming to the RFCs it will represent a
133132
time in UTC but with no indication of the actual source timezone of the
134133
message the date comes from. If the input date has any other valid timezone

‎Lib/email/_parseaddr.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def _parsedate_tz(data):
6565
6666
"""
6767
if not data:
68-
return
68+
return None
6969
data = data.split()
7070
# The FWS after the comma after the day-of-week is optional, so search and
7171
# adjust for this.

‎Lib/email/headerregistry.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ def parse(cls, value, kwds):
305305
kwds['decoded'] = value
306306
try:
307307
value = utils.parsedate_to_datetime(value)
308-
except (ValueError, TypeError):
308+
except ValueError:
309309
kwds['defects'].append(errors.InvalidDateDefect('Invalid date value or format'))
310310
kwds['datetime'] = None
311311
kwds['parse_tree'] = parser.TokenList()

‎Lib/email/utils.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,10 @@ def make_msgid(idstring=None, domain=None):
195195

196196

197197
def parsedate_to_datetime(data):
198-
*dtuple, tz = _parsedate_tz(data)
198+
parsed_date_tz = _parsedate_tz(data)
199+
if parsed_date_tz is None:
200+
raise ValueError('Invalid date value or format "%s"' % str(data))
201+
*dtuple, tz = parsed_date_tz
199202
if tz is None:
200203
return datetime.datetime(*dtuple[:6])
201204
return datetime.datetime(*dtuple[:6],

‎Lib/test/test_email/test_utils.py‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,11 @@ def test_parsedate_to_datetime_naive(self):
4848
utils.parsedate_to_datetime(self.datestring + ' -0000'),
4949
self.naive_dt)
5050

51-
def test_parsedate_to_datetime_with_invalid_raises_typeerror(self):
52-
invalid_dates = ['', '0', 'A Complete Waste of Time']
53-
for dtstr in invalid_dates:
54-
with self.subTest(dtstr=dtstr):
55-
self.assertRaises(TypeError, utils.parsedate_to_datetime, dtstr)
56-
5751
def test_parsedate_to_datetime_with_invalid_raises_valueerror(self):
58-
invalid_dates = ['Tue, 06 Jun 2017 27:39:33 +0600',
52+
invalid_dates = ['',
53+
'0',
54+
'A Complete Waste of Time'
55+
'Tue, 06 Jun 2017 27:39:33 +0600',
5956
'Tue, 06 Jun 2017 07:39:33 +2600',
6057
'Tue, 06 Jun 2017 27:39:33']
6158
for dtstr in invalid_dates:

0 commit comments

Comments
 (0)