Skip to content

Commit 75fbf87

Browse files
committed
Issue python#12885: Merge with 3.5
2 parents 5c38cb2 + 65e3319 commit 75fbf87

3 files changed

Lines changed: 61 additions & 31 deletions

File tree

‎Lib/distutils/filelist.py‎

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import os, re
88
import fnmatch
9+
import functools
910
from distutils.util import convert_path
1011
from distutils.errors import DistutilsTemplateError, DistutilsInternalError
1112
from distutils import log
@@ -242,35 +243,28 @@ def exclude_pattern (self, pattern,
242243
# ----------------------------------------------------------------------
243244
# Utility functions
244245

246+
def _find_all_simple(path):
247+
"""
248+
Find all files under 'path'
249+
"""
250+
results = (
251+
os.path.join(base, file)
252+
for base, dirs, files in os.walk(path, followlinks=True)
253+
for file in files
254+
)
255+
return filter(os.path.isfile, results)
256+
257+
245258
def findall(dir=os.curdir):
246-
"""Find all files under 'dir' and return the list of full filenames
247-
(relative to 'dir').
248259
"""
249-
from stat import ST_MODE, S_ISREG, S_ISDIR, S_ISLNK
250-
251-
list = []
252-
stack = [dir]
253-
pop = stack.pop
254-
push = stack.append
255-
256-
while stack:
257-
dir = pop()
258-
names = os.listdir(dir)
259-
260-
for name in names:
261-
if dir != os.curdir: # avoid the dreaded "./" syndrome
262-
fullname = os.path.join(dir, name)
263-
else:
264-
fullname = name
265-
266-
# Avoid excess stat calls -- just one will do, thank you!
267-
stat = os.stat(fullname)
268-
mode = stat[ST_MODE]
269-
if S_ISREG(mode):
270-
list.append(fullname)
271-
elif S_ISDIR(mode) and not S_ISLNK(mode):
272-
push(fullname)
273-
return list
260+
Find all files under 'dir' and return the list of full filenames.
261+
Unless dir is '.', return full filenames with dir prepended.
262+
"""
263+
files = _find_all_simple(dir)
264+
if dir == os.curdir:
265+
make_rel = functools.partial(os.path.relpath, start=dir)
266+
files = map(make_rel, files)
267+
return list(files)
274268

275269

276270
def glob_to_re(pattern):

‎Lib/distutils/tests/test_filelist.py‎

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
from distutils.log import WARN
77
from distutils.errors import DistutilsTemplateError
88
from distutils.filelist import glob_to_re, translate_pattern, FileList
9+
from distutils import filelist
910

10-
from test.support import captured_stdout, run_unittest
11+
import test.support
12+
from test.support import captured_stdout
1113
from distutils.tests import support
1214

1315
MANIFEST_IN = """\
@@ -292,8 +294,40 @@ def test_process_template(self):
292294
self.assertWarnings()
293295

294296

295-
def test_suite():
296-
return unittest.makeSuite(FileListTestCase)
297+
class FindAllTestCase(unittest.TestCase):
298+
@test.support.skip_unless_symlink
299+
def test_missing_symlink(self):
300+
with test.support.temp_cwd():
301+
os.symlink('foo', 'bar')
302+
self.assertEqual(filelist.findall(), [])
303+
304+
def test_basic_discovery(self):
305+
"""
306+
When findall is called with no parameters or with
307+
'.' as the parameter, the dot should be omitted from
308+
the results.
309+
"""
310+
with test.support.temp_cwd():
311+
os.mkdir('foo')
312+
file1 = os.path.join('foo', 'file1.txt')
313+
test.support.create_empty_file(file1)
314+
os.mkdir('bar')
315+
file2 = os.path.join('bar', 'file2.txt')
316+
test.support.create_empty_file(file2)
317+
expected = [file2, file1]
318+
self.assertEqual(sorted(filelist.findall()), expected)
319+
320+
def test_non_local_discovery(self):
321+
"""
322+
When findall is called with another path, the full
323+
path name should be returned.
324+
"""
325+
with test.support.temp_dir() as temp_dir:
326+
file1 = os.path.join(temp_dir, 'file1.txt')
327+
test.support.create_empty_file(file1)
328+
expected = [file1]
329+
self.assertEqual(filelist.findall(temp_dir), expected)
330+
297331

298332
if __name__ == "__main__":
299-
run_unittest(test_suite())
333+
unittest.main()

‎Misc/NEWS‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ Library
7070
- Issue #27842: The csv.DictReader now returns rows of type OrderedDict.
7171
(Contributed by Steve Holden.)
7272

73+
- Issue #12285: Fix error when distutils encounters symlink.
74+
7375
- Issue #27881: Fixed possible bugs when setting sqlite3.Connection.isolation_level.
7476
Based on patch by Xiang Zhang.
7577

0 commit comments

Comments
 (0)