8000 gh-101196: Make isdir/isfile/exists faster on Windows by mdboom · Pull Request #101324 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

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

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

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
39 8000 beb86
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
Add islink
  • Loading branch information
mdboom committed Jan 25, 2023
commit f6ce580b189d75978bf049409499aad98bcae667
7 changes: 4 additions & 3 deletions Lib/ntpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -852,11 +852,12 @@ def commonpath(paths):


try:
# The isdir(), isfile(), and exists() implementations in genericpath use
# os.stat(). This is overkill on Windows. Use simpler builtin functions
# if they are available.
# The isdir(), isfile(), islink() and exists() implementations in
# genericpath use os.stat(). This is overkill on Windows. Use simpler
# builtin functions if they are available.
from nt import _isdir as isdir
from nt import _isfile as isfile
from nt import _islink as islink
from nt import _exists as exists
except ImportError:
# Use genericpath.* as imported above
Expand Down
2 changes: 2 additions & 0 deletions Lib/test/test_ntpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -905,6 +905,8 @@ def test_fast_paths_in_use(self):
self.assertFalse(inspect.isfunction(os.path.isdir))
self.assertTrue(os.path.isfile is nt._isfile)
self.assertFalse(inspect.isfunction(os.path.isfile))
self.assertTrue(os.path.islink is nt._islink)
self.assertFalse(inspect.isfunction(os.path.islink))
self.assertTrue(os.path.exists is nt._exists)
self.assertFalse(inspect.isfunction(os.path.exists))

Expand Down
67 changes: 66 additions & 1 deletion Modules/clinic/posixmodule.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

67 changes: 66 additions & 1 deletion Modules/posixmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -15143,6 +15143,70 @@ os__exists_impl(PyObject *module, PyObject *path)
Py_RETURN_FALSE;
}
}


/*[clinic input]
os._islink
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
os._islink
os._path_islink


path: 'O'

Return True if path refers to an existing directory entry that is a symbolic link.

Always False if symbolic links are not supported by the Python runtime.
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
Always False if symbolic links are not supported by the Python runtime.
Return False if symbolic links are not supported by the Python runtime.

Copy link
Contributor

Choose a reason for hiding this comment

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

It should be the same as posixpath.islink(): "Test whether a path is a symbolic link". Symbolic links are supported in NT 6.0+, and we only support NT 6.3+.


[clinic start generated code]*/

static PyObject *
os__islink_impl(PyObject *module, PyObject *path)
8000
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
os__islink_impl(PyObject *module, PyObject *path)
os__path_islink_impl(PyObject *module, PyObject *path)

/*[clinic end generated code: output=597174066981ca21 input=df8e74a37ac3c6dc]*/
{
HANDLE hfile;
BOOL close_file = TRUE;
FILE_ATTRIBUTE_TAG_INFO info = { 0 };
path_t _path = PATH_T_INITIALIZE("islink", "path", 0, 1);
int result;

if (!path_converter(path, &_path)) {
path_cleanup(&_path);
if (PyErr_ExceptionMatches(PyExc_ValueError)) {
PyErr_Clear();
Py_RETURN_FALSE;
} else {
return NULL;
}
}

Py_BEGIN_ALLOW_THREADS
if (_path.fd != -1) {
hfile = _Py_get_osfhandle_noraise(_path.fd);
close_file = FALSE;
} else {
hfile = CreateFileW(_path.wide, FILE_READ_ATTRIBUTES, 0, NULL,
OPEN_EXISTING,
FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS,
NULL);
}
if (hfile != INVALID_HANDLE_VALUE) {
if (GetFileInformationByHandleEx(hfile, FileAttributeTagInfo, &info, sizeof(info))) {
result = (info.ReparseTag == IO_REPARSE_TAG_SYMLINK);
} else {
result = 0;
}
if (close_file) {
CloseHandle(hfile);
}
} else {
result = 0;
}
Py_END_ALLOW_THREADS

path_cleanup(&_path);
if (result) {
Py_RETURN_TRUE;
} else {
Py_RETURN_FALSE;
}
}
#endif


Expand Down Expand Up @@ -15335,8 +15399,9 @@ static PyMethodDef posix_methods[] = {
OS_SETNS_METHODDEF
OS_UNSHARE_METHODDEF

OS__ISFILE_METHODDEF
OS__ISDIR_METHODDEF
OS__ISFILE_METHODDEF
OS__ISLINK_METHODDEF
OS__EXISTS_METHODDEF
{NULL, NULL} /* Sentinel */
};
Expand Down
0