From b15fd6a01891271b9bb2398e493ce664cee052be Mon Sep 17 00:00:00 2001 From: cetus Date: Thu, 2 Nov 2017 10:39:18 +0200 Subject: [PATCH 1/2] Fix pygettext directory walk (#31920) --- Tools/i18n/pygettext.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Tools/i18n/pygettext.py b/Tools/i18n/pygettext.py index 8ef5ff8a3e72c2..dfbfb3fc9a496f 100755 --- a/Tools/i18n/pygettext.py +++ b/Tools/i18n/pygettext.py @@ -302,7 +302,8 @@ def getFilesForName(name): if os.path.isdir(name): # find all python files in directory list = [] - os.walk(name, _visit_pyfiles, list) + for root, dirs, files in os.walk(name): + _visit_pyfiles(list, root, dirs + files) return list elif os.path.exists(name): # a single file From 5c34353e4c1ebf5da720351765548c77c8bb7a94 Mon Sep 17 00:00:00 2001 From: cetus Date: Fri, 3 Nov 2017 17:24:50 +0200 Subject: [PATCH 2/2] Split dirs and files operations for pygettext directory walk, add regression test --- Lib/test/test_tools/test_i18n.py | 20 +++++++++++++++++++- Tools/i18n/pygettext.py | 29 ++++++++++------------------- 2 files changed, 29 insertions(+), 20 deletions(-) diff --git a/Lib/test/test_tools/test_i18n.py b/Lib/test/test_tools/test_i18n.py index 5c28baea7241c2..931e83541761fd 100644 --- a/Lib/test/test_tools/test_i18n.py +++ b/Lib/test/test_tools/test_i18n.py @@ -1,12 +1,13 @@ """Tests to cover the Tools/i18n package""" import os +import re import sys import unittest from test.support.script_helper import assert_python_ok from test.test_tools import skip_if_missing, toolsdir -from test.support import temp_cwd +from test.support import temp_cwd, temp_dir skip_if_missing() @@ -72,3 +73,20 @@ def test_POT_Creation_Date(self): # This will raise if the date format does not exactly match. datetime.strptime(creationDate, '%Y-%m-%d %H:%M%z') + + def test_files_list(self): + """Make sure the directories are inspected for source files + bpo-31920 + """ + source_dir = 'pypkg' + text = 'Text to translate' + with temp_cwd(None) as cwd: + with temp_dir(os.path.join(cwd, source_dir)) as sdir: + with open(os.path.join(sdir, 'pymod.py'), 'w') as sfile: + sfile.write('_("{}")'.format(text)) + assert_python_ok(self.script, source_dir) + with open('messages.pot') as fp: + data = fp.read() + msgids = re.findall(r'msgid "(.*?)"', data) + + self.assertIn(text, msgids) diff --git a/Tools/i18n/pygettext.py b/Tools/i18n/pygettext.py index dfbfb3fc9a496f..648b189b52d110 100755 --- a/Tools/i18n/pygettext.py +++ b/Tools/i18n/pygettext.py @@ -259,24 +259,6 @@ def containsAny(str, set): return 1 in [c in str for c in set] -def _visit_pyfiles(list, dirname, names): - """Helper for getFilesForName().""" - # get extension for python source files - if '_py_ext' not in globals(): - global _py_ext - _py_ext = importlib.machinery.SOURCE_SUFFIXES[0] - - # don't recurse into CVS directories - if 'CVS' in names: - names.remove('CVS') - - # add all *.py files to list - list.extend( - [os.path.join(dirname, file) for file in names - if os.path.splitext(file)[1] == _py_ext] - ) - - def getFilesForName(name): """Get a list of module files for a filename, a module or package name, or a directory. @@ -302,8 +284,17 @@ def getFilesForName(name): if os.path.isdir(name): # find all python files in directory list = [] + # get extension for python source files + _py_ext = importlib.machinery.SOURCE_SUFFIXES[0] for root, dirs, files in os.walk(name): - _visit_pyfiles(list, root, dirs + files) + # don't recurse into CVS directories + if 'CVS' in dirs: + dirs.remove('CVS') + # add all *.py files to list + list.extend( + [os.path.join(root, file) for file in files + if os.path.splitext(file)[1] == _py_ext] + ) return list elif os.path.exists(name): # a single file