Skip to content

Commit 4237d34

Browse files
committed
Fix test_time on platform with 32-bit time_t type
Filter values which would overflow when converted to a C time_t type.
1 parent 9c72f9b commit 4237d34

2 files changed

Lines changed: 19 additions & 5 deletions

File tree

Lib/test/test_time.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,15 @@ class CPyTimeTestCase:
680680
"""
681681
OVERFLOW_SECONDS = None
682682

683+
def setUp(self):
684+
from _testcapi import SIZEOF_TIME_T
685+
bits = SIZEOF_TIME_T * 8 - 1
686+
self.time_t_min = -2 ** bits
687+
self.time_t_max = 2 ** bits - 1
688+
689+
def time_t_filter(self, seconds):
690+
return (self.time_t_min <= seconds <= self.time_t_max)
691+
683692
def _rounding_values(self, use_float):
684693
"Build timestamps used to test rounding."
685694

@@ -858,7 +867,7 @@ def timeval_converter(ns):
858867
def seconds_filter(secs):
859868
return LONG_MIN <= secs <= LONG_MAX
860869
else:
861-
seconds_filter = None
870+
seconds_filter = self.time_t_filter
862871

863872
self.check_int_rounding(PyTime_AsTimeval,
864873
timeval_converter,
@@ -875,7 +884,8 @@ def timespec_converter(ns):
875884

876885
self.check_int_rounding(lambda ns, rnd: PyTime_AsTimespec(ns),
877886
timespec_converter,
878-
NS_TO_SEC)
887+
NS_TO_SEC,
888+
value_filter=self.time_t_filter)
879889

880890
def test_AsMilliseconds(self):
881891
from _testcapi import PyTime_AsMilliseconds
@@ -904,7 +914,8 @@ def test_object_to_time_t(self):
904914
from _testcapi import pytime_object_to_time_t
905915

906916
self.check_int_rounding(pytime_object_to_time_t,
907-
lambda secs: secs)
917+
lambda secs: secs,
918+
value_filter=self.time_t_filter)
908919

909920
self.check_float_rounding(pytime_object_to_time_t,
910921
self.decimal_round)
@@ -928,7 +939,8 @@ def test_object_to_timeval(self):
928939
from _testcapi import pytime_object_to_timeval
929940

930941
self.check_int_rounding(pytime_object_to_timeval,
931-
lambda secs: (secs, 0))
942+
lambda secs: (secs, 0),
943+
value_filter=self.time_t_filter)
932944

933945
self.check_float_rounding(pytime_object_to_timeval,
934946
self.create_converter(SEC_TO_US))
@@ -937,7 +949,8 @@ def test_object_to_timespec(self):
937949
from _testcapi import pytime_object_to_timespec
938950

939951
self.check_int_rounding(pytime_object_to_timespec,
940-
lambda secs: (secs, 0))
952+
lambda secs: (secs, 0),
953+
value_filter=self.time_t_filter)
941954

942955
self.check_float_rounding(pytime_object_to_timespec,
943956
self.create_converter(SEC_TO_NS))

Modules/_testcapimodule.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4114,6 +4114,7 @@ PyInit__testcapi(void)
41144114
PyModule_AddObject(m, "PY_SSIZE_T_MAX", PyLong_FromSsize_t(PY_SSIZE_T_MAX));
41154115
PyModule_AddObject(m, "PY_SSIZE_T_MIN", PyLong_FromSsize_t(PY_SSIZE_T_MIN));
41164116
PyModule_AddObject(m, "SIZEOF_PYGC_HEAD", PyLong_FromSsize_t(sizeof(PyGC_Head)));
4117+
PyModule_AddObject(m, "SIZEOF_TIME_T", PyLong_FromSsize_t(sizeof(time_t)));
41174118
Py_INCREF(&PyInstanceMethod_Type);
41184119
PyModule_AddObject(m, "instancemethod", (PyObject *)&PyInstanceMethod_Type);
41194120

0 commit comments

Comments
 (0)