gh-153406: Raise ValueError, not OverflowError, for out-of-range date… · python/cpython@37a26b9 · GitHub
Skip to content

Commit 37a26b9

Browse files
authored
gh-153406: Raise ValueError, not OverflowError, for out-of-range dates in email.utils.parsedate_to_datetime (#153407)
email.utils.parsedate_to_datetime documented that it raises ValueError for an invalid date, but it leaked OverflowError when the parsed year or timezone offset was too large for the datetime and timedelta constructors, and that OverflowError also escaped the modern header parsing path since DateHeader.parse only caught ValueError. Wrap the datetime and timezone construction so an OverflowError is re-raised as a ValueError with the original chained as the cause, which restores the documented contract and lets the existing header handler record an InvalidDateDefect instead of raising.
1 parent 11f1b70 commit 37a26b9

4 files changed

Lines changed: 27 additions & 4 deletions

File tree

Lib/email/utils.py

Lines changed: 7 additions & 4 deletions

Lib/test/test_email/test_headerregistry.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,14 @@ def test_invalid_date_value(self):
221221
self.assertEqual(len(h.defects), 1)
222222
self.assertIsInstance(h.defects[0], errors.InvalidDateDefect)
223223

224+
def test_out_of_range_date_value(self):
225+
s = 'Mon, 20 Nov 9999999999 12:00:00 +0000'
226+
h = self.make_header('date', s)
227+
self.assertEqual(h, s)
228+
self.assertIsNone(h.datetime)
229+
self.assertEqual(len(h.defects), 1)
230+
self.assertIsInstance(h.defects[0], errors.InvalidDateDefect)
231+
224232
def test_datetime_read_only(self):
225233
h = self.make_header('date', self.datestring)
226234
with self.assertRaises(AttributeError):

Lib/test/test_email/test_utils.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,15 @@ def test_parsedate_to_datetime_with_invalid_raises_valueerror(self):
7777
with self.subTest(dtstr=dtstr):
7878
self.assertRaises(ValueError, utils.parsedate_to_datetime, dtstr)
7979

80+
def test_parsedate_to_datetime_out_of_range_raises_valueerror(self):
81+
out_of_range_dates = [
82+
'Mon, 20 Nov 9999999999 12:00:00 +0000',
83+
'Mon, 20 Nov 2017 12:00:00 +24000000000000',
84+
]
85+
for dtstr in out_of_range_dates:
86+
with self.subTest(dtstr=dtstr):
87+
self.assertRaises(ValueError, utils.parsedate_to_datetime, dtstr)
88+
8089
class LocaltimeTests(unittest.TestCase):
8190

8291
def test_localtime_is_tz_aware_daylight_true(self):
Lines changed: 3 additions & 0 deletions

0 commit comments

Comments
 (0)