[Python-checkins] cpython: Expose clock_settime() as time.clock_settime()

victor.stinner python-checkins at python.org
Tue Apr 3 00:45:16 CEST 2012


http://hg.python.org/cpython/rev/8ba72c0987dc
changeset:   76080:8ba72c0987dc
user:        Victor Stinner <victor.stinner at gmail.com>
date:        Tue Apr 03 00:45:07 2012 +0200
summary:
  Expose clock_settime() as time.clock_settime()

files:
  Doc/library/time.rst  |   7 +++++++
  Lib/test/test_time.py |  11 +++++++++++
  Modules/timemodule.c  |  30 ++++++++++++++++++++++++++++++
  3 files changed, 48 insertions(+), 0 deletions(-)


diff --git a/Doc/library/time.rst b/Doc/library/time.rst
--- a/Doc/library/time.rst
+++ b/Doc/library/time.rst
@@ -151,6 +151,13 @@
    .. versionadded:: 3.3
 
 
+.. function:: clock_settime(clk_id, time)
+
+   Set the time of the specified clock *clk_id*.
+
+   .. versionadded:: 3.3
+
+
 .. data:: CLOCK_REALTIME
 
    System-wide real-time clock. Setting this clock requires appropriate
diff --git a/Lib/test/test_time.py b/Lib/test/test_time.py
--- a/Lib/test/test_time.py
+++ b/Lib/test/test_time.py
@@ -47,6 +47,17 @@
         self.assertGreater(res, 0.0)
         self.assertLessEqual(res, 1.0)
 
+    @unittest.skipUnless(hasattr(time, 'clock_settime'),
+                         'need time.clock_settime()')
+    def test_clock_settime(self):
+        t = time.clock_gettime(time.CLOCK_REALTIME)
+        try:
+            time.clock_settime(time.CLOCK_REALTIME, t)
+        except PermissionError:
+            pass
+
+        self.assertRaises(OSError, time.clock_settime, time.CLOCK_MONOTONIC, 0)
+
     def test_conversions(self):
         self.assertEqual(time.ctime(self.t),
                          time.asctime(time.localtime(self.t)))
diff --git a/Modules/timemodule.c b/Modules/timemodule.c
--- a/Modules/timemodule.c
+++ b/Modules/timemodule.c
@@ -158,6 +158,33 @@
 "clock_gettime(clk_id) -> floating point number\n\
 \n\
 Return the time of the specified clock clk_id.");
+
+static PyObject *
+time_clock_settime(PyObject *self, PyObject *args)
+{
+    clockid_t clk_id;
+    PyObject *obj;
+    struct timespec tp;
+    int ret;
+
+    if (!PyArg_ParseTuple(args, "iO:clock_settime", &clk_id, &obj))
+        return NULL;
+
+    if (_PyTime_ObjectToTimespec(obj, &tp.tv_sec, &tp.tv_nsec) == -1)
+        return NULL;
+
+    ret = clock_settime((clockid_t)clk_id, &tp);
+    if (ret != 0) {
+        PyErr_SetFromErrno(PyExc_IOError);
+        return NULL;
+    }
+    Py_RETURN_NONE;
+}
+
+PyDoc_STRVAR(clock_settime_doc,
+"clock_settime(clk_id, time)\n\
+\n\
+Set the time of the specified clock clk_id.");
 #endif
 
 #ifdef HAVE_CLOCK_GETRES
@@ -983,6 +1010,9 @@
 #ifdef HAVE_CLOCK_GETTIME
     {"clock_gettime",   time_clock_gettime, METH_VARARGS, clock_gettime_doc},
 #endif
+#ifdef HAVE_CLOCK_GETTIME
+    {"clock_settime",   time_clock_settime, METH_VARARGS, clock_settime_doc},
+#endif
 #ifdef HAVE_CLOCK_GETRES
     {"clock_getres",    time_clock_getres, METH_VARARGS, clock_getres_doc},
 #endif

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


More information about the Python-checkins mailing list