bpo-36035: fix Path.rglob for broken links (GH-11988)
Links creating an infinite symlink loop would raise an exception.
This commit is contained in:
parent
ccb7ca728e
commit
d5c120f7eb
|
@ -7,7 +7,7 @@ import posixpath
|
||||||
import re
|
import re
|
||||||
import sys
|
import sys
|
||||||
from _collections_abc import Sequence
|
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 operator import attrgetter
|
||||||
from stat import S_ISDIR, S_ISLNK, S_ISREG, S_ISSOCK, S_ISBLK, S_ISCHR, S_ISFIFO
|
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
|
from urllib.parse import quote_from_bytes as urlquote_from_bytes
|
||||||
|
@ -35,10 +35,11 @@ __all__ = [
|
||||||
#
|
#
|
||||||
|
|
||||||
# EBADF - guard against macOS `stat` throwing EBADF
|
# EBADF - guard against macOS `stat` throwing EBADF
|
||||||
_IGNORED_ERROS = (ENOENT, ENOTDIR, EBADF)
|
_IGNORED_ERROS = (ENOENT, ENOTDIR, EBADF, ELOOP)
|
||||||
|
|
||||||
_IGNORED_WINERRORS = (
|
_IGNORED_WINERRORS = (
|
||||||
21, # ERROR_NOT_READY - drive exists but is not accessible
|
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):
|
def _ignore_error(exception):
|
||||||
|
@ -520,7 +521,13 @@ class _WildcardSelector(_Selector):
|
||||||
cf = parent_path._flavour.casefold
|
cf = parent_path._flavour.casefold
|
||||||
entries = list(scandir(parent_path))
|
entries = list(scandir(parent_path))
|
||||||
for entry in entries:
|
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
|
name = entry.name
|
||||||
casefolded = cf(name)
|
casefolded = cf(name)
|
||||||
if self.pat.match(casefolded):
|
if self.pat.match(casefolded):
|
||||||
|
|
|
@ -1221,7 +1221,8 @@ class _BasePathTest(object):
|
||||||
# |-- dirE # No permissions
|
# |-- dirE # No permissions
|
||||||
# |-- fileA
|
# |-- fileA
|
||||||
# |-- linkA -> fileA
|
# |-- linkA -> fileA
|
||||||
# `-- linkB -> dirB
|
# |-- linkB -> dirB
|
||||||
|
# `-- brokenLinkLoop -> brokenLinkLoop
|
||||||
#
|
#
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
|
@ -1252,6 +1253,8 @@ class _BasePathTest(object):
|
||||||
self.dirlink(os.path.join('..', 'dirB'), join('dirA', 'linkC'))
|
self.dirlink(os.path.join('..', 'dirB'), join('dirA', 'linkC'))
|
||||||
# This one goes upwards, creating a loop.
|
# This one goes upwards, creating a loop.
|
||||||
self.dirlink(os.path.join('..', 'dirB'), join('dirB', 'linkD'))
|
self.dirlink(os.path.join('..', 'dirB'), join('dirB', 'linkD'))
|
||||||
|
# Broken symlink (pointing to itself).
|
||||||
|
os.symlink('brokenLinkLoop', join('brokenLinkLoop'))
|
||||||
|
|
||||||
if os.name == 'nt':
|
if os.name == 'nt':
|
||||||
# Workaround for http://bugs.python.org/issue13772.
|
# Workaround for http://bugs.python.org/issue13772.
|
||||||
|
@ -1384,7 +1387,7 @@ class _BasePathTest(object):
|
||||||
paths = set(it)
|
paths = set(it)
|
||||||
expected = ['dirA', 'dirB', 'dirC', 'dirE', 'fileA']
|
expected = ['dirA', 'dirB', 'dirC', 'dirE', 'fileA']
|
||||||
if support.can_symlink():
|
if support.can_symlink():
|
||||||
expected += ['linkA', 'linkB', 'brokenLink']
|
expected += ['linkA', 'linkB', 'brokenLink', 'brokenLinkLoop']
|
||||||
self.assertEqual(paths, { P(BASE, q) for q in expected })
|
self.assertEqual(paths, { P(BASE, q) for q in expected })
|
||||||
|
|
||||||
@support.skip_unless_symlink
|
@support.skip_unless_symlink
|
||||||
|
@ -1465,6 +1468,7 @@ class _BasePathTest(object):
|
||||||
'fileA',
|
'fileA',
|
||||||
'linkA',
|
'linkA',
|
||||||
'linkB',
|
'linkB',
|
||||||
|
'brokenLinkLoop',
|
||||||
}
|
}
|
||||||
self.assertEqual(given, {p / x for x in expect})
|
self.assertEqual(given, {p / x for x in expect})
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Added fix for broken symlinks in combination with pathlib
|
Loading…
Reference in New Issue