return NULL;
PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode));
- Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(st->st_ino));
+ static_assert(sizeof(unsigned long long) >= sizeof(st->st_ino),
+ "stat.st_ino is larger than unsigned long long");
PyStructSequence_SET_ITEM(v, 1, PyLong_FromUnsignedLongLong(st->st_ino));
#ifdef MS_WINDOWS
PyStructSequence_SET_ITEM(v, 2, PyLong_FromUnsignedLong(st->st_dev));
PyStructSequence_SET_ITEM(v, 4, _PyLong_FromUid(st->st_uid));
PyStructSequence_SET_ITEM(v, 5, _PyLong_FromGid(st->st_gid));
#endif
- Py_BUILD_ASSERT(sizeof(long long) >= sizeof(st->st_size));
+ static_assert(sizeof(long long) >= sizeof(st->st_size),
+ "stat.st_size is larger than long long");
PyStructSequence_SET_ITEM(v, 6, PyLong_FromLongLong(st->st_size));
#if defined(HAVE_STAT_TV_NSEC)
self->win32_file_index = stat.st_ino;
self->got_file_index = 1;
}
- Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(self->win32_file_index));
+ static_assert(sizeof(unsigned long long) >= sizeof(self->win32_file_index),
+ "DirEntry.win32_file_index is larger than unsigned long long");
return PyLong_FromUnsignedLongLong(self->win32_file_index);
#else /* POSIX */
- Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(self->d_ino));
+ static_assert(sizeof(unsigned long long) >= sizeof(self->d_ino),
+ "DirEntry.d_ino is larger than unsigned long long");
return PyLong_FromUnsignedLongLong(self->d_ino);
#endif
}
_PyLong_AsTime_t(PyObject *obj)
{
#if SIZEOF_TIME_T == SIZEOF_LONG_LONG
- long long val;
- val = PyLong_AsLongLong(obj);
+ long long val = PyLong_AsLongLong(obj);
+#elif SIZEOF_TIME_T <= SIZEOF_LONG
+ long val = PyLong_AsLong(obj);
#else
- long val;
- Py_BUILD_ASSERT(sizeof(time_t) <= sizeof(long));
- val = PyLong_AsLong(obj);
+# error "unsupported time_t size"
#endif
if (val == -1 && PyErr_Occurred()) {
if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
{
#if SIZEOF_TIME_T == SIZEOF_LONG_LONG
return PyLong_FromLongLong((long long)t);
-#else
- Py_BUILD_ASSERT(sizeof(time_t) <= sizeof(long));
+#elif SIZEOF_TIME_T <= SIZEOF_LONG
return PyLong_FromLong((long)t);
+#else
+# error "unsupported time_t size"
#endif
}
_PyTime_FromSeconds(int seconds)
{
/* ensure that integer overflow cannot happen, int type should have 32
- bits, whereas _PyTime_t type has at least 64 bits (SEC_TO_MS takes 30
+ bits, whereas _PyTime_t type has at least 64 bits (SEC_TO_NS takes 30
bits). */
- Py_BUILD_ASSERT(INT_MAX <= _PyTime_MAX / SEC_TO_NS);
- Py_BUILD_ASSERT(INT_MIN >= _PyTime_MIN / SEC_TO_NS);
+ static_assert(INT_MAX <= _PyTime_MAX / SEC_TO_NS, "_PyTime_t overflow");
+ static_assert(INT_MIN >= _PyTime_MIN / SEC_TO_NS, "_PyTime_t underflow");
_PyTime_t t = (_PyTime_t)seconds;
assert((t >= 0 && t <= _PyTime_MAX / SEC_TO_NS)
return -1;
}
- Py_BUILD_ASSERT(sizeof(long long) == sizeof(_PyTime_t));
+ static_assert(sizeof(long long) == sizeof(_PyTime_t),
+ "_PyTime_t is not long long");
long long nsec = PyLong_AsLongLong(obj);
if (nsec == -1 && PyErr_Occurred()) {
if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
{
_PyTime_t t, tv_nsec;
- Py_BUILD_ASSERT(sizeof(ts->tv_sec) <= sizeof(_PyTime_t));
+ static_assert(sizeof(ts->tv_sec) <= sizeof(_PyTime_t),
+ "timespec.tv_sec is larger than _PyTime_t");
t = (_PyTime_t)ts->tv_sec;
int res1 = pytime_mul(&t, SEC_TO_NS);
static int
pytime_fromtimeval(_PyTime_t *tp, struct timeval *tv, int raise_exc)
{
- Py_BUILD_ASSERT(sizeof(tv->tv_sec) <= sizeof(_PyTime_t));
+ static_assert(sizeof(tv->tv_sec) <= sizeof(_PyTime_t),
+ "timeval.tv_sec is larger than _PyTime_t");
_PyTime_t t = (_PyTime_t)tv->tv_sec;
int res1 = pytime_mul(&t, SEC_TO_NS);
return -1;
}
- Py_BUILD_ASSERT(sizeof(long long) <= sizeof(_PyTime_t));
+ static_assert(sizeof(long long) <= sizeof(_PyTime_t),
+ "_PyTime_t is smaller than long long");
_PyTime_t ns = (_PyTime_t)sec;
if (pytime_mul(&ns, unit_to_ns) < 0) {
pytime_overflow();
_PyTime_AsNanosecondsObject(_PyTime_t t)
{
_PyTime_t ns = pytime_as_nanoseconds(t);
- Py_BUILD_ASSERT(sizeof(long long) >= sizeof(_PyTime_t));
+ static_assert(sizeof(long long) >= sizeof(_PyTime_t),
+ "_PyTime_t is larger than long long");
return PyLong_FromLongLong((long long)ns);
}
_PyTime_t. In practice, timebase uses uint32_t, so casting cannot
overflow. At the end, only make sure that the type is uint32_t
(_PyTime_t is 64-bit long). */
- Py_BUILD_ASSERT(sizeof(timebase.numer) < sizeof(_PyTime_t));
- Py_BUILD_ASSERT(sizeof(timebase.denom) < sizeof(_PyTime_t));
+ static_assert(sizeof(timebase.numer) <= sizeof(_PyTime_t),
+ "timebase.numer is larger than _PyTime_t");
+ static_assert(sizeof(timebase.denom) <= sizeof(_PyTime_t),
+ "timebase.denom is larger than _PyTime_t");
- /* Make sure that (ticks * timebase.numer) cannot overflow in
- _PyTime_MulDiv(), with ticks < timebase.denom.
+ /* Make sure that _PyTime_MulDiv(ticks, timebase_numer, timebase_denom)
+ cannot overflow.
Known time bases:
- * always (1, 1) on Intel
+ * (1, 1) on Intel
* (1000000000, 33333335) or (1000000000, 25000000) on PowerPC
None of these time bases can overflow with 64-bit _PyTime_t, but
#if defined(MS_WINDOWS)
ULONGLONG ticks = GetTickCount64();
- Py_BUILD_ASSERT(sizeof(ticks) <= sizeof(_PyTime_t));
- _PyTime_t t = (_PyTime_t)ticks;
+ static_assert(sizeof(ticks) <= sizeof(_PyTime_t),
+ "ULONGLONG is larger than _PyTime_t");
+ _PyTime_t t;
+ if (ticks <= (ULONGLONG)_PyTime_MAX) {
+ t = (_PyTime_t)ticks;
+ }
+ else {
+ // GetTickCount64() maximum is larger than _PyTime_t maximum:
+ // ULONGLONG is unsigned, whereas _PyTime_t is signed.
+ t = _PyTime_MAX;
+ }
int res = pytime_mul(&t, MS_TO_NS);
*tp = t;
/* Make sure that casting LONGLONG to _PyTime_t cannot overflow,
both types are signed */
_PyTime_t ticks;
- Py_BUILD_ASSERT(sizeof(ticksll) <= sizeof(ticks));
+ static_assert(sizeof(ticksll) <= sizeof(ticks),
+ "LONGLONG is larger than _PyTime_t");
ticks = (_PyTime_t)ticksll;
_PyTime_t ns = _PyTime_MulDiv(ticks, SEC_TO_NS, (_PyTime_t)frequency);