Skip to content

Commit e4d1be1

Browse files
committed
Add another try/except PermissionError to avoid depending on listdir order. Fix issues #24120 and #26012.
1 parent 62933db commit e4d1be1

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

Lib/pathlib.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -494,11 +494,14 @@ def __init__(self, pat, child_parts):
494494

495495
def _iterate_directories(self, parent_path, is_dir, listdir):
496496
yield parent_path
497-
for name in listdir(parent_path):
498-
path = parent_path._make_child_relpath(name)
499-
if is_dir(path) and not path.is_symlink():
500-
for p in self._iterate_directories(path, is_dir, listdir):
501-
yield p
497+
try:
498+
for name in listdir(parent_path):
499+
path = parent_path._make_child_relpath(name)
500+
if is_dir(path) and not path.is_symlink():
501+
for p in self._iterate_directories(path, is_dir, listdir):
502+
yield p
503+
except PermissionError:
504+
return
502505

503506
def _select_from(self, parent_path, is_dir, exists, listdir):
504507
try:

Lib/test/test_pathlib.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,7 +1240,7 @@ class _BasePathTest(object):
12401240
# | |-- dirD
12411241
# | | `-- fileD
12421242
# | `-- fileC
1243-
# |-- dirE
1243+
# |-- dirE # No permissions
12441244
# |-- fileA
12451245
# |-- linkA -> fileA
12461246
# `-- linkB -> dirB
@@ -1396,13 +1396,13 @@ def _check(glob, expected):
13961396
p = P(BASE)
13971397
it = p.rglob("fileA")
13981398
self.assertIsInstance(it, collections.Iterator)
1399-
# XXX cannot test because of symlink loops in the test setup
1400-
#_check(it, ["fileA"])
1401-
#_check(p.rglob("fileB"), ["dirB/fileB"])
1402-
#_check(p.rglob("*/fileA"), [""])
1403-
#_check(p.rglob("*/fileB"), ["dirB/fileB"])
1404-
#_check(p.rglob("file*"), ["fileA", "dirB/fileB"])
1405-
# No symlink loops here
1399+
_check(it, ["fileA"])
1400+
_check(p.rglob("fileB"), ["dirB/fileB"])
1401+
_check(p.rglob("*/fileA"), [])
1402+
_check(p.rglob("*/fileB"), ["dirB/fileB", "dirB/linkD/fileB",
1403+
"linkB/fileB", "dirA/linkC/fileB"])
1404+
_check(p.rglob("file*"), ["fileA", "dirB/fileB",
1405+
"dirC/fileC", "dirC/dirD/fileD"])
14061406
p = P(BASE, "dirC")
14071407
_check(p.rglob("file*"), ["dirC/fileC", "dirC/dirD/fileD"])
14081408
_check(p.rglob("*/*"), ["dirC/dirD/fileD"])

0 commit comments

Comments
 (0)