Skip to content

Commit aea49b1

Browse files
[3.7] bpo-36035: fix Path.rglob for broken links (GH-11988) (GH-13469)
Links creating an infinite symlink loop would raise an exception. (cherry picked from commit d5c120f) Co-authored-by: Jörg Stucke <[email protected]> https://bugs.python.org/issue36035
1 parent 390d88e commit aea49b1

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

Lib/pathlib.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import re
88
import sys
99
from _collections_abc import Sequence
10-
from errno import EINVAL, ENOENT, ENOTDIR, EBADF
10+
from errno import EINVAL, ENOENT, ENOTDIR, EBADF, ELOOP
1111
from operator import attrgetter
1212
from stat import S_ISDIR, S_ISLNK, S_ISREG, S_ISSOCK, S_ISBLK, S_ISCHR, S_ISFIFO
1313
from urllib.parse import quote_from_bytes as urlquote_from_bytes
@@ -35,10 +35,11 @@
3535
#
3636

3737
# EBADF - guard against macOS `stat` throwing EBADF
38-
_IGNORED_ERROS = (ENOENT, ENOTDIR, EBADF)
38+
_IGNORED_ERROS = (ENOENT, ENOTDIR, EBADF, ELOOP)
3939

4040
_IGNORED_WINERRORS = (
4141
21, # ERROR_NOT_READY - drive exists but is not accessible
42+
1921, # ERROR_CANT_RESOLVE_FILENAME - fix for broken symlink pointing to itself
4243
)
4344

4445
def _ignore_error(exception):
@@ -518,7 +519,13 @@ def _select_from(self, parent_path, is_dir, exists, scandir):
518519
cf = parent_path._flavour.casefold
519520
entries = list(scandir(parent_path))
520521
for entry in entries:
521-
if not self.dironly or entry.is_dir():
522+
entry_is_dir = False
523+
try:
524+
entry_is_dir = entry.is_dir()
525+
except OSError as e:
526+
if not _ignore_error(e):
527+
raise
528+
if not self.dironly or entry_is_dir:
522529
name = entry.name
523530
casefolded = cf(name)
524531
if self.pat.match(casefolded):

Lib/test/test_pathlib.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1218,7 +1218,8 @@ class _BasePathTest(object):
12181218
# |-- dirE # No permissions
12191219
# |-- fileA
12201220
# |-- linkA -> fileA
1221-
# `-- linkB -> dirB
1221+
# |-- linkB -> dirB
1222+
# `-- brokenLinkLoop -> brokenLinkLoop
12221223
#
12231224

12241225
def setUp(self):
@@ -1249,6 +1250,8 @@ def cleanup():
12491250
self.dirlink(os.path.join('..', 'dirB'), join('dirA', 'linkC'))
12501251
# This one goes upwards, creating a loop
12511252
self.dirlink(os.path.join('..', 'dirB'), join('dirB', 'linkD'))
1253+
# Broken symlink (pointing to itself).
1254+
os.symlink('brokenLinkLoop', join('brokenLinkLoop'))
12521255

12531256
if os.name == 'nt':
12541257
# Workaround for http://bugs.python.org/issue13772
@@ -1379,7 +1382,7 @@ def test_iterdir(self):
13791382
paths = set(it)
13801383
expected = ['dirA', 'dirB', 'dirC', 'dirE', 'fileA']
13811384
if support.can_symlink():
1382-
expected += ['linkA', 'linkB', 'brokenLink']
1385+
expected += ['linkA', 'linkB', 'brokenLink', 'brokenLinkLoop']
13831386
self.assertEqual(paths, { P(BASE, q) for q in expected })
13841387

13851388
@support.skip_unless_symlink
@@ -1460,6 +1463,7 @@ def test_rglob_symlink_loop(self):
14601463
'fileA',
14611464
'linkA',
14621465
'linkB',
1466+
'brokenLinkLoop',
14631467
}
14641468
self.assertEqual(given, {p / x for x in expect})
14651469

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added fix for broken symlinks in combination with pathlib

0 commit comments

Comments
 (0)