Skip to content

Commit 9b25bd6

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

File tree

4 files changed

+39
-20
lines changed

4 files changed

+39
-20
lines changed

Lib/test/test_tools/test_i18n.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from test.support.script_helper import assert_python_ok
99
from test.test_tools import skip_if_missing, toolsdir
10-
from test.support import temp_cwd
10+
from test.support import temp_cwd, temp_dir
1111

1212

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

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -851,6 +851,7 @@ Maksim Kozyarchuk
851851
Stefan Krah
852852
Rolf Krahl
853853
Bob Kras
854+
Oleg Krasnikov
854855
Sebastian Kreft
855856
Holger Krekel
856857
Michael Kremer
Lines changed: 2 additions & 0 deletions
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

Lines changed: 11 additions & 19 deletions
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)