(original) (raw)
changeset: 99779:9826dbad1252 branch: 3.5 parent: 99776:224a026b4ca1 parent: 99778:18f5b125a863 user: Guido van Rossum guido@python.org date: Wed Jan 06 10:35:30 2016 -0800 files: Lib/pathlib.py Lib/test/test_pathlib.py Misc/NEWS description: Issue #26012: Don't traverse into symlinks for ** pattern in pathlib.Path.[r]glob(). (Merge 3.4->3.5) diff -r 224a026b4ca1 -r 9826dbad1252 Lib/pathlib.py --- a/Lib/pathlib.py Wed Jan 06 09:51:42 2016 -0800 +++ b/Lib/pathlib.py Wed Jan 06 10:35:30 2016 -0800 @@ -541,7 +541,7 @@ yield parent_path for name in listdir(parent_path): path = parent_path._make_child_relpath(name) - if is_dir(path): + if is_dir(path) and not path.is_symlink(): for p in self._iterate_directories(path, is_dir, listdir): yield p diff -r 224a026b4ca1 -r 9826dbad1252 Lib/test/test_pathlib.py --- a/Lib/test/test_pathlib.py Wed Jan 06 09:51:42 2016 -0800 +++ b/Lib/test/test_pathlib.py Wed Jan 06 10:35:30 2016 -0800 @@ -1241,7 +1241,7 @@ os.symlink('non-existing', join('brokenLink')) self.dirlink('dirB', join('linkB')) self.dirlink(os.path.join('..', 'dirB'), join('dirA', 'linkC')) - # This one goes upwards but doesn't create a loop + # This one goes upwards, creating a loop self.dirlink(os.path.join('..', 'dirB'), join('dirB', 'linkD')) if os.name == 'nt': @@ -1437,6 +1437,23 @@ _check(p.rglob("file*"), ["dirC/fileC", "dirC/dirD/fileD"]) _check(p.rglob("*/*"), ["dirC/dirD/fileD"]) + @with_symlinks + def test_rglob_symlink_loop(self): + # Don't get fooled by symlink loops (Issue #26012) + P = self.cls + p = P(BASE) + given = set(p.rglob('*')) + expect = {'brokenLink', + 'dirA', 'dirA/linkC', + 'dirB', 'dirB/fileB', 'dirB/linkD', + 'dirC', 'dirC/dirD', 'dirC/dirD/fileD', 'dirC/fileC', + 'dirE', + 'fileA', + 'linkA', + 'linkB', + } + self.assertEqual(given, {p / x for x in expect}) + def test_glob_dotdot(self): # ".." is not special in globs P = self.cls diff -r 224a026b4ca1 -r 9826dbad1252 Misc/NEWS --- a/Misc/NEWS Wed Jan 06 09:51:42 2016 -0800 +++ b/Misc/NEWS Wed Jan 06 10:35:30 2016 -0800 @@ -41,6 +41,9 @@ Library ------- +- Issue #26012: Don't traverse into symlinks for ** pattern in + pathlib.Path.[r]glob(). + - Issue #24120: Ignore PermissionError when traversing a tree with pathlib.Path.[r]glob(). Patch by Ulrich Petri. /guido@python.org