@@ -87,7 +87,8 @@ def assert_POT_equal(self, expected, actual):
8787self.maxDiff=None
8888self.assertEqual(normalize_POT_file(expected), normalize_POT_file(actual))
898990-defextract_from_str(self, module_content, *, args=(), strict=True, with_stderr=False):
90+defextract_from_str(self, module_content, *, args=(), strict=True,
91+with_stderr=False, raw=False):
9192"""Return all msgids extracted from module_content."""
9293filename='test.py'
9394withtemp_cwd(None):
@@ -98,10 +99,11 @@ def extract_from_str(self, module_content, *, args=(), strict=True, with_stderr=
9899self.assertEqual(res.err, b'')
99100withopen('messages.pot', encoding='utf-8') asfp:
100101data=fp.read()
101-msgids=self.get_msgids(data)
102+ifnotraw:
103+data=self.get_msgids(data)
102104ifnotwith_stderr:
103-returnmsgids
104-returnmsgids, res.err
105+returndata
106+returndata, res.err
105107106108defextract_docstrings_from_str(self, module_content):
107109"""Return all docstrings extracted from module_content."""
@@ -381,7 +383,8 @@ def test_pygettext_output(self):
381383contents=input_file.read_text(encoding='utf-8')
382384withtemp_cwd(None):
383385Path(input_file.name).write_text(contents)
384-assert_python_ok('-Xutf8', self.script, '--docstrings', input_file.name)
386+assert_python_ok('-Xutf8', self.script, '--docstrings',
387+'--add-comments=i18n:', input_file.name)
385388output=Path('messages.pot').read_text(encoding='utf-8')
386389387390expected=output_file.read_text(encoding='utf-8')
@@ -437,14 +440,60 @@ def test_error_messages(self):
437440"*** test.py:3: Variable positional arguments are not allowed in gettext calls\n"
438441 )
439442443+deftest_extract_all_comments(self):
444+"""
445+ Test that the --add-comments option without an
446+ explicit tag extracts all translator comments.
447+ """
448+forargin ('--add-comments', '-c'):
449+withself.subTest(arg=arg):
450+data=self.extract_from_str(dedent('''\
451+ # Translator comment
452+ _("foo")
453+ '''), args=(arg,), raw=True)
454+self.assertIn('#. Translator comment', data)
455+456+deftest_comments_with_multiple_tags(self):
457+"""
458+ Test that multiple --add-comments tags can be specified.
459+ """
460+forargin ('--add-comments={}', '-c{}'):
461+withself.subTest(arg=arg):
462+args= (arg.format('foo:'), arg.format('bar:'))
463+data=self.extract_from_str(dedent('''\
464+ # foo: comment
465+ _("foo")
466+467+ # bar: comment
468+ _("bar")
469+470+ # baz: comment
471+ _("baz")
472+ '''), args=args, raw=True)
473+self.assertIn('#. foo: comment', data)
474+self.assertIn('#. bar: comment', data)
475+self.assertNotIn('#. baz: comment', data)
476+477+deftest_comments_not_extracted_without_tags(self):
478+"""
479+ Test that translator comments are not extracted without
480+ specifying --add-comments.
481+ """
482+data=self.extract_from_str(dedent('''\
483+ # Translator comment
484+ _("foo")
485+ '''), raw=True)
486+self.assertNotIn('#.', data)
487+440488441489defupdate_POT_snapshots():
442490forinput_fileinDATA_DIR.glob('*.py'):
443491output_file=input_file.with_suffix('.pot')
444492contents=input_file.read_bytes()
445493withtemp_cwd(None):
446494Path(input_file.name).write_bytes(contents)
447-assert_python_ok('-Xutf8', Test_pygettext.script, '--docstrings', input_file.name)
495+assert_python_ok('-Xutf8', Test_pygettext.script, '--docstrings',
496+'--add-comments=i18n:', input_file.name)
448497output=Path('messages.pot').read_text(encoding='utf-8')
449498450499output=normalize_POT_file(output)