Fix test_time on platform with 32-bit time_t type

Filter values which would overflow when converted to a C time_t type.
This commit is contained in:
Victor Stinner 2015-09-10 10:10:39 +02:00
parent 9c72f9b30a
commit 4237d3474c
2 changed files with 19 additions and 5 deletions

View File

@ -680,6 +680,15 @@ class CPyTimeTestCase:
""" """
OVERFLOW_SECONDS = None OVERFLOW_SECONDS = None
def setUp(self):
from _testcapi import SIZEOF_TIME_T
bits = SIZEOF_TIME_T * 8 - 1
self.time_t_min = -2 ** bits
self.time_t_max = 2 ** bits - 1
def time_t_filter(self, seconds):
return (self.time_t_min <= seconds <= self.time_t_max)
def _rounding_values(self, use_float): def _rounding_values(self, use_float):
"Build timestamps used to test rounding." "Build timestamps used to test rounding."
@ -858,7 +867,7 @@ class TestCPyTime(CPyTimeTestCase, unittest.TestCase):
def seconds_filter(secs): def seconds_filter(secs):
return LONG_MIN <= secs <= LONG_MAX return LONG_MIN <= secs <= LONG_MAX
else: else:
seconds_filter = None seconds_filter = self.time_t_filter
self.check_int_rounding(PyTime_AsTimeval, self.check_int_rounding(PyTime_AsTimeval,
timeval_converter, timeval_converter,
@ -875,7 +884,8 @@ class TestCPyTime(CPyTimeTestCase, unittest.TestCase):
self.check_int_rounding(lambda ns, rnd: PyTime_AsTimespec(ns), self.check_int_rounding(lambda ns, rnd: PyTime_AsTimespec(ns),
timespec_converter, timespec_converter,
NS_TO_SEC) NS_TO_SEC,
value_filter=self.time_t_filter)
def test_AsMilliseconds(self): def test_AsMilliseconds(self):
from _testcapi import PyTime_AsMilliseconds from _testcapi import PyTime_AsMilliseconds
@ -904,7 +914,8 @@ class TestOldPyTime(CPyTimeTestCase, unittest.TestCase):
from _testcapi import pytime_object_to_time_t from _testcapi import pytime_object_to_time_t
self.check_int_rounding(pytime_object_to_time_t, self.check_int_rounding(pytime_object_to_time_t,
lambda secs: secs) lambda secs: secs,
value_filter=self.time_t_filter)
self.check_float_rounding(pytime_object_to_time_t, self.check_float_rounding(pytime_object_to_time_t,
self.decimal_round) self.decimal_round)
@ -928,7 +939,8 @@ class TestOldPyTime(CPyTimeTestCase, unittest.TestCase):
from _testcapi import pytime_object_to_timeval from _testcapi import pytime_object_to_timeval
self.check_int_rounding(pytime_object_to_timeval, self.check_int_rounding(pytime_object_to_timeval,
lambda secs: (secs, 0)) lambda secs: (secs, 0),
value_filter=self.time_t_filter)
self.check_float_rounding(pytime_object_to_timeval, self.check_float_rounding(pytime_object_to_timeval,
self.create_converter(SEC_TO_US)) self.create_converter(SEC_TO_US))
@ -937,7 +949,8 @@ class TestOldPyTime(CPyTimeTestCase, unittest.TestCase):
from _testcapi import pytime_object_to_timespec from _testcapi import pytime_object_to_timespec
self.check_int_rounding(pytime_object_to_timespec, self.check_int_rounding(pytime_object_to_timespec,
lambda secs: (secs, 0)) lambda secs: (secs, 0),
value_filter=self.time_t_filter)
self.check_float_rounding(pytime_object_to_timespec, self.check_float_rounding(pytime_object_to_timespec,
self.create_converter(SEC_TO_NS)) self.create_converter(SEC_TO_NS))

View File

@ -4114,6 +4114,7 @@ PyInit__testcapi(void)
PyModule_AddObject(m, "PY_SSIZE_T_MAX", PyLong_FromSsize_t(PY_SSIZE_T_MAX)); PyModule_AddObject(m, "PY_SSIZE_T_MAX", PyLong_FromSsize_t(PY_SSIZE_T_MAX));
PyModule_AddObject(m, "PY_SSIZE_T_MIN", PyLong_FromSsize_t(PY_SSIZE_T_MIN)); PyModule_AddObject(m, "PY_SSIZE_T_MIN", PyLong_FromSsize_t(PY_SSIZE_T_MIN));
PyModule_AddObject(m, "SIZEOF_PYGC_HEAD", PyLong_FromSsize_t(sizeof(PyGC_Head))); PyModule_AddObject(m, "SIZEOF_PYGC_HEAD", PyLong_FromSsize_t(sizeof(PyGC_Head)));
PyModule_AddObject(m, "SIZEOF_TIME_T", PyLong_FromSsize_t(sizeof(time_t)));
Py_INCREF(&PyInstanceMethod_Type); Py_INCREF(&PyInstanceMethod_Type);
PyModule_AddObject(m, "instancemethod", (PyObject *)&PyInstanceMethod_Type); PyModule_AddObject(m, "instancemethod", (PyObject *)&PyInstanceMethod_Type);