Skip to content

Commit 5a45762

Browse files
committed
Ignore errors when deleting files and folders on Windows
1 parent 8871844 commit 5a45762

10 files changed

+45
-31
lines changed

misc/incremental_checker.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,13 @@
3939
import json
4040
import os
4141
import random
42-
import shutil
4342
import subprocess
4443
import sys
4544
import textwrap
4645
import time
4746

47+
from mypy.util import delete_folder
48+
4849

4950
CACHE_PATH = ".incremental_checker_cache.json"
5051
MYPY_REPO_URL = "https://github.com/python/mypy.git"
@@ -59,11 +60,6 @@ def print_offset(text: str, indent_length: int = 4) -> None:
5960
print()
6061

6162

62-
def delete_folder(folder_path: str) -> None:
63-
if os.path.exists(folder_path):
64-
shutil.rmtree(folder_path)
65-
66-
6763
def execute(command: List[str], fail_on_error: bool = True) -> Tuple[str, str, int]:
6864
proc = subprocess.Popen(
6965
' '.join(command),

misc/perf_checker.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import textwrap
1010
import time
1111

12+
from mypy.util import delete_folder
13+
1214

1315
class Command:
1416
def __init__(self, setup: Callable[[], None], command: Callable[[], None]) -> None:
@@ -22,11 +24,6 @@ def print_offset(text: str, indent_length: int = 4) -> None:
2224
print()
2325

2426

25-
def delete_folder(folder_path: str) -> None:
26-
if os.path.exists(folder_path):
27-
shutil.rmtree(folder_path)
28-
29-
3027
def execute(command: List[str]) -> None:
3128
proc = subprocess.Popen(
3229
' '.join(command),

misc/touch_checker.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,15 @@
1111
import textwrap
1212
import time
1313

14+
from mypy.util import delete_folder
15+
1416

1517
def print_offset(text: str, indent_length: int = 4) -> None:
1618
print()
1719
print(textwrap.indent(text, ' ' * indent_length))
1820
print()
1921

2022

21-
def delete_folder(folder_path: str) -> None:
22-
if os.path.exists(folder_path):
23-
shutil.rmtree(folder_path)
24-
25-
2623
def execute(command: List[str]) -> None:
2724
proc = subprocess.Popen(
2825
' '.join(command),
@@ -121,7 +118,7 @@ def main() -> None:
121118
deltas = []
122119
for filename in glob.iglob("mypy/**/*.py", recursive=True):
123120
print("{} {}".format(verb, filename))
124-
121+
125122
setup, teardown = make_wrappers(filename)
126123
delta = test(
127124
setup,

mypy/myunit/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,10 @@ def tear_down(self) -> None:
131131
if self.suite:
132132
self.suite.tear_down()
133133
os.chdir(self.old_cwd)
134-
self.tmpdir.cleanup()
134+
try:
135+
self.tmpdir.cleanup()
136+
except OSError:
137+
pass
135138
self.old_cwd = None
136139
self.tmpdir = None
137140

mypy/test/data.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
import posixpath
66
import re
77
from os import remove, rmdir
8-
import shutil
98

109
import pytest # type: ignore # no pytest in typeshed
1110
from typing import Callable, List, Tuple, Set, Optional, Iterator, Any
1211

1312
from mypy.myunit import TestCase, SkipTestCaseException
13+
from mypy.util import delete_folder, delete_file
1414

1515

1616
def parse_test_cases(
@@ -228,13 +228,13 @@ def tear_down(self) -> None:
228228
# First remove files.
229229
for is_dir, path in reversed(self.clean_up):
230230
if not is_dir:
231-
remove(path)
231+
delete_file(path)
232232
# Then remove directories.
233233
for is_dir, path in reversed(self.clean_up):
234234
if is_dir:
235235
pycache = os.path.join(path, '__pycache__')
236236
if os.path.isdir(pycache):
237-
shutil.rmtree(pycache)
237+
delete_folder(pycache)
238238
try:
239239
rmdir(path)
240240
except OSError as error:
@@ -251,7 +251,7 @@ def tear_down(self) -> None:
251251
# Be defensive -- only call rmtree if we're sure we aren't removing anything
252252
# valuable.
253253
if path.startswith('tmp/') and os.path.isdir(path):
254-
shutil.rmtree(path)
254+
delete_folder(path)
255255
raise
256256
super().tear_down()
257257

mypy/test/testcheck.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
)
2222
from mypy.errors import CompileError
2323
from mypy.options import Options
24+
from mypy.util import delete_folder
2425

2526
from mypy import experiments
2627

@@ -113,10 +114,7 @@ def run_case(self, testcase: DataDrivenTestCase) -> None:
113114
experiments.STRICT_OPTIONAL = old_strict_optional
114115

115116
def clear_cache(self) -> None:
116-
dn = defaults.CACHE_DIR
117-
118-
if os.path.exists(dn):
119-
shutil.rmtree(dn)
117+
delete_folder(defaults.CACHE_DIR)
120118

121119
def run_case_once(self, testcase: DataDrivenTestCase, incremental: int = 0) -> None:
122120
find_module_clear_caches()

mypy/test/testcmdline.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from mypy.test.data import parse_test_cases, DataDrivenTestCase
1818
from mypy.test.helpers import assert_string_arrays_equal
1919
from mypy.version import __version__, base_version
20+
from mypy.util import delete_file
2021

2122
# Path to Python 3 interpreter
2223
python3_path = sys.executable
@@ -58,7 +59,7 @@ def test_python_evaluation(testcase: DataDrivenTestCase) -> None:
5859
# Split output into lines.
5960
out = [s.rstrip('\n\r') for s in str(outb, 'utf8').splitlines()]
6061
# Remove temp file.
61-
os.remove(program_path)
62+
delete_file(program_path)
6263
# Compare actual output to expected.
6364
if testcase.output_files:
6465
for path, expected_content in testcase.output_files:

mypy/test/testpythoneval.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
from mypy.test.config import test_data_prefix, test_temp_dir
2626
from mypy.test.data import DataDrivenTestCase, parse_test_cases
2727
from mypy.test.helpers import assert_string_arrays_equal
28-
from mypy.util import try_find_python2_interpreter
28+
from mypy.util import try_find_python2_interpreter, delete_file
2929

3030

3131
# Files which contain test case descriptions.
@@ -94,7 +94,7 @@ def test_python_evaluation(testcase: DataDrivenTestCase) -> None:
9494
returncode, interp_out = run([interpreter, program], env=env)
9595
out += interp_out
9696
# Remove temp file.
97-
os.remove(program_path)
97+
delete_file(program_path)
9898
assert_string_arrays_equal(adapt_output(testcase), out,
9999
'Invalid output ({}, line {})'.format(
100100
testcase.file, testcase.line))

mypy/test/teststubgen.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
parse_signature, parse_all_signatures, build_signature, find_unique_signatures,
2323
infer_sig_from_docstring
2424
)
25+
from mypy.util import delete_folder, delete_file
2526

2627

2728
class StubgenUtilSuite(Suite):
@@ -136,8 +137,8 @@ def test_stubgen(testcase: DataDrivenTestCase) -> None:
136137
testcase.file, testcase.line))
137138
finally:
138139
handle.close()
139-
os.unlink(handle.name)
140-
shutil.rmtree(out_dir)
140+
delete_file(handle.name)
141+
delete_folder(out_dir)
141142

142143

143144
def reset_importlib_caches() -> None:

mypy/util.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
import re
44
import subprocess
5+
import os
6+
import sys
7+
import shutil
58
from xml.sax.saxutils import escape
69
from typing import TypeVar, List, Tuple, Optional, Sequence, Dict
710

@@ -134,3 +137,21 @@ def id(self, o: object) -> int:
134137
self.id_map[o] = self.next_id
135138
self.next_id += 1
136139
return self.id_map[o]
140+
141+
142+
def delete_folder(folder_path: str) -> None:
143+
if sys.platform.startswith('win'):
144+
shutil.rmtree(folder_path, ignore_errors=True)
145+
else:
146+
if os.path.exists(folder_path):
147+
shutil.rmtree(folder_path)
148+
149+
150+
def delete_file(file_path: str) -> None:
151+
if sys.platform.startswith('win'):
152+
try:
153+
os.remove(file_path)
154+
except OSError:
155+
pass
156+
else:
157+
os.remove(file_path)

0 commit comments

Comments
 (0)