From 2b77a61faf44ccce7fbc92279fcf3a3d0ba034f5 Mon Sep 17 00:00:00 2001 From: barneygale Date: Thu, 7 Dec 2023 23:17:19 +0000 Subject: [PATCH] Fix possible scoping issue in `_glob()` --- Lib/pathlib.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Lib/pathlib.py b/Lib/pathlib.py index b848bcd33b2c06..644a784bbcbbc9 100644 --- a/Lib/pathlib.py +++ b/Lib/pathlib.py @@ -92,6 +92,12 @@ def _compile_pattern(pat, sep, case_sensitive): return re.compile(regex, flags=flags).match +def _select_parents(paths, dir_only): + """Yield lexical parents of the given paths.""" + for path in paths: + yield path._make_child_relpath('..', dir_only) + + def _select_children(parent_paths, dir_only, follow_symlinks, match): """Yield direct children of given paths, filtering by name and type.""" if follow_symlinks is None: @@ -1105,7 +1111,7 @@ def _glob(self, pattern, case_sensitive, follow_symlinks): pass elif part == '..': dir_only = part_idx < len(pattern_parts) - paths = (path._make_child_relpath('..', dir_only) for path in paths) + paths = _select_parents(paths, dir_only) elif part == '**': # Consume adjacent '**' components. while part_idx < len(pattern_parts) and pattern_parts[part_idx] == '**':