[Python-checkins] cpython: Fix time.steady(strict=True): don't use CLOCK_REALTIME

victor.stinner python-checkins at python.org
Mon Mar 26 22:53:37 CEST 2012


http://hg.python.org/cpython/rev/566527ace50b
changeset:   75960:566527ace50b
user:        Victor Stinner <victor.stinner at gmail.com>
date:        Mon Mar 26 22:53:14 2012 +0200
summary:
  Fix time.steady(strict=True): don't use CLOCK_REALTIME

files:
  Modules/timemodule.c |  35 +++++++++++++++++++++++++------
  1 files changed, 28 insertions(+), 7 deletions(-)


diff --git a/Modules/timemodule.c b/Modules/timemodule.c
--- a/Modules/timemodule.c
+++ b/Modules/timemodule.c
@@ -779,26 +779,47 @@
 
     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);

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list