8000 GH-115060: Speed up `pathlib.Path.glob()` by not scanning literal parts by barneygale · Pull Request #117732 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

GH-115060: Speed up pathlib.Path.glob() by not scanning literal parts #117732

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 4 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
Fix tests, add comment.
  • Loading branch information
barneygale committed Apr 10, 2024
commit 4c48dc098fb1604ac3a495982e1884432c0b34a3
3 changes: 3 additions & 0 deletions Lib/pathlib/_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,9 @@ def _glob_selector(self, parts, case_sensitive, recurse_symlinks):
case_sensitive = _is_case_sensitive(self.parser)
case_pedantic = False
else:
# The user has expressed a case sensitivity choice, but we don't
# know the case sensitivity of the underlying filesystem, so we
# must use scandir() for everything, including non-wildcard parts.
case_pedantic = True
recursive = True if recurse_symlinks else glob._no_recurse_symlinks
globber = self._globber(self.parser.sep, case_sensitive, case_pedantic, recursive)
Expand Down
15 changes: 9 additions & 6 deletions Lib/test/test_pathlib/test_pathlib_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1741,8 +1741,9 @@ def _check(glob, expected):
def test_glob_posix(self):
P = self.cls
p = P(self.base)
q = p / "FILEa"
given = set(p.glob("FILEa"))
expect = set()
expect = {q} if q.exists() else set()
self.assertEqual(given, expect)
self.assertEqual(set(p.glob("FILEa*")), set())

Expand All @@ -1753,8 +1754,6 @@ def test_glob_windows(self):
self.assertEqual(set(p.glob("FILEa")), { P(self.base, "fileA") })
self.assertEqual(set(p.glob("*a\\")), { P(self.base, "dirA/") })
self.assertEqual(set(p.glob("F*a")), { P(self.base, "fileA") })
self.assertEqual(set(map(str, p.glob("FILEa"))), {f"{p}\\fileA"})
self.assertEqual(set(map(str, p.glob("F*a"))), {f"{p}\\fileA"})

def test_glob_empty_pattern(self):
P = self.cls
Expand Down Expand Up @@ -1857,8 +1856,9 @@ def _check(path, glob, expected):
def test_rglob_posix(self):
P = self.cls
p = P(self.base, "dirC")
q = p / "dirC" / "FILEd"
given = set(p.rglob("FILEd"))
expect = set()
expect = {q} if q.exists() else set()
self.assertEqual(given, expect)
self.assertEqual(set(p.rglob("FILEd*")), set())

Expand All @@ -1868,7 +1868,6 @@ def test_rglob_windows(self):
p = P(self.base, "dirC")
self.assertEqual(set(p.rglob("FILEd")), { P(self.base, "dirC/dirD/fileD") })
self.assertEqual(set(p.rglob("*\\")), { P(self.base, "dirC/dirD/") })
self.assertEqual(set(map(str, p.rglob("FILEd"))), {f"{p}\\dirD\\fileD"})

@needs_symlinks
def test_rglob_recurse_symlinks_common(self):
Expand Down Expand Up @@ -1931,7 +1930,11 @@ def test_glob_dotdot(self):
self.assertEqual(set(p.glob("dirA/../file*")), { P(self.base, "dirA/../fileA") })
self.assertEqual(set(p.glob("dirA/../file*/..")), set())
self.assertEqual(set(p.glob("../xyzzy")), set())
self.assertEqual(set(p.glob("xyzzy/..")), set())
if self.cls.parser is posixpath:
self.assertEqual(set(p.glob("xyzzy/..")), set())
else:
# ".." segments are normalized first on Windows, so this path is stat()able.
self.assertEqual(set(p.glob("xyzzy/..")), { P(self.base, "zyzzy", "..") })
self.assertEqual(set(p.glob("/".join([".."] * 50))), { P(self.base, *[".."] * 50)})

@needs_symlinks
Expand Down
0