Skip to content

Commit 504adcf

Browse files
committed
No math :)
1 parent beb453f commit 504adcf

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

mypy/util.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import sys
77
import math
88
import time
9+
from itertools import count
910
from xml.sax.saxutils import escape
1011
from typing import (
1112
TypeVar, List, Tuple, Optional, Sequence, Dict, Callable, Iterable, Type, Union, AnyStr, cast
@@ -158,18 +159,27 @@ def _replace(src: PathType[AnyStr], dest: PathType[AnyStr], timeout: float = 1)
158159
Increase wait time exponentially until total wait of timeout sec;
159160
on timeout, give up and reraise the last exception seen.
160161
"""
161-
n_iter = max(1, math.ceil(math.log2(timeout / 0.001)))
162-
for i in range(n_iter):
163-
# Last wait is ~ timeout/2, so that total wait ~ timeout.
164-
wait = timeout / 2 ** (n_iter - i - 1)
162+
# Find starting value for wait.
163+
wait = timeout
164+
total_wait = 0
165+
while True:
166+
if wait < 0.001:
167+
break
168+
wait /= 2
169+
while True:
165170
try:
166171
os.replace(src, dest)
167172
except PermissionError:
168-
if i == n_iter - 1:
173+
# Can't compare for equality because float arithmetic;
174+
# if we hit 0.9999 * timeout, it's time to stop.
175+
# For timeout more than a few ms, we'll be very close to the desired timeout.
176+
if total_wait > 0.99 * timeout:
169177
raise
170178
else:
171179
return
172180
time.sleep(wait)
181+
total_wait += wait
182+
wait *= 2
173183

174184

175185
if sys.platform.startswith("win"):

0 commit comments

Comments
 (0)