]> git.ipfire.org Git - thirdparty/Python/cpython.git/commit
gh-112567: Add _PyTimeFraction C API (#112568)
authorVictor Stinner <vstinner@python.org>
Fri, 1 Dec 2023 18:50:10 +0000 (19:50 +0100)
committerGitHub <noreply@github.com>
Fri, 1 Dec 2023 18:50:10 +0000 (19:50 +0100)
commit5c5022b8625e34f0035ad5a23bc4c2f16649d134
tree1fcc940eb9aa9361579dc5216c43a2a46d527469
parent05a370abd6cdfe4b54be60b3b911f3a441026bb2
gh-112567: Add _PyTimeFraction C API (#112568)

Use a fraction internally in the _PyTime API to reduce the risk of
integer overflow: simplify the fraction using Greatest Common
Divisor (GCD). The fraction API is used by time functions:
perf_counter(), monotonic() and process_time().

For example, QueryPerformanceFrequency() usually returns 10 MHz on
Windows 10 and newer. The fraction SEC_TO_NS / frequency =
1_000_000_000 / 10_000_000 can be simplified to 100 / 1.

* Add _PyTimeFraction type.
* Add functions:

  * _PyTimeFraction_Set()
  * _PyTimeFraction_Mul()
  * _PyTimeFraction_Resolution()

* No longer check "numer * denom <= _PyTime_MAX" in
  _PyTimeFraction_Set(). _PyTimeFraction_Mul() uses _PyTime_Mul()
  which handles integer overflow.
Include/internal/pycore_time.h
Modules/timemodule.c
Python/pytime.c