diff options
author | Victor Stinner <victor.stinner@haypocalc.com> | 2012-01-26 23:38:48 (GMT) |
---|---|---|
committer | Victor Stinner <victor.stinner@haypocalc.com> | 2012-01-26 23:38:48 (GMT) |
commit | 85fdfa85e191c03c1844dfdc20894d33e101eab1 (patch) | |
tree | 3a610afb11217c3546e607c9dccb374dfd11c3a8 /Modules | |
parent | e9cd900585d44b418b2a2235a7eec6e4b362798f (diff) | |
download | cpython-85fdfa85e191c03c1844dfdc20894d33e101eab1.zip cpython-85fdfa85e191c03c1844dfdc20894d33e101eab1.tar.gz cpython-85fdfa85e191c03c1844dfdc20894d33e101eab1.tar.bz2 |
Issue #13847: time.clock() now raises a RuntimeError if the processor time used
is not available or its value cannot be represented
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/timemodule.c | 38 |
1 files changed, 27 insertions, 11 deletions
diff --git a/Modules/timemodule.c b/Modules/timemodule.c index d2cc62f..30a01f5 100644 --- a/Modules/timemodule.c +++ b/Modules/timemodule.c @@ -62,6 +62,31 @@ PyDoc_STRVAR(time_doc, Return the current time in seconds since the Epoch.\n\ Fractions of a second may be present if the system clock provides them."); +#if defined(HAVE_CLOCK) + +#ifndef CLOCKS_PER_SEC +#ifdef CLK_TCK +#define CLOCKS_PER_SEC CLK_TCK +#else +#define CLOCKS_PER_SEC 1000000 +#endif +#endif + +static PyObject * +pyclock(void) +{ + clock_t value; + value = clock(); + if (value == (clock_t)-1) { + PyErr_SetString(PyExc_RuntimeError, + "the processor time used is not available " + "or its value cannot be represented"); + return NULL; + } + return PyFloat_FromDouble((double)value / CLOCKS_PER_SEC); +} +#endif /* HAVE_CLOCK */ + #if defined(MS_WINDOWS) && !defined(__BORLANDC__) /* Win32 has better clock replacement; we have our own version, due to Mark Hammond and Tim Peters */ @@ -79,8 +104,7 @@ time_clock(PyObject *self, PyObject *unused) if (!QueryPerformanceFrequency(&freq) || freq.QuadPart == 0) { /* Unlikely to happen - this works on all intel machines at least! Revert to clock() */ - return PyFloat_FromDouble(((double)clock()) / - CLOCKS_PER_SEC); + return pyclock(); } divisor = (double)freq.QuadPart; } @@ -91,18 +115,10 @@ time_clock(PyObject *self, PyObject *unused) #elif defined(HAVE_CLOCK) -#ifndef CLOCKS_PER_SEC -#ifdef CLK_TCK -#define CLOCKS_PER_SEC CLK_TCK -#else -#define CLOCKS_PER_SEC 1000000 -#endif -#endif - static PyObject * time_clock(PyObject *self, PyObject *unused) { - return PyFloat_FromDouble(((double)clock()) / CLOCKS_PER_SEC); + return pyclock(); } #endif /* HAVE_CLOCK */ |