Skip to content

Commit a70e04f

Browse files
committed
No math :)
1 parent beb453f commit a70e04f

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

mypy/util.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
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 (
11-
TypeVar, List, Tuple, Optional, Sequence, Dict, Callable, Iterable, Type, Union, AnyStr, cast
12+
TypeVar, List, Tuple, Optional, Sequence, Dict, Callable, Iterable, Type, Union,
13+
AnyStr, Generic
1214
)
1315

1416

@@ -158,18 +160,27 @@ def _replace(src: PathType[AnyStr], dest: PathType[AnyStr], timeout: float = 1)
158160
Increase wait time exponentially until total wait of timeout sec;
159161
on timeout, give up and reraise the last exception seen.
160162
"""
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)
163+
# Find starting value for wait.
164+
wait = timeout
165+
total_wait: float = 0
166+
while True:
167+
if wait < 0.001:
168+
break
169+
wait /= 2
170+
while True:
165171
try:
166172
os.replace(src, dest)
167173
except PermissionError:
168-
if i == n_iter - 1:
174+
# Can't compare for equality because float arithmetic;
175+
# if we hit 0.9999 * timeout, it's time to stop.
176+
# For timeout more than a few ms, we'll be very close to the desired timeout.
177+
if total_wait > 0.99 * timeout:
169178
raise
170179
else:
171180
return
172181
time.sleep(wait)
182+
total_wait += wait
183+
wait *= 2
173184

174185

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

0 commit comments

Comments
 (0)