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 10 commits
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
12 changes: 7 additions & 5 deletions Lib/ntpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -852,11 +852,13 @@ def commonpath(paths):


try:
# The genericpath.isdir implementation uses os.stat and checks the mode
# attribute to tell whether or not the path is a directory.
# This is overkill on Windows - just pass the path to GetFileAttributes
# and check the attribute from there.
# 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
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
from nt import _isdir as isdir
from nt import _isfile as isfile
from nt import _islink as islink
from nt import _exists as exists
from nt import _path_isdir as isdir
from nt import _path_isfile as isfile
from nt import _path_islink as islink
from nt import _path_exists as exists

except ImportError:
# Use genericpath.isdir as imported above.
# Use genericpath.* as imported above
pass
23 changes: 22 additions & 1 deletion Lib/test/test_ntpath.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import inspect
import ntpath
import os
import sys
import unittest
import warnings
from test.support import os_helper
from test.support import cpython_only, os_helper
from test.support import TestFailed, is_emscripten
from test.support.os_helper import FakePath
from test import test_genericpath
Expand Down Expand Up @@ -889,6 +890,26 @@ def test_isjunction(self):
self.assertFalse(ntpath.isjunction('tmpdir'))
self.assertPathEqual(ntpath.realpath('testjunc'), ntpath.realpath('tmpdir'))

@unittest.skipIf(sys.platform != 'win32', "drive letters are a windows concept")
def test_isfile_driveletter(self):
current_drive = os.path.splitdrive(os.path.abspath(__file__))[0] + "\\"
self.assertFalse(os.path.isfile(current_drive))
Copy link
Contributor

@eryksun eryksun Jan 26, 2023

Choose a reason for hiding this comment

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

The reason this is false is because a relative drive path like "C:" resolves to the working directory on the drive. Did you want to test a volume device path like r'\\.\C:' instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, yes. And this is exactly the thing that (erroneously) returns True without checking the error code from GetFileInformationByHandleEx.


@unittest.skipIf(sys.platform != 'win32', "Fast paths are only for win32")
@cpython_only
def test_fast_paths_in_use(self):
# There are fast paths of these functions implemented in posixmodule.c.
# Confirm that they are being used, and not the Python fallbacks in
# genericpath.py.
self.assertTrue(os.path.isdir is nt._isdir)
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)
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
self.assertTrue(os.path.isdir is nt._isdir)
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.assertTrue(os.path.isdir is nt._path_isdir)
self.assertFalse(inspect.isfunction(os.path.isdir))
self.assertTrue(os.path.isfile is nt._path_isfile)
self.assertFalse(inspect.isfunction(os.path.isfile))
self.assertTrue(os.path.islink is nt._path_islink)
self.assertFalse(inspect.isfunction(os.path.islink))
self.assertTrue(os.path.exists is nt._path_exists)

self.assertFalse(inspect.isfunction(os.path.exists))


class NtCommonTest(test_genericpath.CommonTest, unittest.TestCase):
pathmodule = ntpath
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
The functions ``os.path.isdir``, ``os.path.isfile``, ``os.path.islink`` and
``os.path.exists`` are now 13% to 28% faster on Windows, by making fewer win32
mdboom marked this conversation as resolved.
Show resolved Hide resolved
API calls.
266 changes: 265 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.

Loading