From b852339e2f1ed74b5a03ce454cee5b13efed508d Mon Sep 17 00:00:00 2001 From: barneygale Date: Thu, 7 Dec 2023 23:12:08 +0000 Subject: [PATCH] Simplify `_glob()` slightly --- Lib/pathlib.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Lib/pathlib.py b/Lib/pathlib.py index f181a4ce33a5e1..b848bcd33b2c06 100644 --- a/Lib/pathlib.py +++ b/Lib/pathlib.py @@ -1065,21 +1065,21 @@ def _glob(self, pattern, case_sensitive, follow_symlinks): elif not path_pattern._tail: raise ValueError("Unacceptable pattern: {!r}".format(pattern)) - sep = self.pathmod.sep - pattern = str(path_pattern) - pattern_parts = pattern.split(sep) - if pattern_parts[-1] == '**': + if path_pattern.name == '**' and not path_pattern.has_trailing_sep: # GH-70303: '**' only matches directories. Add trailing slash. warnings.warn( "Pattern ending '**' will match files and directories in a " "future Python release. Add a trailing slash to match only " "directories and remove this warning.", FutureWarning, 3) - pattern_parts.append('') + path_pattern = path_pattern.with_trailing_sep() if case_sensitive is None: # TODO: evaluate case-sensitivity of each directory in _select_children(). case_sensitive = _is_case_sensitive(self.pathmod) + sep = self.pathmod.sep + pattern_str = str(path_pattern) + pattern_parts = pattern_str.split(sep) # If symlinks are handled consistently, and the pattern does not # contain '..' components, then we can use a 'walk-and-match' strategy @@ -1117,7 +1117,7 @@ def _glob(self, pattern, case_sensitive, follow_symlinks): # Filter out paths that don't match pattern. prefix_len = len(str(self._make_child_relpath('_'))) - 1 - match = _compile_pattern(pattern, sep, case_sensitive) + match = _compile_pattern(pattern_str, sep, case_sensitive) paths = (path for path in paths if match(str(path), prefix_len)) return paths