diff options
| author | Barney Gale <barney.gale@gmail.com> | 2024-04-12 21:19:21 (GMT) |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-04-12 21:19:21 (GMT) |
| commit | 0eb52f5f266d9e0a662f28a4d2dfef8c746cf96e (patch) | |
| tree | 2cccc9ca7a407f88e57b1bf1b7406a1c73005ce2 /Lib/test/test_pathlib/test_pathlib_abc.py | |
| parent | 069de14cb948f56b37e507f367b99c5563d3685e (diff) | |
| download | cpython-0eb52f5f266d9e0a662f28a4d2dfef8c746cf96e.zip cpython-0eb52f5f266d9e0a662f28a4d2dfef8c746cf96e.tar.gz cpython-0eb52f5f266d9e0a662f28a4d2dfef8c746cf96e.tar.bz2 | |
GH-115060: Speed up `pathlib.Path.glob()` by not scanning literal parts (#117732)
Don't bother calling `os.scandir()` to scan for literal pattern segments,
like `foo` in `foo/*.py`. Instead, append the segment(s) as-is and call
through to the next selector with `exists=False`, which signals that the
path might not exist. Subsequent selectors will call `os.scandir()` or
`os.lstat()` to filter out missing paths as needed.
Diffstat (limited to 'Lib/test/test_pathlib/test_pathlib_abc.py')
| -rw-r--r-- | Lib/test/test_pathlib/test_pathlib_abc.py | 21 |
1 files changed, 12 insertions, 9 deletions
diff --git a/Lib/test/test_pathlib/test_pathlib_abc.py b/Lib/test/test_pathlib/test_pathlib_abc.py index 336115c..6656b03 100644 --- a/Lib/test/test_pathlib/test_pathlib_abc.py +++ b/Lib/test/test_pathlib/test_pathlib_abc.py @@ -1429,10 +1429,10 @@ class DummyPath(PathBase): return "{}({!r})".format(self.__class__.__name__, self.as_posix()) def stat(self, *, follow_symlinks=True): - if follow_symlinks: - path = str(self.resolve()) + if follow_symlinks or self.name in ('', '.', '..'): + path = str(self.resolve(strict=True)) else: - path = str(self.parent.resolve() / self.name) + path = str(self.parent.resolve(strict=True) / self.name) if path in self._files: st_mode = stat.S_IFREG elif path in self._directories: @@ -1741,8 +1741,9 @@ class DummyPathTest(DummyPurePathTest): 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()) @@ -1753,8 +1754,6 @@ class DummyPathTest(DummyPurePathTest): 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 @@ -1857,8 +1856,9 @@ class DummyPathTest(DummyPurePathTest): def test_rglob_posix(self): P = self.cls p = P(self.base, "dirC") + q = p / "dirD" / "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()) @@ -1868,7 +1868,6 @@ class DummyPathTest(DummyPurePathTest): 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): @@ -1931,7 +1930,11 @@ class DummyPathTest(DummyPurePathTest): 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, "xyzzy", "..") }) self.assertEqual(set(p.glob("/".join([".."] * 50))), { P(self.base, *[".."] * 50)}) @needs_symlinks |
