Skip to content

Commit e0dbc57

Browse files
bpo-31920: Fixed handling directories as arguments in the pygettext script. (pythonGH-6259)
Based on patch by Oleg Krasnikov. (cherry picked from commit c93938b) Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent 2d01ac5 commit e0dbc57

File tree

4 files changed

+39
-20
lines changed

4 files changed

+39
-20
lines changed

Lib/test/test_tools/test_i18n.py

+25-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from test.support.script_helper import assert_python_ok
88
from test.test_tools import skip_if_missing, toolsdir
9-
from test.support import temp_cwd
9+
from test.support import temp_cwd, temp_dir
1010

1111

1212
skip_if_missing()
@@ -158,3 +158,27 @@ class D(L[1:2], F({1: 2}), metaclass=M(lambda x: x)):
158158
"""doc"""
159159
'''))
160160
self.assertIn('doc', msgids)
161+
162+
def test_files_list(self):
163+
"""Make sure the directories are inspected for source files
164+
bpo-31920
165+
"""
166+
text1 = 'Text to translate1'
167+
text2 = 'Text to translate2'
168+
text3 = 'Text to ignore'
169+
with temp_cwd(None), temp_dir(None) as sdir:
170+
os.mkdir(os.path.join(sdir, 'pypkg'))
171+
with open(os.path.join(sdir, 'pypkg', 'pymod.py'), 'w') as sfile:
172+
sfile.write(f'_({text1!r})')
173+
os.mkdir(os.path.join(sdir, 'pkg.py'))
174+
with open(os.path.join(sdir, 'pkg.py', 'pymod2.py'), 'w') as sfile:
175+
sfile.write(f'_({text2!r})')
176+
os.mkdir(os.path.join(sdir, 'CVS'))
177+
with open(os.path.join(sdir, 'CVS', 'pymod3.py'), 'w') as sfile:
178+
sfile.write(f'_({text3!r})')
179+
assert_python_ok(self.script, sdir)
180+
with open('messages.pot') as fp:
181+
data = fp.read()
182+
self.assertIn(f'msgid "{text1}"', data)
183+
self.assertIn(f'msgid "{text2}"', data)
184+
self.assertNotIn(text3, data)

Misc/ACKS

+1
Original file line numberDiff line numberDiff line change
@@ -835,6 +835,7 @@ Maksim Kozyarchuk
835835
Stefan Krah
836836
Rolf Krahl
837837
Bob Kras
838+
Oleg Krasnikov
838839
Sebastian Kreft
839840
Holger Krekel
840841
Michael Kremer
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fixed handling directories as arguments in the ``pygettext`` script. Based
2+
on patch by Oleg Krasnikov.

Tools/i18n/pygettext.py

+11-19
Original file line numberDiff line numberDiff line change
@@ -259,24 +259,6 @@ def containsAny(str, set):
259259
return 1 in [c in str for c in set]
260260

261261

262-
def _visit_pyfiles(list, dirname, names):
263-
"""Helper for getFilesForName()."""
264-
# get extension for python source files
265-
if '_py_ext' not in globals():
266-
global _py_ext
267-
_py_ext = importlib.machinery.SOURCE_SUFFIXES[0]
268-
269-
# don't recurse into CVS directories
270-
if 'CVS' in names:
271-
names.remove('CVS')
272-
273-
# add all *.py files to list
274-
list.extend(
275-
[os.path.join(dirname, file) for file in names
276-
if os.path.splitext(file)[1] == _py_ext]
277-
)
278-
279-
280262
def getFilesForName(name):
281263
"""Get a list of module files for a filename, a module or package name,
282264
or a directory.
@@ -302,7 +284,17 @@ def getFilesForName(name):
302284
if os.path.isdir(name):
303285
# find all python files in directory
304286
list = []
305-
os.walk(name, _visit_pyfiles, list)
287+
# get extension for python source files
288+
_py_ext = importlib.machinery.SOURCE_SUFFIXES[0]
289+
for root, dirs, files in os.walk(name):
290+
# don't recurse into CVS directories
291+
if 'CVS' in dirs:
292+
dirs.remove('CVS')
293+
# add all *.py files to list
294+
list.extend(
295+
[os.path.join(root, file) for file in files
296+
if os.path.splitext(file)[1] == _py_ext]
297+
)
306298
return list
307299
elif os.path.exists(name):
308300
# a single file

0 commit comments

Comments
 (0)