Skip to content

Commit 88ef8db

Browse files
authored
fix: avoid races in filesystem ops (#2287)
Use `exist_ok` and `missing_ok` in pathlib operations where we can.
1 parent 4d37797 commit 88ef8db

File tree

4 files changed

+4
-8
lines changed

4 files changed

+4
-8
lines changed

bin/update_how_it_works_image.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ def main() -> None:
4242
)
4343

4444
dest_path = Path("docs/data/how-it-works.png")
45-
if dest_path.exists():
46-
dest_path.unlink()
45+
dest_path.unlink(missing_ok=True)
4746

4847
Path(screenshot).rename(dest_path)
4948

cibuildwheel/__main__.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -399,8 +399,7 @@ def build_in_directory(args: CommandLineArguments) -> None:
399399

400400
output_dir = options.globals.output_dir
401401

402-
if not output_dir.exists():
403-
output_dir.mkdir(parents=True)
402+
output_dir.mkdir(parents=True, exist_ok=True)
404403

405404
tmp_path = Path(mkdtemp(prefix="cibw-run-")).resolve(strict=True)
406405
try:

cibuildwheel/util/file.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@
2525
def download(url: str, dest: Path) -> None:
2626
print(f"+ Download {url} to {dest}")
2727
dest_dir = dest.parent
28-
if not dest_dir.exists():
29-
dest_dir.mkdir(parents=True)
28+
dest_dir.mkdir(parents=True, exist_ok=True)
3029

3130
# we've had issues when relying on the host OS' CA certificates on Windows,
3231
# so we use certifi (this sounds odd but requests also does this by default)

test/test_custom_repair_wheel.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121
name = f"spam-0.1.0-py2-none-{platform}.whl"
2222
dest = dest_dir / name
2323
dest_dir.mkdir(parents=True, exist_ok=True)
24-
if dest.exists():
25-
dest.unlink()
24+
dest.unlink(missing_ok=True)
2625
shutil.copy(wheel, dest)
2726
"""
2827

0 commit comments

Comments
 (0)