summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@python.org>2024-05-01 18:05:01 (GMT)
committerGitHub <noreply@github.com>2024-05-01 18:05:01 (GMT)
commitb52c753e0fb8b87c7a0e3f1c72acd18327d8d16b (patch)
tree0ffb22ddef0675046cd44d9aec0361b5fce0f4cd /Modules
parenta8bcf3ec3284f042dab9cad676951092fb1f9890 (diff)
downloadcpython-b52c753e0fb8b87c7a0e3f1c72acd18327d8d16b.zip
cpython-b52c753e0fb8b87c7a0e3f1c72acd18327d8d16b.tar.gz
cpython-b52c753e0fb8b87c7a0e3f1c72acd18327d8d16b.tar.bz2
gh-110850: Add PyTime_TimeRaw() function (#118394)
Add "Raw" variant of PyTime functions: * PyTime_MonotonicRaw() * PyTime_PerfCounterRaw() * PyTime_TimeRaw() Changes: * Add documentation and tests. Tests release the GIL while calling raw clock functions. * py_get_system_clock() and py_get_monotonic_clock() now check that the GIL is hold by the caller if raise_exc is non-zero. * Reimplement "Unchecked" functions with raw clock functions. Co-authored-by: Petr Viktorin <encukou@gmail.com>
Diffstat (limited to 'Modules')
-rw-r--r--Modules/_testcapi/time.c60
1 files changed, 60 insertions, 0 deletions
diff --git a/Modules/_testcapi/time.c b/Modules/_testcapi/time.c
index 68f082b..464cf5c 100644
--- a/Modules/_testcapi/time.c
+++ b/Modules/_testcapi/time.c
@@ -51,6 +51,25 @@ 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);
+ return pytime_as_float(t);
+}
+
+
+static PyObject*
+test_pytime_monotonic_raw(PyObject *Py_UNUSED(self), PyObject *Py_UNUSED(args))
+{
+ PyTime_t t;
+ int res;
+ Py_BEGIN_ALLOW_THREADS
+ res = PyTime_MonotonicRaw(&t);
+ Py_END_ALLOW_THREADS
+ if (res < 0) {
+ assert(t == 0);
+ PyErr_SetString(PyExc_RuntimeError, "PyTime_MonotonicRaw() failed");
return NULL;
}
assert(res == 0);
@@ -64,6 +83,25 @@ 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);
+ return pytime_as_float(t);
+}
+
+
+static PyObject*
+test_pytime_perf_counter_raw(PyObject *Py_UNUSED(self), PyObject *Py_UNUSED(args))
+{
+ PyTime_t t;
+ int res;
+ Py_BEGIN_ALLOW_THREADS
+ res = PyTime_PerfCounterRaw(&t);
+ Py_END_ALLOW_THREADS
+ if (res < 0) {
+ assert(t == 0);
+ PyErr_SetString(PyExc_RuntimeError, "PyTime_PerfCounterRaw() failed");
return NULL;
}
assert(res == 0);
@@ -77,6 +115,25 @@ 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);
+ return pytime_as_float(t);
+}
+
+
+static PyObject*
+test_pytime_time_raw(PyObject *Py_UNUSED(self), PyObject *Py_UNUSED(args))
+{
+ PyTime_t t;
+ int res;
+ Py_BEGIN_ALLOW_THREADS
+ res = PyTime_TimeRaw(&t);
+ Py_END_ALLOW_THREADS
+ if (res < 0) {
+ assert(t == 0);
+ PyErr_SetString(PyExc_RuntimeError, "PyTime_TimeRaw() failed");
return NULL;
}
assert(res == 0);
@@ -87,8 +144,11 @@ test_pytime_time(PyObject *Py_UNUSED(self), PyObject *Py_UNUSED(args))
static PyMethodDef test_methods[] = {
{"PyTime_AsSecondsDouble", test_pytime_assecondsdouble, METH_VARARGS},
{"PyTime_Monotonic", test_pytime_monotonic, METH_NOARGS},
+ {"PyTime_MonotonicRaw", test_pytime_monotonic_raw, METH_NOARGS},
{"PyTime_PerfCounter", test_pytime_perf_counter, METH_NOARGS},
+ {"PyTime_PerfCounterRaw", test_pytime_perf_counter_raw, METH_NOARGS},
{"PyTime_Time", test_pytime_time, METH_NOARGS},
+ {"PyTime_TimeRaw", test_pytime_time_raw, METH_NOARGS},
{NULL},
};