@@ -43,7 +43,7 @@ _PyLong_AsTime_t(PyObject *obj)
4343 val = PyLong_AsLongLong (obj );
4444#else
4545 long val ;
46- assert (sizeof (time_t ) <= sizeof (long ));
46+ Py_BUILD_ASSERT (sizeof (time_t ) <= sizeof (long ));
4747 val = PyLong_AsLong (obj );
4848#endif
4949 if (val == -1 && PyErr_Occurred ()) {
@@ -60,7 +60,7 @@ _PyLong_FromTime_t(time_t t)
6060#if defined(HAVE_LONG_LONG ) && SIZEOF_TIME_T == SIZEOF_LONG_LONG
6161 return PyLong_FromLongLong ((PY_LONG_LONG )t );
6262#else
63- assert (sizeof (time_t ) <= sizeof (long ));
63+ Py_BUILD_ASSERT (sizeof (time_t ) <= sizeof (long ));
6464 return PyLong_FromLong ((long )t );
6565#endif
6666}
@@ -209,6 +209,8 @@ _PyTime_FromSeconds(int seconds)
209209 /* ensure that integer overflow cannot happen, int type should have 32
210210 bits, whereas _PyTime_t type has at least 64 bits (SEC_TO_MS takes 30
211211 bits). */
212+ Py_BUILD_ASSERT (INT_MAX <= _PyTime_MAX / SEC_TO_NS );
213+ Py_BUILD_ASSERT (INT_MIN >= _PyTime_MIN / SEC_TO_NS );
212214 assert ((t >= 0 && t <= _PyTime_MAX / SEC_TO_NS )
213215 || (t < 0 && t >= _PyTime_MIN / SEC_TO_NS ));
214216 t *= SEC_TO_NS ;
@@ -219,7 +221,7 @@ _PyTime_t
219221_PyTime_FromNanoseconds (PY_LONG_LONG ns )
220222{
221223 _PyTime_t t ;
222- assert (sizeof (PY_LONG_LONG ) <= sizeof (_PyTime_t ));
224+ Py_BUILD_ASSERT (sizeof (PY_LONG_LONG ) <= sizeof (_PyTime_t ));
223225 t = Py_SAFE_DOWNCAST (ns , PY_LONG_LONG , _PyTime_t );
224226 return t ;
225227}
@@ -231,7 +233,7 @@ _PyTime_FromTimespec(_PyTime_t *tp, struct timespec *ts, int raise)
231233 _PyTime_t t ;
232234 int res = 0 ;
233235
234- assert (sizeof (ts -> tv_sec ) <= sizeof (_PyTime_t ));
236+ Py_BUILD_ASSERT (sizeof (ts -> tv_sec ) <= sizeof (_PyTime_t ));
235237 t = (_PyTime_t )ts -> tv_sec ;
236238
237239 if (_PyTime_check_mul_overflow (t , SEC_TO_NS )) {
@@ -253,7 +255,7 @@ _PyTime_FromTimeval(_PyTime_t *tp, struct timeval *tv, int raise)
253255 _PyTime_t t ;
254256 int res = 0 ;
255257
256- assert (sizeof (tv -> tv_sec ) <= sizeof (_PyTime_t ));
258+ Py_BUILD_ASSERT (sizeof (tv -> tv_sec ) <= sizeof (_PyTime_t ));
257259 t = (_PyTime_t )tv -> tv_sec ;
258260
259261 if (_PyTime_check_mul_overflow (t , SEC_TO_NS )) {
@@ -304,12 +306,12 @@ _PyTime_FromObject(_PyTime_t *t, PyObject *obj, _PyTime_round_t round,
304306 else {
305307#ifdef HAVE_LONG_LONG
306308 PY_LONG_LONG sec ;
307- assert (sizeof (PY_LONG_LONG ) <= sizeof (_PyTime_t ));
309+ Py_BUILD_ASSERT (sizeof (PY_LONG_LONG ) <= sizeof (_PyTime_t ));
308310
309311 sec = PyLong_AsLongLong (obj );
310312#else
311313 long sec ;
312- assert (sizeof (PY_LONG_LONG ) <= sizeof (_PyTime_t ));
314+ Py_BUILD_ASSERT (sizeof (PY_LONG_LONG ) <= sizeof (_PyTime_t ));
313315
314316 sec = PyLong_AsLong (obj );
315317#endif
@@ -364,10 +366,10 @@ PyObject *
364366_PyTime_AsNanosecondsObject (_PyTime_t t )
365367{
366368#ifdef HAVE_LONG_LONG
367- assert (sizeof (PY_LONG_LONG ) >= sizeof (_PyTime_t ));
369+ Py_BUILD_ASSERT (sizeof (PY_LONG_LONG ) >= sizeof (_PyTime_t ));
368370 return PyLong_FromLongLong ((PY_LONG_LONG )t );
369371#else
370- assert (sizeof (long ) >= sizeof (_PyTime_t ));
372+ Py_BUILD_ASSERT (sizeof (long ) >= sizeof (_PyTime_t ));
371373 return PyLong_FromLong ((long )t );
372374#endif
373375}
@@ -650,7 +652,7 @@ pymonotonic(_PyTime_t *tp, _Py_clock_info_t *info, int raise)
650652 assert (info == NULL || raise );
651653
652654 ticks = GetTickCount64 ();
653- assert (sizeof (ticks ) <= sizeof (_PyTime_t ));
655+ Py_BUILD_ASSERT (sizeof (ticks ) <= sizeof (_PyTime_t ));
654656 t = (_PyTime_t )ticks ;
655657
656658 if (_PyTime_check_mul_overflow (t , MS_TO_NS )) {
@@ -774,8 +776,5 @@ _PyTime_Init(void)
774776 if (_PyTime_GetMonotonicClockWithInfo (& t , NULL ) < 0 )
775777 return -1 ;
776778
777- /* check that _PyTime_FromSeconds() cannot overflow */
778- assert (INT_MAX <= _PyTime_MAX / SEC_TO_NS );
779- assert (INT_MIN >= _PyTime_MIN / SEC_TO_NS );
780779 return 0 ;
781780}
0 commit comments