Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-101196: Make isdir/isfile/exists faster on Windows #101324

Merged
merged 32 commits into from
Feb 8, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
9a7d3d8
Make isdir/isfile/exists faster on Windows
mdboom Jan 25, 2023
a07f1e7
Fix doc syntax
mdboom Jan 25, 2023
7765fea
Merge branch 'main' into win-isdir-fastpath
mdboom Jan 25, 2023
684d683
Fix doc syntax
mdboom Jan 25, 2023
781fa07
Update Lib/ntpath.py
mdboom Jan 25, 2023
dda7be7
Mark test as CPython-only
mdboom Jan 25, 2023
565c2e1
Handle files correctly
mdboom Jan 25, 2023
53b932b
Remove unused variable
mdboom Jan 25, 2023
f6ce580
Add islink
mdboom Jan 25, 2023
88c8b25
Update CHANGELOG entry
mdboom Jan 25, 2023
39beb86
Handle uncommon error cases
mdboom Jan 26, 2023
9d4af5a
Fix drive test
mdboom Jan 26, 2023
1030d8a
Update Lib/test/test_ntpath.py
mdboom Jan 26, 2023
be6b592
Use STAT and LSTAT macros
mdboom Jan 26, 2023
0e465dc
Fix and add more tests
mdboom Jan 26, 2023
8fff56b
Merge remote-tracking branch 'origin/win-isdir-fastpath' into win-isd…
mdboom Jan 26, 2023
dcb9513
Don't unnecessarily zero-out info
mdboom Jan 31, 2023
0d2985d
Fix spelling of Win32
mdboom Jan 31, 2023
19018dc
PEP7
mdboom Jan 31, 2023
7583a1d
Reduce use of else
mdboom Jan 31, 2023
30cf754
Remove variable declarations
mdboom Jan 31, 2023
c0991ec
Docstring improvements
mdboom Jan 31, 2023
3400f07
No need for 'error' local variable
mdboom Jan 31, 2023
cb7cea3
Update generated code
mdboom Jan 31, 2023
636886e
PEP7
mdboom Jan 31, 2023
05c9165
Make docs consistent
mdboom Jan 31, 2023
5818815
Merge branch 'main' into win-isdir-fastpath
mdboom Jan 31, 2023
ff6bca9
Revert docstrings to the equivalent Python ones
mdboom Feb 1, 2023
6d48808
Revert docs changes
mdboom Feb 1, 2023
c7128bc
Move islink to genericpath.py
mdboom Feb 1, 2023
a72aba0
Regenerate clinic
mdboom Feb 1, 2023
aac93e4
Rename and reorganize - _isdir -> _path_isdir etc.
mdboom Feb 3, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Handle uncommon error cases
  • Loading branch information
mdboom committed Jan 26, 2023
commit 39beb86d98725e74f663d94b5d348c9f10c0c18d
4 changes: 4 additions & 0 deletions Include/pyport.h
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,10 @@ typedef Py_ssize_t Py_ssize_clean_t;
#define S_ISCHR(x) (((x) & S_IFMT) == S_IFCHR)
#endif

#ifndef S_ISLNK
#define S_ISLNK(x) (((x) & S_IFMT) == S_IFLNK)
#endif

#ifdef __cplusplus
/* Move this down here since some C++ #include's don't like to be included
inside an extern "C" */
Expand Down
1 change: 1 addition & 0 deletions Lib/test/test_os.py
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,7 @@ def test_access_denied(self):
)
result = os.stat(fname)
self.assertNotEqual(result.st_size, 0)
self.assertTrue(os.path.isfile(fname))

@unittest.skipUnless(sys.platform == "win32", "Win32 specific tests")
def test_stat_block_device(self):
Expand Down
72 changes: 68 additions & 4 deletions Modules/posixmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -14983,6 +14983,8 @@ os__isdir_impl(PyObject *module, PyObject *path)
BOOL close_file = TRUE;
FILE_BASIC_INFO info = { 0 };
path_t _path = PATH_T_INITIALIZE("isdir", "path", 0, 1);
STRUCT_STAT st;
DWORD error;
erlend-aasland marked this conversation as resolved.
Show resolved Hide resolved
int result;

if (!path_converter(path, &_path)) {
Expand Down Expand Up @@ -15010,7 +15012,21 @@ os__isdir_impl(PyObject *module, PyObject *path)
}
result = info.FileAttributes & FILE_ATTRIBUTE_DIRECTORY;
} else {
result = 0;
error = GetLastError();
switch (error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
error = GetLastError();
switch (error) {
switch (GetLastError()) {

case ERROR_ACCESS_DENIED:
case ERROR_SHARING_VIOLATION:
case ERROR_CANT_ACCESS_FILE:
case ERROR_INVALID_PARAMETER:
if (win32_stat(_path.wide, &st)) {
mdboom marked this conversation as resolved.
Show resolved Hide resolved
result = 0;
} else {
result = S_ISDIR(st.st_mode);
}
break;
default:
result = 0;
}
}
Py_END_ALLOW_THREADS

Expand Down Expand Up @@ -15043,6 +15059,8 @@ os__isfile_impl(PyObject *module, PyObject *path)
BOOL close_file = TRUE;
FILE_BASIC_INFO info = { 0 };
path_t _path = PATH_T_INITIALIZE("isfile", "path", 0, 1);
STRUCT_STAT st;
DWORD error;
int result;

if (!path_converter(path, &_path)) {
Expand Down Expand Up @@ -15073,7 +15091,21 @@ os__isfile_impl(PyObject *module, PyObject *path)
CloseHandle(hfile);
}
} else {
result = 0;
error = GetLastError();
switch (error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
error = GetLastError();
switch (error) {
switch (GetLastError()) {

case ERROR_ACCESS_DENIED:
case ERROR_SHARING_VIOLATION:
case ERROR_CANT_ACCESS_FILE:
case ERROR_INVALID_PARAMETER:
if (win32_stat(_path.wide, &st)) {
mdboom marked this conversation as resolved.
Show resolved Hide resolved
result = 0;
} else {
result = S_ISREG(st.st_mode);
}
break;
default:
result = 0;
}
}
Py_END_ALLOW_THREADS

Expand Down Expand Up @@ -15106,6 +15138,8 @@ os__exists_impl(PyObject *module, PyObject *path)
HANDLE hfile;
BOOL close_file = TRUE;
path_t _path = PATH_T_INITIALIZE("exists", "path", 0, 1);
STRUCT_STAT st;
DWORD error;
int result;

if (!path_converter(path, &_path)) {
Expand All @@ -15132,7 +15166,21 @@ os__exists_impl(PyObject *module, PyObject *path)
CloseHandle(hfile);
}
} else {
result = 0;
error = GetLastError();
switch (error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
error = GetLastError();
switch (error) {
switch (GetLastError()) {

case ERROR_ACCESS_DENIED:
case ERROR_SHARING_VIOLATION:
case ERROR_CANT_ACCESS_FILE:
case ERROR_INVALID_PARAMETER:
if (win32_stat(_path.wide, &st)) {
mdboom marked this conversation as resolved.
Show resolved Hide resolved
result = 0;
} else {
erlend-aasland marked this conversation as resolved.
Show resolved Hide resolved
result = 1;
}
break;
default:
result = 0;
}
}
Py_END_ALLOW_THREADS

Expand Down Expand Up @@ -15164,6 +15212,8 @@ os__islink_impl(PyObject *module, PyObject *path)
BOOL close_file = TRUE;
FILE_ATTRIBUTE_TAG_INFO info = { 0 };
path_t _path = PATH_T_INITIALIZE("islink", "path", 0, 1);
STRUCT_STAT st;
DWORD error;
int result;

if (!path_converter(path, &_path)) {
Expand Down Expand Up @@ -15196,7 +15246,21 @@ os__islink_impl(PyObject *module, PyObject *path)
CloseHandle(hfile);
}
} else {
result = 0;
error = GetLastError();
switch (error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
error = GetLastError();
switch (error) {
switch (GetLastError()) {

case ERROR_ACCESS_DENIED:
case ERROR_SHARING_VIOLATION:
case ERROR_CANT_ACCESS_FILE:
case ERROR_INVALID_PARAMETER:
if (win32_stat(_path.wide, &st)) {
mdboom marked this conversation as resolved.
Show resolved Hide resolved
result = 0;
} else {
result = S_ISLNK(st.st_mode);
}
break;
default:
result = 0;
}
}
Py_END_ALLOW_THREADS

Expand Down