summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@gmail.com>2012-03-26 20:53:14 (GMT)
committerVictor Stinner <victor.stinner@gmail.com>2012-03-26 20:53:14 (GMT)
commit8486076dd53ff5628cea35935295eb3b0ad0b0da (patch)
treefadea562ee13f12a84e8871435fa4c2dfeb10486
parent3c1e48176e077bbbf85183874f432de6d4ac8f88 (diff)
downloadcpython-8486076dd53ff5628cea35935295eb3b0ad0b0da.zip
cpython-8486076dd53ff5628cea35935295eb3b0ad0b0da.tar.gz
cpython-8486076dd53ff5628cea35935295eb3b0ad0b0da.tar.bz2
Fix time.steady(strict=True): don't use CLOCK_REALTIME
-rw-r--r--Modules/timemodule.c35
1 files changed, 28 insertions, 7 deletions
diff --git a/Modules/timemodule.c b/Modules/timemodule.c
index 1d84db1..b32c9df 100644
--- a/Modules/timemodule.c
+++ b/Modules/timemodule.c
@@ -779,26 +779,47 @@ steady_clock(int strict)
return PyFloat_FromDouble(secs);
#elif defined(HAVE_CLOCK_GETTIME)
- static int clk_index = 0;
- clockid_t clk_ids[] = {
+ static int steady_clk_index = 0;
+ static int monotonic_clk_index = 0;
+ int *clk_index;
+ clockid_t steady_clk_ids[] = {
#ifdef CLOCK_MONOTONIC_RAW
CLOCK_MONOTONIC_RAW,
#endif
CLOCK_MONOTONIC,
CLOCK_REALTIME
};
+ clockid_t monotonic_clk_ids[] = {
+#ifdef CLOCK_MONOTONIC_RAW
+ CLOCK_MONOTONIC_RAW,
+#endif
+ CLOCK_MONOTONIC
+ };
+ clockid_t *clk_ids;
+ int clk_ids_len;
int ret;
struct timespec tp;
- while (0 <= clk_index) {
- clockid_t clk_id = clk_ids[clk_index];
+ if (strict) {
+ clk_index = &monotonic_clk_index;
+ clk_ids = monotonic_clk_ids;
+ clk_ids_len = Py_ARRAY_LENGTH(monotonic_clk_ids);
+ }
+ else {
+ clk_index = &steady_clk_index;
+ clk_ids = steady_clk_ids;
+ clk_ids_len = Py_ARRAY_LENGTH(steady_clk_ids);
+ }
+
+ while (0 <= *clk_index) {
+ clockid_t clk_id = clk_ids[*clk_index];
ret = clock_gettime(clk_id, &tp);
if (ret == 0)
return PyFloat_FromDouble(tp.tv_sec + tp.tv_nsec * 1e-9);
- clk_index++;
- if (Py_ARRAY_LENGTH(clk_ids) <= clk_index)
- clk_index = -1;
+ (*clk_index)++;
+ if (clk_ids_len <= *clk_index)
+ (*clk_index) = -1;
}
if (strict) {
PyErr_SetFromErrno(PyExc_OSError);