Skip to content

Commit 7b178f6

Browse files
miss-islingtontonghuarootStanFromIreland
authored
[3.13] gh-152849: Fix OverflowError message for out-of-range timestamps in the time module (GH-152850) (#153207)
(cherry picked from commit 2d7a74e) Co-authored-by: tonghuaroot (童话) <tonghuaroot@gmail.com> Co-authored-by: Stan Ulbrych <stan@python.org>
1 parent b06069b commit 7b178f6

3 files changed

Lines changed: 15 additions & 2 deletions

File tree

Lib/test/test_time.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -974,6 +974,17 @@ def test_FromSecondsObject(self):
974974
with self.assertRaises(ValueError):
975975
_PyTime_FromSecondsObject(float('nan'), time_rnd)
976976

977+
def test_FromSecondsObject_float_overflow_message(self):
978+
# Float path must report a PyTime_t overflow, like the integer path.
979+
from _testinternalcapi import _PyTime_FromSecondsObject
980+
for value in (PyTime_MAX, PyTime_MIN):
981+
for time_rnd, _ in ROUNDING_MODES:
982+
with self.subTest(value=value, time_rnd=time_rnd):
983+
with self.assertRaisesRegex(
984+
OverflowError,
985+
"timestamp out of range for C PyTime_t"):
986+
_PyTime_FromSecondsObject(value, time_rnd)
987+
977988
def test_AsSecondsDouble(self):
978989
from _testcapi import PyTime_AsSecondsDouble
979990

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Out-of-range float and integer timestamps now raise :exc:`OverflowError`
2+
with the same message. Patch by tonghuaroot.

Python/pytime.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ static void
112112
pytime_overflow(void)
113113
{
114114
PyErr_SetString(PyExc_OverflowError,
115-
"timestamp too large to convert to C PyTime_t");
115+
"timestamp out of range for C PyTime_t");
116116
}
117117

118118

@@ -572,7 +572,7 @@ pytime_from_double(PyTime_t *tp, double value, _PyTime_round_t round,
572572

573573
/* See comments in pytime_double_to_denominator */
574574
if (!((double)PyTime_MIN <= d && d < -(double)PyTime_MIN)) {
575-
pytime_time_t_overflow();
575+
pytime_overflow();
576576
*tp = 0;
577577
return -1;
578578
}

0 commit comments

Comments
 (0)