From 81dcf588b5e077e0acd066d75c47b9edc761e753 Mon Sep 17 00:00:00 2001 From: barneygale Date: Fri, 26 Jan 2024 19:46:39 +0000 Subject: [PATCH] GH-79634: Speed up globbing by removing `joinpath()` call. Remove `self.joinpath('')` call that should have been removed in 6313cdde. This makes `PathBase.glob('')` yield itself *without* adding a trailing slash. It's hard to say whether this is more or less correct, but at least everything else is faster, and there's no behaviour change in the public classes where empty glob patterns are disallowed. --- Lib/pathlib/_abc.py | 2 +- Lib/test/test_pathlib/test_pathlib.py | 2 ++ Lib/test/test_pathlib/test_pathlib_abc.py | 7 +++---- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/Lib/pathlib/_abc.py b/Lib/pathlib/_abc.py index 6303a18680befc..ad5684829ebc80 100644 --- a/Lib/pathlib/_abc.py +++ b/Lib/pathlib/_abc.py @@ -771,7 +771,7 @@ def glob(self, pattern, *, case_sensitive=None, follow_symlinks=None): filter_paths = False deduplicate_paths = False sep = self.pathmod.sep - paths = iter([self.joinpath('')] if self.is_dir() else []) + paths = iter([self] if self.is_dir() else []) while stack: part = stack.pop() if part in specials: diff --git a/Lib/test/test_pathlib/test_pathlib.py b/Lib/test/test_pathlib/test_pathlib.py index bdbe92369639ef..19773f013ed229 100644 --- a/Lib/test/test_pathlib/test_pathlib.py +++ b/Lib/test/test_pathlib/test_pathlib.py @@ -1824,6 +1824,8 @@ def test_glob_empty_pattern(self): list(p.glob('')) with self.assertRaisesRegex(ValueError, 'Unacceptable pattern'): list(p.glob('.')) + with self.assertRaisesRegex(ValueError, 'Unacceptable pattern'): + list(p.glob('./')) def test_glob_many_open_files(self): depth = 30 diff --git a/Lib/test/test_pathlib/test_pathlib_abc.py b/Lib/test/test_pathlib/test_pathlib_abc.py index 364f776dbb1413..0b81c4d50b5c87 100644 --- a/Lib/test/test_pathlib/test_pathlib_abc.py +++ b/Lib/test/test_pathlib/test_pathlib_abc.py @@ -1097,12 +1097,11 @@ def _check(glob, expected): _check(p.glob("*/"), ["dirA/", "dirB/", "dirC/", "dirE/", "linkB/"]) def test_glob_empty_pattern(self): - def _check(glob, expected): - self.assertEqual(set(glob), { P(self.base, q) for q in expected }) P = self.cls p = P(self.base) - _check(p.glob(""), [""]) - _check(p.glob("."), ["."]) + self.assertEqual(list(p.glob("")), [p]) + self.assertEqual(list(p.glob(".")), [p / "."]) + self.assertEqual(list(p.glob("./")), [p / "./"]) def test_glob_case_sensitive(self): P = self.cls