summaryrefslogtreecommitdiffstats
path: root/Modules/timemodule.c
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@python.org>2021-09-15 12:26:43 (GMT)
committerGitHub <noreply@github.com>2021-09-15 12:26:43 (GMT)
commitb49263b698993cad2b8aaddc55cdeaa678412b30 (patch)
tree8425d73518fbddd840e28d8e1d072b05778104d2 /Modules/timemodule.c
parent40d2ac92f9a28a486156dafdbb613016bb1f6b98 (diff)
downloadcpython-b49263b698993cad2b8aaddc55cdeaa678412b30.zip
cpython-b49263b698993cad2b8aaddc55cdeaa678412b30.tar.gz
cpython-b49263b698993cad2b8aaddc55cdeaa678412b30.tar.bz2
bpo-21302: Add _PyTime_AsNanoseconds() (GH-28350)
Refactor pytime.c: * Add pytime_from_nanoseconds() and pytime_as_nanoseconds(), and use explicitly these functions * Add two empty lines between functions * PEP 7: add braces { ... } * C99: declare variables where they are set * Rename private functions to lowercase * Rename error_time_t_overflow() to pytime_time_t_overflow() * Rename win_perf_counter_frequency() to py_win_perf_counter_frequency() * py_get_monotonic_clock(): add an assertion to detect overflow when mach_absolute_time() unsigned uint64_t is casted to _PyTime_t (signed int64_t). _testcapi: use _PyTime_FromNanoseconds().
Diffstat (limited to 'Modules/timemodule.c')
-rw-r--r--Modules/timemodule.c16
1 files changed, 8 insertions, 8 deletions
diff --git a/Modules/timemodule.c b/Modules/timemodule.c
index cf58a18..52c6115 100644
--- a/Modules/timemodule.c
+++ b/Modules/timemodule.c
@@ -128,12 +128,11 @@ static int
_PyTime_GetClockWithInfo(_PyTime_t *tp, _Py_clock_info_t *info)
{
static int initialized = 0;
- clock_t ticks;
if (!initialized) {
initialized = 1;
- /* must sure that _PyTime_MulDiv(ticks, SEC_TO_NS, CLOCKS_PER_SEC)
+ /* Make sure that _PyTime_MulDiv(ticks, SEC_TO_NS, CLOCKS_PER_SEC)
above cannot overflow */
if ((_PyTime_t)CLOCKS_PER_SEC > _PyTime_MAX / SEC_TO_NS) {
PyErr_SetString(PyExc_OverflowError,
@@ -149,14 +148,15 @@ _PyTime_GetClockWithInfo(_PyTime_t *tp, _Py_clock_info_t *info)
info->adjustable = 0;
}
- ticks = clock();
+ clock_t ticks = clock();
if (ticks == (clock_t)-1) {
PyErr_SetString(PyExc_RuntimeError,
"the processor time used is not available "
"or its value cannot be represented");
return -1;
}
- *tp = _PyTime_MulDiv(ticks, SEC_TO_NS, (_PyTime_t)CLOCKS_PER_SEC);
+ _PyTime_t ns = _PyTime_MulDiv(ticks, SEC_TO_NS, (_PyTime_t)CLOCKS_PER_SEC);
+ *tp = _PyTime_FromNanoseconds(ns);
return 0;
}
#endif /* HAVE_CLOCK */
@@ -1325,10 +1325,10 @@ _PyTime_GetProcessTimeWithInfo(_PyTime_t *tp, _Py_clock_info_t *info)
info->resolution = 1.0 / (double)ticks_per_second;
}
- _PyTime_t total;
- total = _PyTime_MulDiv(t.tms_utime, SEC_TO_NS, ticks_per_second);
- total += _PyTime_MulDiv(t.tms_stime, SEC_TO_NS, ticks_per_second);
- *tp = total;
+ _PyTime_t ns;
+ ns = _PyTime_MulDiv(t.tms_utime, SEC_TO_NS, ticks_per_second);
+ ns += _PyTime_MulDiv(t.tms_stime, SEC_TO_NS, ticks_per_second);
+ *tp = _PyTime_FromNanoseconds(ns);
return 0;
}
}