Skip to content

Commit f35ddf2

Browse files
authored
bpo-41299: QueryPerformanceFrequency() cannot fail (GH-28552)
py_win_perf_counter_frequency() no longer checks for QueryPerformanceFrequency() failure. According to the QueryPerformanceFrequency() documentation, the function can no longer fails since Windows XP.
1 parent 7c801e0 commit f35ddf2

File tree

1 file changed

+7
-21
lines changed

1 file changed

+7
-21
lines changed

Python/pytime.c

+7-21
Original file line numberDiff line numberDiff line change
@@ -1050,26 +1050,14 @@ py_win_perf_counter_frequency(LONGLONG *pfrequency, int raise)
10501050
LONGLONG frequency;
10511051

10521052
LARGE_INTEGER freq;
1053-
if (!QueryPerformanceFrequency(&freq)) {
1054-
if (raise) {
1055-
PyErr_SetFromWindowsErr(0);
1056-
}
1057-
return -1;
1058-
}
1053+
// Since Windows XP, the function cannot fail.
1054+
(void)QueryPerformanceFrequency(&freq);
10591055
frequency = freq.QuadPart;
10601056

1061-
/* Sanity check: should never occur in practice */
1062-
if (frequency < 1) {
1063-
if (raise) {
1064-
PyErr_SetString(PyExc_RuntimeError,
1065-
"invalid QueryPerformanceFrequency");
1066-
}
1067-
return -1;
1068-
}
1069-
1070-
/* Check that frequency can be casted to _PyTime_t.
1057+
// Since Windows XP, frequency cannot be zero.
1058+
assert(frequency >= 1);
10711059

1072-
Make also sure that (ticks * SEC_TO_NS) cannot overflow in
1060+
/* Make also sure that (ticks * SEC_TO_NS) cannot overflow in
10731061
_PyTime_MulDiv(), with ticks < frequency.
10741062
10751063
Known QueryPerformanceFrequency() values:
@@ -1078,10 +1066,8 @@ py_win_perf_counter_frequency(LONGLONG *pfrequency, int raise)
10781066
* 3,579,545 Hz (3.6 MHz): 279 ns resolution
10791067
10801068
None of these frequencies can overflow with 64-bit _PyTime_t, but
1081-
check for overflow, just in case. */
1082-
if (frequency > _PyTime_MAX
1083-
|| frequency > (LONGLONG)_PyTime_MAX / (LONGLONG)SEC_TO_NS)
1084-
{
1069+
check for integer overflow just in case. */
1070+
if (frequency > _PyTime_MAX / SEC_TO_NS) {
10851071
if (raise) {
10861072
PyErr_SetString(PyExc_OverflowError,
10871073
"QueryPerformanceFrequency is too large");

0 commit comments

Comments
 (0)