Skip to content

[3.7] bpo-36035: fix Path.rglob for broken links (GH-11988) #13469

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 21, 2019
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
13 changes: 10 additions & 3 deletions Lib/pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -35,10 +35,11 @@
#

# EBADF - guard against 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
1921, # ERROR_CANT_RESOLVE_FILENAME - fix for broken symlink pointing to itself
)

def _ignore_error(exception):
Expand Down Expand Up @@ -518,7 +519,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):
Expand Down
8 changes: 6 additions & 2 deletions Lib/test/test_pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1218,7 +1218,8 @@ class _BasePathTest(object):
# |-- dirE # No permissions
# |-- fileA
# |-- linkA -> fileA
# `-- linkB -> dirB
# |-- linkB -> dirB
# `-- brokenLinkLoop -> brokenLinkLoop
#

def setUp(self):
Expand Down Expand Up @@ -1249,6 +1250,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
Expand Down Expand Up @@ -1379,7 +1382,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
Expand Down Expand Up @@ -1460,6 +1463,7 @@ def test_rglob_symlink_loop(self):
'fileA',
'linkA',
'linkB',
'brokenLinkLoop',
}
self.assertEqual(given, {p / x for x in expect})

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added fix for broken symlinks in combination with pathlib