mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
Closes #15925: fix regression in parsedate() and parsedate_tz() that should return None if unable to parse the argument.
This commit is contained in:
parent
deb92b5b1b
commit
1aca31e8f3
4 changed files with 21 additions and 24 deletions
|
@ -48,6 +48,8 @@ def parsedate_tz(data):
|
||||||
Accounts for military timezones.
|
Accounts for military timezones.
|
||||||
"""
|
"""
|
||||||
res = _parsedate_tz(data)
|
res = _parsedate_tz(data)
|
||||||
|
if not res:
|
||||||
|
return
|
||||||
if res[9] is None:
|
if res[9] is None:
|
||||||
res[9] = 0
|
res[9] = 0
|
||||||
return tuple(res)
|
return tuple(res)
|
||||||
|
@ -62,6 +64,8 @@ def _parsedate_tz(data):
|
||||||
source timezone really was UTC.
|
source timezone really was UTC.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
if not data:
|
||||||
|
return
|
||||||
data = data.split()
|
data = data.split()
|
||||||
# The FWS after the comma after the day-of-week is optional, so search and
|
# The FWS after the comma after the day-of-week is optional, so search and
|
||||||
# adjust for this.
|
# adjust for this.
|
||||||
|
|
|
@ -37,10 +37,7 @@ from email._parseaddr import quote
|
||||||
from email._parseaddr import AddressList as _AddressList
|
from email._parseaddr import AddressList as _AddressList
|
||||||
from email._parseaddr import mktime_tz
|
from email._parseaddr import mktime_tz
|
||||||
|
|
||||||
# We need wormarounds for bugs in these methods in older Pythons (see below)
|
from email._parseaddr import parsedate, parsedate_tz, _parsedate_tz
|
||||||
from email._parseaddr import parsedate as _parsedate
|
|
||||||
from email._parseaddr import parsedate_tz as _parsedate_tz
|
|
||||||
from email._parseaddr import _parsedate_tz as __parsedate_tz
|
|
||||||
|
|
||||||
from quopri import decodestring as _qdecode
|
from quopri import decodestring as _qdecode
|
||||||
|
|
||||||
|
@ -222,25 +219,8 @@ def make_msgid(idstring=None, domain=None):
|
||||||
return msgid
|
return msgid
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# These functions are in the standalone mimelib version only because they've
|
|
||||||
# subsequently been fixed in the latest Python versions. We use this to worm
|
|
||||||
# around broken older Pythons.
|
|
||||||
def parsedate(data):
|
|
||||||
if not data:
|
|
||||||
return None
|
|
||||||
return _parsedate(data)
|
|
||||||
|
|
||||||
|
|
||||||
def parsedate_tz(data):
|
|
||||||
if not data:
|
|
||||||
return None
|
|
||||||
return _parsedate_tz(data)
|
|
||||||
|
|
||||||
def parsedate_to_datetime(data):
|
def parsedate_to_datetime(data):
|
||||||
if not data:
|
*dtuple, tz = _parsedate_tz(data)
|
||||||
return None
|
|
||||||
*dtuple, tz = __parsedate_tz(data)
|
|
||||||
if tz is None:
|
if tz is None:
|
||||||
return datetime.datetime(*dtuple[:6])
|
return datetime.datetime(*dtuple[:6])
|
||||||
return datetime.datetime(*dtuple[:6],
|
return datetime.datetime(*dtuple[:6],
|
||||||
|
|
|
@ -2718,8 +2718,17 @@ class TestMiscellaneous(TestEmailBase):
|
||||||
utils.formatdate(now, localtime=False, usegmt=True),
|
utils.formatdate(now, localtime=False, usegmt=True),
|
||||||
time.strftime('%a, %d %b %Y %H:%M:%S GMT', time.gmtime(now)))
|
time.strftime('%a, %d %b %Y %H:%M:%S GMT', time.gmtime(now)))
|
||||||
|
|
||||||
def test_parsedate_none(self):
|
# parsedate and parsedate_tz will become deprecated interfaces someday
|
||||||
self.assertEqual(utils.parsedate(''), None)
|
def test_parsedate_returns_None_for_invalid_strings(self):
|
||||||
|
self.assertIsNone(utils.parsedate(''))
|
||||||
|
self.assertIsNone(utils.parsedate_tz(''))
|
||||||
|
self.assertIsNone(utils.parsedate('0'))
|
||||||
|
self.assertIsNone(utils.parsedate_tz('0'))
|
||||||
|
self.assertIsNone(utils.parsedate('A Complete Waste of Time'))
|
||||||
|
self.assertIsNone(utils.parsedate_tz('A Complete Waste of Time'))
|
||||||
|
# Not a part of the spec but, but this has historically worked:
|
||||||
|
self.assertIsNone(utils.parsedate(None))
|
||||||
|
self.assertIsNone(utils.parsedate_tz(None))
|
||||||
|
|
||||||
def test_parsedate_compact(self):
|
def test_parsedate_compact(self):
|
||||||
# The FWS after the comma is optional
|
# The FWS after the comma is optional
|
||||||
|
|
|
@ -22,6 +22,10 @@ Library
|
||||||
with decimal.py: Infinity coefficients are undefined in _decimal
|
with decimal.py: Infinity coefficients are undefined in _decimal
|
||||||
(in accordance with the specification).
|
(in accordance with the specification).
|
||||||
|
|
||||||
|
- Issue #15925: Fix a regression in email.util where the parsedate() and
|
||||||
|
parsedate_tz() functions did not return None anymore when the argument could
|
||||||
|
not be parsed.
|
||||||
|
|
||||||
Extension Modules
|
Extension Modules
|
||||||
-----------------
|
-----------------
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue