@@ -43,7 +43,7 @@ _PyLong_AsTime_t(PyObject *obj)
43
43
val = PyLong_AsLongLong (obj );
44
44
#else
45
45
long val ;
46
- assert (sizeof (time_t ) <= sizeof (long ));
46
+ Py_BUILD_ASSERT (sizeof (time_t ) <= sizeof (long ));
47
47
val = PyLong_AsLong (obj );
48
48
#endif
49
49
if (val == -1 && PyErr_Occurred ()) {
@@ -60,7 +60,7 @@ _PyLong_FromTime_t(time_t t)
60
60
#if defined(HAVE_LONG_LONG ) && SIZEOF_TIME_T == SIZEOF_LONG_LONG
61
61
return PyLong_FromLongLong ((PY_LONG_LONG )t );
62
62
#else
63
- assert (sizeof (time_t ) <= sizeof (long ));
63
+ Py_BUILD_ASSERT (sizeof (time_t ) <= sizeof (long ));
64
64
return PyLong_FromLong ((long )t );
65
65
#endif
66
66
}
@@ -209,6 +209,8 @@ _PyTime_FromSeconds(int seconds)
209
209
/* ensure that integer overflow cannot happen, int type should have 32
210
210
bits, whereas _PyTime_t type has at least 64 bits (SEC_TO_MS takes 30
211
211
bits). */
212
+ Py_BUILD_ASSERT (INT_MAX <= _PyTime_MAX / SEC_TO_NS );
213
+ Py_BUILD_ASSERT (INT_MIN >= _PyTime_MIN / SEC_TO_NS );
212
214
assert ((t >= 0 && t <= _PyTime_MAX / SEC_TO_NS )
213
215
|| (t < 0 && t >= _PyTime_MIN / SEC_TO_NS ));
214
216
t *= SEC_TO_NS ;
@@ -219,7 +221,7 @@ _PyTime_t
219
221
_PyTime_FromNanoseconds (PY_LONG_LONG ns )
220
222
{
221
223
_PyTime_t t ;
222
- assert (sizeof (PY_LONG_LONG ) <= sizeof (_PyTime_t ));
224
+ Py_BUILD_ASSERT (sizeof (PY_LONG_LONG ) <= sizeof (_PyTime_t ));
223
225
t = Py_SAFE_DOWNCAST (ns , PY_LONG_LONG , _PyTime_t );
224
226
return t ;
225
227
}
@@ -231,7 +233,7 @@ _PyTime_FromTimespec(_PyTime_t *tp, struct timespec *ts, int raise)
231
233
_PyTime_t t ;
232
234
int res = 0 ;
233
235
234
- assert (sizeof (ts -> tv_sec ) <= sizeof (_PyTime_t ));
236
+ Py_BUILD_ASSERT (sizeof (ts -> tv_sec ) <= sizeof (_PyTime_t ));
235
237
t = (_PyTime_t )ts -> tv_sec ;
236
238
237
239
if (_PyTime_check_mul_overflow (t , SEC_TO_NS )) {
@@ -253,7 +255,7 @@ _PyTime_FromTimeval(_PyTime_t *tp, struct timeval *tv, int raise)
253
255
_PyTime_t t ;
254
256
int res = 0 ;
255
257
256
- assert (sizeof (tv -> tv_sec ) <= sizeof (_PyTime_t ));
258
+ Py_BUILD_ASSERT (sizeof (tv -> tv_sec ) <= sizeof (_PyTime_t ));
257
259
t = (_PyTime_t )tv -> tv_sec ;
258
260
259
261
if (_PyTime_check_mul_overflow (t , SEC_TO_NS )) {
@@ -304,12 +306,12 @@ _PyTime_FromObject(_PyTime_t *t, PyObject *obj, _PyTime_round_t round,
304
306
else {
305
307
#ifdef HAVE_LONG_LONG
306
308
PY_LONG_LONG sec ;
307
- assert (sizeof (PY_LONG_LONG ) <= sizeof (_PyTime_t ));
309
+ Py_BUILD_ASSERT (sizeof (PY_LONG_LONG ) <= sizeof (_PyTime_t ));
308
310
309
311
sec = PyLong_AsLongLong (obj );
310
312
#else
311
313
long sec ;
312
- assert (sizeof (PY_LONG_LONG ) <= sizeof (_PyTime_t ));
314
+ Py_BUILD_ASSERT (sizeof (PY_LONG_LONG ) <= sizeof (_PyTime_t ));
313
315
314
316
sec = PyLong_AsLong (obj );
315
317
#endif
@@ -364,10 +366,10 @@ PyObject *
364
366
_PyTime_AsNanosecondsObject (_PyTime_t t )
365
367
{
366
368
#ifdef HAVE_LONG_LONG
367
- assert (sizeof (PY_LONG_LONG ) >= sizeof (_PyTime_t ));
369
+ Py_BUILD_ASSERT (sizeof (PY_LONG_LONG ) >= sizeof (_PyTime_t ));
368
370
return PyLong_FromLongLong ((PY_LONG_LONG )t );
369
371
#else
370
- assert (sizeof (long ) >= sizeof (_PyTime_t ));
372
+ Py_BUILD_ASSERT (sizeof (long ) >= sizeof (_PyTime_t ));
371
373
return PyLong_FromLong ((long )t );
372
374
#endif
373
375
}
@@ -650,7 +652,7 @@ pymonotonic(_PyTime_t *tp, _Py_clock_info_t *info, int raise)
650
652
assert (info == NULL || raise );
651
653
652
654
ticks = GetTickCount64 ();
653
- assert (sizeof (ticks ) <= sizeof (_PyTime_t ));
655
+ Py_BUILD_ASSERT (sizeof (ticks ) <= sizeof (_PyTime_t ));
654
656
t = (_PyTime_t )ticks ;
655
657
656
658
if (_PyTime_check_mul_overflow (t , MS_TO_NS )) {
@@ -774,8 +776,5 @@ _PyTime_Init(void)
774
776
if (_PyTime_GetMonotonicClockWithInfo (& t , NULL ) < 0 )
775
777
return -1 ;
776
778
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 );
780
779
return 0 ;
781
780
}
0 commit comments