diff --git a/Doc/c-api/time.rst b/Doc/c-api/time.rst index 17ddbc5f9cd3e75..5cfdef71b3e191e 100644 --- a/Doc/c-api/time.rst +++ b/Doc/c-api/time.rst @@ -78,8 +78,12 @@ Raw Clock Functions Similar to clock functions, but don't set an exception on error and don't require the caller to hold the GIL. -The functions return ``0`` on success, or set ``*result`` to ``0`` return -``-1`` on failure. +On success, the functions return ``0``. + +On failure, they set ``*result`` to ``0`` and return ``-1``, *without* setting +an exception. To get the cause of the error, acquire the GIL and call the +regular (non-``Raw``) function. Note that the regular function may succeed after +the ``Raw`` one failed. .. c:function:: int PyTime_MonotonicRaw(PyTime_t *result) diff --git a/Modules/_testcapi/time.c b/Modules/_testcapi/time.c index 3f07c1c48360714..464cf5c3125012e 100644 --- a/Modules/_testcapi/time.c +++ b/Modules/_testcapi/time.c @@ -51,6 +51,7 @@ test_pytime_monotonic(PyObject *Py_UNUSED(self), PyObject *Py_UNUSED(args)) PyTime_t t; int res = PyTime_Monotonic(&t); if (res < 0) { + assert(t == 0); return NULL; } assert(res == 0); @@ -67,6 +68,7 @@ test_pytime_monotonic_raw(PyObject *Py_UNUSED(self), PyObject *Py_UNUSED(args)) res = PyTime_MonotonicRaw(&t); Py_END_ALLOW_THREADS if (res < 0) { + assert(t == 0); PyErr_SetString(PyExc_RuntimeError, "PyTime_MonotonicRaw() failed"); return NULL; } @@ -81,6 +83,7 @@ test_pytime_perf_counter(PyObject *Py_UNUSED(self), PyObject *Py_UNUSED(args)) PyTime_t t; int res = PyTime_PerfCounter(&t); if (res < 0) { + assert(t == 0); return NULL; } assert(res == 0); @@ -97,6 +100,7 @@ test_pytime_perf_counter_raw(PyObject *Py_UNUSED(self), PyObject *Py_UNUSED(args res = PyTime_PerfCounterRaw(&t); Py_END_ALLOW_THREADS if (res < 0) { + assert(t == 0); PyErr_SetString(PyExc_RuntimeError, "PyTime_PerfCounterRaw() failed"); return NULL; } @@ -111,6 +115,7 @@ test_pytime_time(PyObject *Py_UNUSED(self), PyObject *Py_UNUSED(args)) PyTime_t t; int res = PyTime_Time(&t); if (res < 0) { + assert(t == 0); return NULL; } assert(res == 0); @@ -127,6 +132,7 @@ test_pytime_time_raw(PyObject *Py_UNUSED(self), PyObject *Py_UNUSED(args)) res = PyTime_TimeRaw(&t); Py_END_ALLOW_THREADS if (res < 0) { + assert(t == 0); PyErr_SetString(PyExc_RuntimeError, "PyTime_TimeRaw() failed"); return NULL; } diff --git a/Python/pytime.c b/Python/pytime.c index d8c46af99b979a5..d2bda090ad99d36 100644 --- a/Python/pytime.c +++ b/Python/pytime.c @@ -1034,7 +1034,10 @@ PyTime_t _PyTime_TimeUnchecked(void) { PyTime_t t; - (void)PyTime_TimeRaw(&t); + int result = PyTime_TimeRaw(&t); + // We assume that getting the time succeeds on current platforms. + // If this ever is not the case, use PyTime_TimeRaw instead. + assert(result == 0); return t; } @@ -1267,7 +1270,10 @@ PyTime_t _PyTime_MonotonicUnchecked(void) { PyTime_t t; - (void)PyTime_MonotonicRaw(&t); + int result = PyTime_MonotonicRaw(&t); + // We assume that getting the time succeeds on current platforms. + // If this ever is not the case, use PyTime_MonotonicRaw instead. + assert(result == 0); return t; }