From dd450785c101a9af3b0bb433b3ae543e271a4640 Mon Sep 17 00:00:00 2001 From: Sorin Sbarnea Date: Wed, 23 Jun 2021 12:39:11 +0100 Subject: [PATCH] Avoid symlink infinite loops Related: https://github.com/pypa/setuptools/issues/332 Related: https://bugs.python.org/issue44497 --- distutils/filelist.py | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/distutils/filelist.py b/distutils/filelist.py index c92d5fdb..2c1862b4 100644 --- a/distutils/filelist.py +++ b/distutils/filelist.py @@ -247,12 +247,22 @@ def _find_all_simple(path): """ Find all files under 'path' """ - results = ( - os.path.join(base, file) - for base, dirs, files in os.walk(path, followlinks=True) - for file in files - ) - return filter(os.path.isfile, results) + # https://bugs.python.org/issue44497 + dirs = set() + files = set() + for dirpath, dirnames, filenames in os.walk(path, followlinks=True): + st = os.stat(dirpath) + scandirs = [] + for dirname in dirnames: + st = os.stat(os.path.join(dirpath, dirname)) + dirkey = st.st_dev, st.st_ino + if dirkey not in dirs: + dirs.add(dirkey) + scandirs.append(dirname) + dirnames[:] = scandirs + for f in filenames: + files.add(os.path.join(dirpath, f)) + return files def findall(dir=os.curdir):