From 13019fdef3b6d589f8d7602943c76fcc637114ea Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Fri, 3 Apr 2015 13:10:54 +0200 Subject: Issue #22117: Add a new _PyTime_FromSeconds() function Fix also _Py_InitializeEx_Private(): initialize time before initializing import, import_init() uses the _PyTime API (for thread locks). --- Python/pytime.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'Python/pytime.c') diff --git a/Python/pytime.c b/Python/pytime.c index 491bbea611..02a93749c7 100644 --- a/Python/pytime.c +++ b/Python/pytime.c @@ -158,6 +158,19 @@ _PyTime_overflow(void) "timestamp too large to convert to C _PyTime_t"); } +_PyTime_t +_PyTime_FromSeconds(int seconds) +{ + _PyTime_t t; + /* 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). */ + assert((seconds >= 0 && seconds <= _PyTime_MAX / SEC_TO_NS) + || (seconds < 0 && seconds >= _PyTime_MIN / SEC_TO_NS)); + t = (_PyTime_t)seconds * SEC_TO_NS; + return t; +} + _PyTime_t _PyTime_FromNanoseconds(PY_LONG_LONG ns) { @@ -657,5 +670,9 @@ _PyTime_Init(void) /* ensure that the operating system provides a monotonic clock */ if (_PyTime_GetMonotonicClockWithInfo(&t, NULL) < 0) return -1; + + /* check that _PyTime_FromSeconds() cannot overflow */ + assert(INT_MAX <= _PyTime_MAX / SEC_TO_NS); + assert(INT_MIN >= _PyTime_MIN / SEC_TO_NS); return 0; } -- cgit v1.2.1