Skip to content

Commit 2d7a74e

Browse files
gh-152849: Fix OverflowError message for out-of-range timestamps in the time module (#152850)
Co-authored-by: Stan Ulbrych <stan@python.org>
1 parent 8477652 commit 2d7a74e

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
@@ -1013,6 +1013,17 @@ def test_FromSecondsObject(self):
10131013
with self.assertRaises(ValueError):
10141014
_PyTime_FromSecondsObject(float('nan'), time_rnd)
10151015

1016+
def test_FromSecondsObject_float_overflow_message(self):
1017+
# Float path must report a PyTime_t overflow, like the integer path.
1018+
from _testinternalcapi import _PyTime_FromSecondsObject
1019+
for value in (PyTime_MAX, PyTime_MIN):
1020+
for time_rnd, _ in ROUNDING_MODES:
1021+
with self.subTest(value=value, time_rnd=time_rnd):
1022+
with self.assertRaisesRegex(
1023+
OverflowError,
1024+
"timestamp out of range for C PyTime_t"):
1025+
_PyTime_FromSecondsObject(value, time_rnd)
1026+
10161027
def test_AsSecondsDouble(self):
10171028
from _testcapi import PyTime_AsSecondsDouble
10181029

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
@@ -107,7 +107,7 @@ static void
107107
pytime_overflow(void)
108108
{
109109
PyErr_SetString(PyExc_OverflowError,
110-
"timestamp too large to convert to C PyTime_t");
110+
"timestamp out of range for C PyTime_t");
111111
}
112112

113113

@@ -636,7 +636,7 @@ pytime_from_double(PyTime_t *tp, double value, _PyTime_round_t round,
636636

637637
/* See comments in pytime_double_to_denominator */
638638
if (!((double)PyTime_MIN <= d && d < -(double)PyTime_MIN)) {
639-
pytime_time_t_overflow();
639+
pytime_overflow();
640640
*tp = 0;
641641
return -1;
642642
}

0 commit comments

Comments
 (0)