From 6f38797d6b3dd76ab797f2d31775c52e69bb0c0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Stucke?= Date: Fri, 22 Feb 2019 14:25:46 +0100 Subject: [PATCH 1/3] bpo-36035: fix rglob for broken links --- Lib/pathlib.py | 12 +++++++++--- Lib/test/test_pathlib.py | 8 ++++++-- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/Lib/pathlib.py b/Lib/pathlib.py index 911b774b564984..2dafefbb93ee05 100644 --- a/Lib/pathlib.py +++ b/Lib/pathlib.py @@ -7,7 +7,7 @@ import re import sys from _collections_abc import Sequence -from errno import EINVAL, ENOENT, ENOTDIR, EBADF +from errno import EINVAL, ENOENT, ENOTDIR, EBADF, ELOOP from operator import attrgetter from stat import S_ISDIR, S_ISLNK, S_ISREG, S_ISSOCK, S_ISBLK, S_ISCHR, S_ISFIFO from urllib.parse import quote_from_bytes as urlquote_from_bytes @@ -35,7 +35,7 @@ # # EBADF - guard agains macOS `stat` throwing EBADF -_IGNORED_ERROS = (ENOENT, ENOTDIR, EBADF) +_IGNORED_ERROS = (ENOENT, ENOTDIR, EBADF, ELOOP) _IGNORED_WINERRORS = ( 21, # ERROR_NOT_READY - drive exists but is not accessible @@ -518,7 +518,13 @@ def _select_from(self, parent_path, is_dir, exists, scandir): cf = parent_path._flavour.casefold entries = list(scandir(parent_path)) for entry in entries: - if not self.dironly or entry.is_dir(): + entry_is_dir = False + try: + entry_is_dir = entry.is_dir() + except OSError as e: + if not _ignore_error(e): + raise + if not self.dironly or entry_is_dir: name = entry.name casefolded = cf(name) if self.pat.match(casefolded): diff --git a/Lib/test/test_pathlib.py b/Lib/test/test_pathlib.py index f8325eb93275a6..1a8b97abb7c1b0 100644 --- a/Lib/test/test_pathlib.py +++ b/Lib/test/test_pathlib.py @@ -1221,7 +1221,8 @@ class _BasePathTest(object): # |-- dirE # No permissions # |-- fileA # |-- linkA -> fileA - # `-- linkB -> dirB + # |-- linkB -> dirB + # `-- brokenLinkLoop -> brokenLinkLoop # def setUp(self): @@ -1252,6 +1253,8 @@ def cleanup(): self.dirlink(os.path.join('..', 'dirB'), join('dirA', 'linkC')) # This one goes upwards, creating a loop. self.dirlink(os.path.join('..', 'dirB'), join('dirB', 'linkD')) + # Broken symlink (pointing to itself). + os.symlink('brokenLinkLoop', join('brokenLinkLoop')) if os.name == 'nt': # Workaround for http://bugs.python.org/issue13772. @@ -1384,7 +1387,7 @@ def test_iterdir(self): paths = set(it) expected = ['dirA', 'dirB', 'dirC', 'dirE', 'fileA'] if support.can_symlink(): - expected += ['linkA', 'linkB', 'brokenLink'] + expected += ['linkA', 'linkB', 'brokenLink', 'brokenLinkLoop'] self.assertEqual(paths, { P(BASE, q) for q in expected }) @support.skip_unless_symlink @@ -1465,6 +1468,7 @@ def test_rglob_symlink_loop(self): 'fileA', 'linkA', 'linkB', + 'brokenLinkLoop', } self.assertEqual(given, {p / x for x in expect}) From 2ca80d30b81cbde9b548a53defc90c132bce9b3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Stucke?= Date: Fri, 22 Feb 2019 15:26:40 +0100 Subject: [PATCH 2/3] bpo-36035: fix for broken links in windows --- Lib/pathlib.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Lib/pathlib.py b/Lib/pathlib.py index 2dafefbb93ee05..d42140ec2b51f8 100644 --- a/Lib/pathlib.py +++ b/Lib/pathlib.py @@ -39,6 +39,7 @@ _IGNORED_WINERRORS = ( 21, # ERROR_NOT_READY - drive exists but is not accessible + 1921, # ERROR_CANT_RESOLVE_FILENAME - fix for broken symlink pointing to itself ) def _ignore_error(exception): From 8974e89f995d5ba6572aa48277d4da5e92d01912 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" Date: Fri, 22 Feb 2019 14:30:23 +0000 Subject: [PATCH 3/3] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Core and Builtins/2019-02-22-14-30-19.bpo-36035.-6dy1y.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2019-02-22-14-30-19.bpo-36035.-6dy1y.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2019-02-22-14-30-19.bpo-36035.-6dy1y.rst b/Misc/NEWS.d/next/Core and Builtins/2019-02-22-14-30-19.bpo-36035.-6dy1y.rst new file mode 100644 index 00000000000000..1e4f7ac71f3395 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2019-02-22-14-30-19.bpo-36035.-6dy1y.rst @@ -0,0 +1 @@ +Added fix for broken symlinks in combination with pathlib \ No newline at end of file