Skip to content

Commit feb1145

Browse files
committed
Make --add-comments optional
1 parent 396a7be commit feb1145

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

Lib/test/test_tools/test_i18n.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ def assert_POT_equal(self, expected, actual):
8787
self.maxDiff = None
8888
self.assertEqual(normalize_POT_file(expected), normalize_POT_file(actual))
8989

90-
def extract_from_str(self, module_content, *, args=(), strict=True, with_stderr=False):
90+
def extract_from_str(self, module_content, *, args=(), strict=True,
91+
with_stderr=False, raw=False):
9192
"""Return all msgids extracted from module_content."""
9293
filename = 'test.py'
9394
with temp_cwd(None):
@@ -98,10 +99,11 @@ def extract_from_str(self, module_content, *, args=(), strict=True, with_stderr=
9899
self.assertEqual(res.err, b'')
99100
with open('messages.pot', encoding='utf-8') as fp:
100101
data = fp.read()
101-
msgids = self.get_msgids(data)
102+
if not raw:
103+
data = self.get_msgids(data)
102104
if not with_stderr:
103-
return msgids
104-
return msgids, res.err
105+
return data
106+
return data, res.err
105107

106108
def extract_docstrings_from_str(self, module_content):
107109
"""Return all docstrings extracted from module_content."""
@@ -432,6 +434,20 @@ def test_error_messages(self):
432434
"*** test.py:3: Variable positional arguments are not allowed in gettext calls\n"
433435
)
434436

437+
def test_extract_all_comments(self):
438+
"""
439+
Test that the --add-comments option without an
440+
explicit tag extracts all translator comments.
441+
"""
442+
443+
for arg in ('--add-comments', '-c'):
444+
with self.subTest(arg=arg):
445+
data = self.extract_from_str(dedent('''\
446+
# Translator comment
447+
_("foo")
448+
'''), args=(arg,), raw=True)
449+
self.assertIn('#. Translator comment', data)
450+
435451

436452
def update_POT_snapshots():
437453
for input_file in DATA_DIR.glob('*.py'):

Tools/i18n/pygettext.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -547,8 +547,8 @@ def main():
547547
try:
548548
opts, args = getopt.getopt(
549549
sys.argv[1:],
550-
'ac:d:DEhk:Kno:p:S:Vvw:x:X:',
551-
['extract-all', 'add-comments=', 'default-domain=', 'escape',
550+
'ac::d:DEhk:Kno:p:S:Vvw:x:X:',
551+
['extract-all', 'add-comments=?', 'default-domain=', 'escape',
552552
'help', 'keyword=', 'no-default-keywords',
553553
'add-location', 'no-location', 'output=', 'output-dir=',
554554
'style=', 'verbose', 'version', 'width=', 'exclude-file=',

0 commit comments

Comments
 (0)