Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 071eca3

Browse files
committed
Issue #10278: Add an optional strict argument to time.steady(), False by default
1 parent ec919cc commit 071eca3

3 files changed

Lines changed: 57 additions & 18 deletions

File tree

Doc/library/time.rst

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ The module defines the following functions and data items:
226226
The earliest date for which it can generate a time is platform-dependent.
227227

228228

229-
.. function:: steady()
229+
.. function:: steady(strict=False)
230230

231231
.. index::
232232
single: benchmarking
@@ -236,6 +236,11 @@ The module defines the following functions and data items:
236236
adjusted. The reference point of the returned value is undefined so only the
237237
difference of consecutive calls is valid.
238238

239+
If available, a monotonic clock is used. By default, if *strict* is False,
240+
the function falls back to another clock if the monotonic clock failed or is
241+
not available. If *strict* is True, raise an :exc:`OSError` on error or
242+
:exc:`NotImplementedError` if no monotonic clock is available.
243+
239244
.. versionadded:: 3.3
240245

241246

Lib/test/test_time.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,16 @@ def test_steady(self):
340340
self.assertGreater(t2, t1)
341341
self.assertAlmostEqual(dt, 0.1, delta=0.2)
342342

343+
def test_steady_strict(self):
344+
try:
345+
t1 = time.steady(strict=True)
346+
except OSError as err:
347+
self.skipTest("the monotonic clock failed: %s" % err)
348+
except NotImplementedError:
349+
self.skipTest("no monotonic clock available")
350+
t2 = time.steady(strict=True)
351+
self.assertGreaterEqual(t2, t1)
352+
343353
def test_localtime_failure(self):
344354
# Issue #13847: check for localtime() failure
345355
invalid_time_t = None

Modules/timemodule.c

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,12 @@
4545

4646
/* Forward declarations */
4747
static int floatsleep(double);
48-
static double floattime(void);
48+
static PyObject* floattime(void);
4949

5050
static PyObject *
5151
time_time(PyObject *self, PyObject *unused)
5252
{
53-
double secs;
54-
secs = floattime();
55-
if (secs == 0.0) {
56-
PyErr_SetFromErrno(PyExc_IOError);
57-
return NULL;
58-
}
59-
return PyFloat_FromDouble(secs);
53+
return floattime();
6054
}
6155

6256
PyDoc_STRVAR(time_doc,
@@ -768,11 +762,11 @@ the local timezone used by methods such as localtime, but this behaviour\n\
768762
should not be relied on.");
769763
#endif /* HAVE_WORKING_TZSET */
770764

771-
static PyObject *
772-
time_steady(PyObject *self, PyObject *unused)
765+
static PyObject*
766+
steady_clock(int strict)
773767
{
774768
#if defined(MS_WINDOWS) && !defined(__BORLANDC__)
775-
return win32_clock(1);
769+
return win32_clock(!strict);
776770
#elif defined(__APPLE__)
777771
uint64_t time = mach_absolute_time();
778772
double secs;
@@ -806,14 +800,37 @@ time_steady(PyObject *self, PyObject *unused)
806800
if (Py_ARRAY_LENGTH(clk_ids) <= clk_index)
807801
clk_index = -1;
808802
}
809-
return time_time(self, NULL);
803+
if (strict) {
804+
PyErr_SetFromErrno(PyExc_OSError);
805+
return NULL;
806+
}
807+
return floattime();
810808
#else
811-
return time_time(self, NULL);
809+
if (strict) {
810+
PyErr_SetString(PyExc_NotImplementedError,
811+
"no steady clock available on your platform");
812+
return NULL;
813+
}
814+
return floattime();
812815
#endif
813816
}
814817

818+
static PyObject *
819+
time_steady(PyObject *self, PyObject *args, PyObject *kwargs)
820+
{
821+
static char *kwlist[] = {"strict", NULL};
822+
int strict = 0;
823+
824+
if (!PyArg_ParseTupleAndKeywords(
825+
args, kwargs, "|i:steady", kwlist,
826+
&strict))
827+
return NULL;
828+
829+
return steady_clock(strict);
830+
}
831+
815832
PyDoc_STRVAR(steady_doc,
816-
"steady() -> float\n\
833+
"steady(strict=False) -> float\n\
817834
\n\
818835
Return the current time as a floating point number expressed in seconds.\n\
819836
This clock advances at a steady rate relative to real time and it may not\n\
@@ -949,7 +966,8 @@ static PyMethodDef time_methods[] = {
949966
#ifdef HAVE_MKTIME
950967
{"mktime", time_mktime, METH_O, mktime_doc},
951968
#endif
952-
{"steady", time_steady, METH_NOARGS, steady_doc},
969+
{"steady", (PyCFunction)time_steady, METH_VARARGS|METH_KEYWORDS,
970+
steady_doc},
953971
#ifdef HAVE_STRFTIME
954972
{"strftime", time_strftime, METH_VARARGS, strftime_doc},
955973
#endif
@@ -1041,12 +1059,18 @@ PyInit_time(void)
10411059
return m;
10421060
}
10431061

1044-
static double
1062+
static PyObject*
10451063
floattime(void)
10461064
{
10471065
_PyTime_timeval t;
1066+
double secs;
10481067
_PyTime_gettimeofday(&t);
1049-
return (double)t.tv_sec + t.tv_usec*0.000001;
1068+
secs = (double)t.tv_sec + t.tv_usec*0.000001;
1069+
if (secs == 0.0) {
1070+
PyErr_SetFromErrno(PyExc_OSError);
1071+
return NULL;
1072+
}
1073+
return PyFloat_FromDouble(secs);
10501074
}
10511075

10521076

0 commit comments

Comments
 (0)