8000 [3.11] gh-92550 - Fix regression in `pathlib.Path.rglob()` (GH-92583) by miss-islington · Pull Request #92589 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

[3.11] gh-92550 - Fix regression in pathlib.Path.rglob() (GH-92583) #92589

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 1 commit into from
May 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
gh-92550 - Fix regression in pathlib.Path.rglob() (GH-92583)
We could try to remedy this by taking a slice, but we then run into an issue where the empty string will match altsep on POSIX. That rabbit hole could keep getting deeper.

A proper fix for the original issue involves making pathlib's path normalisation more configurable - in this case we want to retain trailing slashes, but in other we might want to preserve `./` prefixes, or elide `../` segments when we're sure we won't encounter symlinks.

This reverts commit ea2f5bc.
(cherry picked from commit dcdf250)

Co-authored-by: Barney Gale <barney.gale@gmail.com>
  • Loading branch information
barneygale authored and miss-islington committed May 10, 2022
commit 8d8e823e726a9afb1a67d955379902b329a416c1
6 changes: 0 additions & 6 deletions Doc/library/pathlib.rst
Original file line number Diff line number Diff line change
Expand Up @@ -815,9 +815,6 @@ call fails (for example because the path doesn't exist).

.. audit-event:: pathlib.Path.glob self,pattern pathlib.Path.glob

.. versionchanged:: 3.11
Return only directories if *pattern* ends with a pathname components
separator (:data:`~os.sep` or :data:`~os.altsep`).

.. method:: Path.group()

Expand Down Expand Up @@ -1107,9 +1104,6 @@ call fails (for example because the path doesn't exist).

.. audit-event:: pathlib.Path.rglob self,pattern pathlib.Path.rglob

.. versionchanged:: 3.11
Return only directories if *pattern* ends with a pathname components
separator (:data:`~os.sep` or :data:`~os.altsep`).

.. method:: Path.rmdir()

Expand Down
9 changes: 0 additions & 9 deletions Doc/whatsnew/3.11.rst
Original file line number Diff line number Diff line change
Expand Up @@ -557,15 +557,6 @@ os
instead of ``CryptGenRandom()`` which is deprecated.
(Contributed by Dong-hee Na in :issue:`44611`.)


pathlib
-------

* :meth:`~pathlib.Path.glob` and :meth:`~pathlib.Path.rglob` return only
directories if *pattern* ends with a pathname components separator:
:data:`~os.sep` or :data:`~os.altsep`.
(Contributed by Eisuke Kawasima in :issue:`22276` and :issue:`33392`.)

re
--

Expand Down
6 changes: 0 additions & 6 deletions Lib/pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,6 @@ def make_uri(self, path):
def _make_selector(pattern_parts, flavour):
pat = pattern_parts[0]
child_parts = pattern_parts[1:]
if not pat:
return _TerminatingSelector()
if pat == '**':
cls = _RecursiveWildcardSelector
elif '**' in pat:
Expand Down Expand Up @@ -945,8 +943,6 @@ def glob(self, pattern):
drv, root, pattern_parts = self._flavour.parse_parts((pattern,))
if drv or root:
raise NotImplementedError("Non-relative patterns are unsupported")
if pattern[-1] in (self._flavour.sep, self._flavour.altsep):
pattern_parts.append('')
selector = _make_selector(tuple(pattern_parts), self._flavour)
for p in selector.select_from(self):
yield p
Expand All @@ -960,8 +956,6 @@ def rglob(self, pattern):
drv, root, pattern_parts = self._flavour.parse_parts((pattern,))
if drv or root:
raise NotImplementedError("Non-relative patterns are unsupported")
if pattern[-1] in (self._flavour.sep, self._flavour.altsep):
pattern_parts.append('')
selector = _make_selector(("**",) + tuple(pattern_parts), self._flavour)
for p in selector.select_from(self):
yield p
Expand Down
17 changes: 0 additions & 17 deletions Lib/test/test_pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1662,11 +1662,6 @@ def _check(glob, expected):
else:
_check(p.glob("*/fileB"), ['dirB/fileB', 'linkB/fileB'])

if not os_helper.can_symlink():
_check(p.glob("*/"), ["dirA", "dirB", "dirC", "dirE"])
else:
_check(p.glob("*/"), ["dirA", "dirB", "dirC", "dirE", "linkB"])

def test_rglob_common(self):
def _check(glob, expected):
self.assertEqual(set(glob), { P(BASE, q) for q in expected })
Expand All @@ -1684,16 +1679,6 @@ def _check(glob, expected):
"linkB/fileB", "dirA/linkC/fileB"])
_check(p.rglob("file*"), ["fileA", "dirB/fileB",
"dirC/fileC", "dirC/dirD/fileD"])
if not os_helper.can_symlink():
_check(p.rglob("*/"), [
"dirA", "dirB", "dirC", "dirC/dirD", "dirE",
])
else:
_check(p.rglob("*/"), [
"dirA", "dirA/linkC", "dirB", "dirB/linkD", "dirC",
"dirC/dirD", "dirE", "linkB",
])

p = P(BASE, "dirC")
_check(p.rglob("file*"), ["dirC/fileC", "dirC/dirD/fileD"])
_check(p.rglob("*/*"), ["dirC/dirD/fileD"])
Expand Down Expand Up @@ -2719,7 +2704,6 @@ def test_glob(self):
P = self.cls
p = P(BASE)
self.assertEqual(set(p.glob("FILEa")), { P(BASE, "fileA") })
self.assertEqual(set(p.glob("*a\\")), { P(BASE, "dirA") })
self.assertEqual(set(p.glob("F*a")), { P(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"})
Expand All @@ -2728,7 +2712,6 @@ def test_rglob(self):
P = self.cls
p = P(BASE, "dirC")
self.assertEqual(set(p.rglob("FILEd")), { P(BASE, "dirC/dirD/fileD") })
self.assertEqual(set(p.rglob("*\\")), { P(BASE, "dirC/dirD") })
self.assertEqual(set(map(str, p.rglob("FILEd"))), {f"{p}\\dirD\\FILEd"})

def test_expanduser(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:meth:`pathlib.Path.rglob` raised :exc:`IndexError` when called with an
empty string. This regression was introduced in 3.11b1.
0