-
-
Notifications
You must be signed in to change notification settings - Fork 612
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove unnecessary system calls via os.path.exists()/os.path.isdir()
Rather than checking a file exists, then reading it, simply read it and handle the potential ENOENT OS error. This reduces the number of system calls from: 1. check file exists 2. read file To: 1. read file Reducing system calls is an optimization. More importantly, this also avoids race conditions that were previously not handled. If the file was deleted after the existence check but before reading it, an unhandled FileNotFound error would be raised. This approach better follows Python pattern "Easier to ask for forgiveness than permission" rather than the "Look before you leap" approach. See: https://docs.python.org/3/glossary.html#term-eafp The same pattern applies to os.makedirs(). A compatibility shim was added to allow using the exist_ok parameter on Python 2.
- Loading branch information
Showing
5 changed files
with
65 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import os | ||
|
||
import pytest | ||
|
||
from piptools._compat import makedirs | ||
|
||
|
||
def test_makedirs_exist_ok_true(tmpdir): | ||
path = str(tmpdir / "test") | ||
makedirs(path, exist_ok=True) | ||
assert os.path.isdir(path) | ||
makedirs(path, exist_ok=True) | ||
assert os.path.isdir(path) | ||
|
||
|
||
def test_makedirs_exist_ok_false(tmpdir): | ||
path = str(tmpdir / "test") | ||
makedirs(path) | ||
assert os.path.isdir(path) | ||
with pytest.raises(OSError, match="exists"): | ||
makedirs(path) |