Skip to content

Commit d5c120f

Browse files
jstuckepitrou
authored andcommitted
bpo-36035: fix Path.rglob for broken links (GH-11988)
Links creating an infinite symlink loop would raise an exception.
1 parent ccb7ca7 commit d5c120f

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):
@@ -520,7 +521,13 @@ def _select_from(self, parent_path, is_dir, exists, scandir):
520521
cf = parent_path._flavour.casefold
521522
entries = list(scandir(parent_path))
522523
for entry in entries:
523-
if not self.dironly or entry.is_dir():
524+
entry_is_dir = False
525+
try:
526+
entry_is_dir = entry.is_dir()
527+
except OSError as e:
528+
if not _ignore_error(e):
529+
raise
530+
if not self.dironly or entry_is_dir:
524531
name = entry.name
525532
casefolded = cf(name)
526533
if self.pat.match(casefolded):

Lib/test/test_pathlib.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1221,7 +1221,8 @@ class _BasePathTest(object):
12211221
# |-- dirE # No permissions
12221222
# |-- fileA
12231223
# |-- linkA -> fileA
1224-
# `-- linkB -> dirB
1224+
# |-- linkB -> dirB
1225+
# `-- brokenLinkLoop -> brokenLinkLoop
12251226
#
12261227

12271228
def setUp(self):
@@ -1252,6 +1253,8 @@ def cleanup():
12521253
self.dirlink(os.path.join('..', 'dirB'), join('dirA', 'linkC'))
12531254
# This one goes upwards, creating a loop.
12541255
self.dirlink(os.path.join('..', 'dirB'), join('dirB', 'linkD'))
1256+
# Broken symlink (pointing to itself).
1257+
os.symlink('brokenLinkLoop', join('brokenLinkLoop'))
12551258

12561259
if os.name == 'nt':
12571260
# Workaround for http://bugs.python.org/issue13772.
@@ -1384,7 +1387,7 @@ def test_iterdir(self):
13841387
paths = set(it)
13851388
expected = ['dirA', 'dirB', 'dirC', 'dirE', 'fileA']
13861389
if support.can_symlink():
1387-
expected += ['linkA', 'linkB', 'brokenLink']
1390+
expected += ['linkA', 'linkB', 'brokenLink', 'brokenLinkLoop']
13881391
self.assertEqual(paths, { P(BASE, q) for q in expected })
13891392

13901393
@support.skip_unless_symlink
@@ -1465,6 +1468,7 @@ def test_rglob_symlink_loop(self):
14651468
'fileA',
14661469
'linkA',
14671470
'linkB',
1471+
'brokenLinkLoop',
14681472
}
14691473
self.assertEqual(given, {p / x for x in expect})
14701474

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)