cpython: 27441e0d6a75 (original) (raw)
--- a/Doc/library/time.rst +++ b/Doc/library/time.rst @@ -226,7 +226,7 @@ The module defines the following functio The earliest date for which it can generate a time is platform-dependent. -.. function:: steady() +.. function:: steady(strict=False) .. index:: single: benchmarking @@ -236,6 +236,11 @@ The module defines the following functio adjusted. The reference point of the returned value is undefined so only the difference of consecutive calls is valid.
- If available, a monotonic clock is used. By default, if strict is False,
- the function falls back to another clock if the monotonic clock failed or is
- not available. If strict is True, raise an :exc:
OSError
on error or - :exc:
NotImplementedError
if no monotonic clock is available. + .. versionadded:: 3.3
--- a/Lib/test/test_time.py +++ b/Lib/test/test_time.py @@ -340,6 +340,16 @@ class TimeTestCase(unittest.TestCase): self.assertGreater(t2, t1) self.assertAlmostEqual(dt, 0.1, delta=0.2)
- def test_steady_strict(self):
try:[](#l2.8)
t1 = time.steady(strict=True)[](#l2.9)
except OSError as err:[](#l2.10)
self.skipTest("the monotonic clock failed: %s" % err)[](#l2.11)
except NotImplementedError:[](#l2.12)
self.skipTest("no monotonic clock available")[](#l2.13)
t2 = time.steady(strict=True)[](#l2.14)
self.assertGreaterEqual(t2, t1)[](#l2.15)
+ def test_localtime_failure(self): # Issue #13847: check for localtime() failure invalid_time_t = None
--- a/Modules/timemodule.c +++ b/Modules/timemodule.c @@ -45,18 +45,12 @@ /* Forward declarations / static int floatsleep(double); -static double floattime(void); +static PyObject floattime(void); static PyObject * time_time(PyObject *self, PyObject *unused) {
- double secs;
- secs = floattime();
- if (secs == 0.0) {
PyErr_SetFromErrno(PyExc_IOError);[](#l3.16)
return NULL;[](#l3.17)
- }
- return PyFloat_FromDouble(secs);
} PyDoc_STRVAR(time_doc, @@ -768,11 +762,11 @@ the local timezone used by methods such should not be relied on."); #endif /* HAVE_WORKING_TZSET */ -static PyObject * -time_steady(PyObject *self, PyObject unused) +static PyObject +steady_clock(int strict) { #if defined(MS_WINDOWS) && !defined(BORLANDC)
#elif defined(APPLE) uint64_t time = mach_absolute_time(); double secs; @@ -806,14 +800,37 @@ time_steady(PyObject *self, PyObject *un if (Py_ARRAY_LENGTH(clk_ids) <= clk_index) clk_index = -1; }
- if (strict) {
PyErr_SetFromErrno(PyExc_OSError);[](#l3.45)
return NULL;[](#l3.46)
- }
- return floattime();
- if (strict) {
PyErr_SetString(PyExc_NotImplementedError,[](#l3.52)
"no steady clock available on your platform");[](#l3.53)
return NULL;[](#l3.54)
- }
- return floattime();
#endif } +static PyObject * +time_steady(PyObject *self, PyObject *args, PyObject *kwargs) +{
- if (!PyArg_ParseTupleAndKeywords(
args, kwargs, "|i:steady", kwlist,[](#l3.67)
&strict))[](#l3.68)
return NULL;[](#l3.69)
+} + PyDoc_STRVAR(steady_doc, -"steady() -> float\n[](#l3.75) +"steady(strict=False) -> float\n[](#l3.76) \n[](#l3.77) Return the current time as a floating point number expressed in seconds.\n[](#l3.78) This clock advances at a steady rate relative to real time and it may not\n[](#l3.79) @@ -949,7 +966,8 @@ static PyMethodDef time_methods[] = { #ifdef HAVE_MKTIME {"mktime", time_mktime, METH_O, mktime_doc}, #endif
#ifdef HAVE_STRFTIME {"strftime", time_strftime, METH_VARARGS, strftime_doc}, #endif @@ -1041,12 +1059,18 @@ PyInit_time(void) return m; } -static double +static PyObject* floattime(void) { _PyTime_timeval t;