Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
071eca3f
Commit
071eca3f
authored
Mar 15, 2012
by
Victor Stinner
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue #10278: Add an optional strict argument to time.steady(), False by default
parent
ec919cc7
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
57 additions
and
18 deletions
+57
-18
Doc/library/time.rst
Doc/library/time.rst
+6
-1
Lib/test/test_time.py
Lib/test/test_time.py
+10
-0
Modules/timemodule.c
Modules/timemodule.c
+41
-17
No files found.
Doc/library/time.rst
View file @
071eca3f
...
...
@@ -226,7 +226,7 @@ The module defines the following functions and data items:
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 functions and data items:
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
...
...
Lib/test/test_time.py
View file @
071eca3f
...
...
@@ -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
:
t1
=
time
.
steady
(
strict
=
True
)
except
OSError
as
err
:
self
.
skipTest
(
"the monotonic clock failed: %s"
%
err
)
except
NotImplementedError
:
self
.
skipTest
(
"no monotonic clock available"
)
t2
=
time
.
steady
(
strict
=
True
)
self
.
assertGreaterEqual
(
t2
,
t1
)
def
test_localtime_failure
(
self
):
# Issue #13847: check for localtime() failure
invalid_time_t
=
None
...
...
Modules/timemodule.c
View file @
071eca3f
...
...
@@ -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
);
return
NULL
;
}
return
PyFloat_FromDouble
(
secs
);
return
floattime
();
}
PyDoc_STRVAR
(
time_doc
,
...
...
@@ -768,11 +762,11 @@ the local timezone used by methods such as localtime, but this behaviour\n\
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__)
return
win32_clock
(
1
);
return
win32_clock
(
!
strict
);
#elif defined(__APPLE__)
uint64_t
time
=
mach_absolute_time
();
double
secs
;
...
...
@@ -806,14 +800,37 @@ time_steady(PyObject *self, PyObject *unused)
if
(
Py_ARRAY_LENGTH
(
clk_ids
)
<=
clk_index
)
clk_index
=
-
1
;
}
return
time_time
(
self
,
NULL
);
if
(
strict
)
{
PyErr_SetFromErrno
(
PyExc_OSError
);
return
NULL
;
}
return
floattime
();
#else
return
time_time
(
self
,
NULL
);
if
(
strict
)
{
PyErr_SetString
(
PyExc_NotImplementedError
,
"no steady clock available on your platform"
);
return
NULL
;
}
return
floattime
();
#endif
}
static
PyObject
*
time_steady
(
PyObject
*
self
,
PyObject
*
args
,
PyObject
*
kwargs
)
{
static
char
*
kwlist
[]
=
{
"strict"
,
NULL
};
int
strict
=
0
;
if
(
!
PyArg_ParseTupleAndKeywords
(
args
,
kwargs
,
"|i:steady"
,
kwlist
,
&
strict
))
return
NULL
;
return
steady_clock
(
strict
);
}
PyDoc_STRVAR
(
steady_doc
,
"steady() -> float
\n
\
"steady(
strict=False
) -> float
\n
\
\n
\
Return the current time as a floating point number expressed in seconds.
\n
\
This clock advances at a steady rate relative to real time and it may not
\n
\
...
...
@@ -949,7 +966,8 @@ static PyMethodDef time_methods[] = {
#ifdef HAVE_MKTIME
{
"mktime"
,
time_mktime
,
METH_O
,
mktime_doc
},
#endif
{
"steady"
,
time_steady
,
METH_NOARGS
,
steady_doc
},
{
"steady"
,
(
PyCFunction
)
time_steady
,
METH_VARARGS
|
METH_KEYWORDS
,
steady_doc
},
#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
;
double
secs
;
_PyTime_gettimeofday
(
&
t
);
return
(
double
)
t
.
tv_sec
+
t
.
tv_usec
*
0
.
000001
;
secs
=
(
double
)
t
.
tv_sec
+
t
.
tv_usec
*
0
.
000001
;
if
(
secs
==
0
.
0
)
{
PyErr_SetFromErrno
(
PyExc_OSError
);
return
NULL
;
}
return
PyFloat_FromDouble
(
secs
);
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment