[New-bugs-announce] [issue36035] pathlib.Path().rglob() breaks with broken symlinks

Jörg Stucke report at bugs.python.org
Tue Feb 19 07:09:57 EST 2019


New submission from Jörg Stucke <joergstucke at gmail.com>:

When using rglob() to iterate over the files of a directory containing a broken symlink (a link pointing to itself) rglob breaks with "[Errno 40] Too many levels of symbolic links" (OS: Linux).

Steps to reproduce:

mkdir tmp
touch foo
ls -s foo tmp/foo
cd tmp
file foo
foo: broken symbolic link to foo

python3
>>> from pathlib import Path
>>> for f in Path().rglob("*"):
        print(x)

foo
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.8/pathlib.py", line 1105, in rglob
    for p in selector.select_from(self):
  File "/usr/local/lib/python3.8/pathlib.py", line 552, in _select_from
    for starting_point in self._iterate_directories(parent_path, is_dir, scandir):
  File "/usr/local/lib/python3.8/pathlib.py", line 536, in _iterate_directories
    entry_is_dir = entry.is_dir()
OSError: [Errno 40] Too many levels of symbolic links: './foo'


What is more, stat(), is_dir(), is_file() and exists() also do not like those broken links:
>>> Path("foo").is_file()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.8/pathlib.py", line 1361, in is_file
    return S_ISREG(self.stat().st_mode)
  File "/usr/local/lib/python3.8/pathlib.py", line 1151, in stat
    return self._accessor.stat(self)
OSError: [Errno 40] Too many levels of symbolic links: 'foo'


Is this intended behaviour or is this a bug? I guess it's not intended, since it makes it impossible to iterate over such a directory with rglob(). I could not find anything similar in the bug tracker, but https://bugs.python.org/issue26012 seems to be related.

Tested with Python 3.8.0a1, 3.6.7 and 3.5.2 (OS: Linux Mint 19)

----------
components: Library (Lib)
messages: 335937
nosy: Jörg Stucke
priority: normal
severity: normal
status: open
title: pathlib.Path().rglob() breaks with broken symlinks
type: behavior
versions: Python 3.5, Python 3.6, Python 3.8

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue36035>
_______________________________________


More information about the New-bugs-announce mailing list