diff -r fa3ac31cfa44 -r faf37fc3b097 Lib/distutils/filelist.py --- a/Lib/distutils/filelist.py Sun Aug 30 09:13:48 2015 -0700 +++ b/Lib/distutils/filelist.py Sun Aug 30 14:05:58 2015 -0400 @@ -6,6 +6,7 @@ import os, re import fnmatch +import functools from distutils.util import convert_path from distutils.errors import DistutilsTemplateError, DistutilsInternalError from distutils import log @@ -246,31 +247,15 @@ """Find all files under 'dir' and return the list of full filenames (relative to 'dir'). """ - from stat import ST_MODE, S_ISREG, S_ISDIR, S_ISLNK + def _prepend(base): + return functools.partial(os.path.join, os.path.relpath(base, dir)) - list = [] - stack = [dir] - pop = stack.pop - push = stack.append - - while stack: - dir = pop() - names = os.listdir(dir) - - for name in names: - if dir != os.curdir: # avoid the dreaded "./" syndrome - fullname = os.path.join(dir, name) - else: - fullname = name - - # Avoid excess stat calls -- just one will do, thank you! - stat = os.stat(fullname) - mode = stat[ST_MODE] - if S_ISREG(mode): - list.append(fullname) - elif S_ISDIR(mode) and not S_ISLNK(mode): - push(fullname) - return list + return [ + file + for base, dirs, files in os.walk(dir, followlinks=True) + for file in map(_prepend(base), files) + if os.path.isfile(file) + ] def glob_to_re(pattern): diff -r fa3ac31cfa44 -r faf37fc3b097 Lib/distutils/tests/test_filelist.py --- a/Lib/distutils/tests/test_filelist.py Sun Aug 30 09:13:48 2015 -0700 +++ b/Lib/distutils/tests/test_filelist.py Sun Aug 30 14:05:58 2015 -0400 @@ -6,8 +6,10 @@ from distutils.log import WARN from distutils.errors import DistutilsTemplateError from distutils.filelist import glob_to_re, translate_pattern, FileList +from distutils import filelist -from test.support import captured_stdout, run_unittest +import test.support +from test.support import captured_stdout from distutils.tests import support MANIFEST_IN = """\ @@ -292,8 +294,24 @@ self.assertWarnings() -def test_suite(): - return unittest.makeSuite(FileListTestCase) +class FindAllTestCase(unittest.TestCase): + @test.support.skip_unless_symlink + def test_missing_symlink(self): + with test.support.temp_cwd(): + os.symlink('foo', 'bar') + self.assertEqual(filelist.findall(), []) + + def test_basic_discovery(self): + with test.support.temp_cwd(): + os.mkdir('foo') + file1 = os.path.join('foo', 'file1.txt') + test.support.create_empty_file(file1) + os.mkdir('bar') + file2 = os.path.join('bar', 'file2.txt') + test.support.create_empty_file(file2) + expected = [file2, file1] + self.assertEqual(sorted(filelist.findall()), expected) + if __name__ == "__main__": - run_unittest(test_suite()) + unittest.main()