Skip to content

Commit 1aca31e

Browse files
committed
Closes #15925: fix regression in parsedate() and parsedate_tz() that should return None if unable to parse the argument.
1 parent deb92b5 commit 1aca31e

File tree

4 files changed

+21
-24
lines changed

4 files changed

+21
-24
lines changed

Lib/email/_parseaddr.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ def parsedate_tz(data):
4848
Accounts for military timezones.
4949
"""
5050
res = _parsedate_tz(data)
51+
if not res:
52+
return
5153
if res[9] is None:
5254
res[9] = 0
5355
return tuple(res)
@@ -62,6 +64,8 @@ def _parsedate_tz(data):
6264
source timezone really was UTC.
6365
6466
"""
67+
if not data:
68+
return
6569
data = data.split()
6670
# The FWS after the comma after the day-of-week is optional, so search and
6771
# adjust for this.

Lib/email/utils.py

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,7 @@
3737
from email._parseaddr import AddressList as _AddressList
3838
from email._parseaddr import mktime_tz
3939

40-
# We need wormarounds for bugs in these methods in older Pythons (see below)
41-
from email._parseaddr import parsedate as _parsedate
42-
from email._parseaddr import parsedate_tz as _parsedate_tz
43-
from email._parseaddr import _parsedate_tz as __parsedate_tz
40+
from email._parseaddr import parsedate, parsedate_tz, _parsedate_tz
4441

4542
from quopri import decodestring as _qdecode
4643

@@ -222,25 +219,8 @@ def make_msgid(idstring=None, domain=None):
222219
return msgid
223220

224221

225-
226-
# These functions are in the standalone mimelib version only because they've
227-
# subsequently been fixed in the latest Python versions. We use this to worm
228-
# around broken older Pythons.
229-
def parsedate(data):
230-
if not data:
231-
return None
232-
return _parsedate(data)
233-
234-
235-
def parsedate_tz(data):
236-
if not data:
237-
return None
238-
return _parsedate_tz(data)
239-
240222
def parsedate_to_datetime(data):
241-
if not data:
242-
return None
243-
*dtuple, tz = __parsedate_tz(data)
223+
*dtuple, tz = _parsedate_tz(data)
244224
if tz is None:
245225
return datetime.datetime(*dtuple[:6])
246226
return datetime.datetime(*dtuple[:6],

Lib/test/test_email/test_email.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2718,8 +2718,17 @@ def test_formatdate_usegmt(self):
27182718
utils.formatdate(now, localtime=False, usegmt=True),
27192719
time.strftime('%a, %d %b %Y %H:%M:%S GMT', time.gmtime(now)))
27202720

2721-
def test_parsedate_none(self):
2722-
self.assertEqual(utils.parsedate(''), None)
2721+
# parsedate and parsedate_tz will become deprecated interfaces someday
2722+
def test_parsedate_returns_None_for_invalid_strings(self):
2723+
self.assertIsNone(utils.parsedate(''))
2724+
self.assertIsNone(utils.parsedate_tz(''))
2725+
self.assertIsNone(utils.parsedate('0'))
2726+
self.assertIsNone(utils.parsedate_tz('0'))
2727+
self.assertIsNone(utils.parsedate('A Complete Waste of Time'))
2728+
self.assertIsNone(utils.parsedate_tz('A Complete Waste of Time'))
2729+
# Not a part of the spec but, but this has historically worked:
2730+
self.assertIsNone(utils.parsedate(None))
2731+
self.assertIsNone(utils.parsedate_tz(None))
27232732

27242733
def test_parsedate_compact(self):
27252734
# The FWS after the comma is optional

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ Library
2222
with decimal.py: Infinity coefficients are undefined in _decimal
2323
(in accordance with the specification).
2424

25+
- Issue #15925: Fix a regression in email.util where the parsedate() and
26+
parsedate_tz() functions did not return None anymore when the argument could
27+
not be parsed.
28+
2529
Extension Modules
2630
-----------------
2731

0 commit comments

Comments
 (0)