[Python-checkins] cpython (3.4): Add another try/except PermissionError to avoid depending on listdir order. Fix
guido.van.rossum
python-checkins at python.org
Thu Jan 7 13:59:01 EST 2016
https://hg.python.org/cpython/rev/4043e08e6e52
changeset: 99798:4043e08e6e52
branch: 3.4
parent: 99790:94b0e5e9b6e8
user: Guido van Rossum <guido at python.org>
date: Thu Jan 07 10:56:36 2016 -0800
summary:
Add another try/except PermissionError to avoid depending on listdir order. Fix issues #24120 and #26012.
files:
Lib/pathlib.py | 13 ++++++++-----
Lib/test/test_pathlib.py | 16 ++++++++--------
2 files changed, 16 insertions(+), 13 deletions(-)
diff --git a/Lib/pathlib.py b/Lib/pathlib.py
--- a/Lib/pathlib.py
+++ b/Lib/pathlib.py
@@ -494,11 +494,14 @@
def _iterate_directories(self, parent_path, is_dir, listdir):
yield parent_path
- for name in listdir(parent_path):
- path = parent_path._make_child_relpath(name)
- if is_dir(path) and not path.is_symlink():
- for p in self._iterate_directories(path, is_dir, listdir):
- yield p
+ try:
+ for name in listdir(parent_path):
+ path = parent_path._make_child_relpath(name)
+ if is_dir(path) and not path.is_symlink():
+ for p in self._iterate_directories(path, is_dir, listdir):
+ yield p
+ except PermissionError:
+ return
def _select_from(self, parent_path, is_dir, exists, listdir):
try:
diff --git a/Lib/test/test_pathlib.py b/Lib/test/test_pathlib.py
--- a/Lib/test/test_pathlib.py
+++ b/Lib/test/test_pathlib.py
@@ -1240,7 +1240,7 @@
# | |-- dirD
# | | `-- fileD
# | `-- fileC
- # |-- dirE
+ # |-- dirE # No permissions
# |-- fileA
# |-- linkA -> fileA
# `-- linkB -> dirB
@@ -1396,13 +1396,13 @@
p = P(BASE)
it = p.rglob("fileA")
self.assertIsInstance(it, collections.Iterator)
- # XXX cannot test because of symlink loops in the test setup
- #_check(it, ["fileA"])
- #_check(p.rglob("fileB"), ["dirB/fileB"])
- #_check(p.rglob("*/fileA"), [""])
- #_check(p.rglob("*/fileB"), ["dirB/fileB"])
- #_check(p.rglob("file*"), ["fileA", "dirB/fileB"])
- # No symlink loops here
+ _check(it, ["fileA"])
+ _check(p.rglob("fileB"), ["dirB/fileB"])
+ _check(p.rglob("*/fileA"), [])
+ _check(p.rglob("*/fileB"), ["dirB/fileB", "dirB/linkD/fileB",
+ "linkB/fileB", "dirA/linkC/fileB"])
+ _check(p.rglob("file*"), ["fileA", "dirB/fileB",
+ "dirC/fileC", "dirC/dirD/fileD"])
p = P(BASE, "dirC")
_check(p.rglob("file*"), ["dirC/fileC", "dirC/dirD/fileD"])
_check(p.rglob("*/*"), ["dirC/dirD/fileD"])
--
Repository URL: https://hg.python.org/cpython
More information about the Python-checkins
mailing list