gh-95731: Fix module docstring extraction in pygettext (#95732)

This commit is contained in:
Jakub Kuczys 2022-10-15 16:57:53 +02:00 committed by GitHub
parent 07b5c4699e
commit 120b4ab2b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 3 deletions

View File

@ -155,6 +155,26 @@ class Test_pygettext(unittest.TestCase):
''')) '''))
self.assertFalse([msgid for msgid in msgids if 'doc' in msgid]) self.assertFalse([msgid for msgid in msgids if 'doc' in msgid])
def test_moduledocstring(self):
for doc in ('"""doc"""', "r'''doc'''", "R'doc'", 'u"doc"'):
with self.subTest(doc):
msgids = self.extract_docstrings_from_str(dedent('''\
%s
''' % doc))
self.assertIn('doc', msgids)
def test_moduledocstring_bytes(self):
msgids = self.extract_docstrings_from_str(dedent('''\
b"""doc"""
'''))
self.assertFalse([msgid for msgid in msgids if 'doc' in msgid])
def test_moduledocstring_fstring(self):
msgids = self.extract_docstrings_from_str(dedent('''\
f"""doc"""
'''))
self.assertFalse([msgid for msgid in msgids if 'doc' in msgid])
def test_msgid(self): def test_msgid(self):
msgids = self.extract_docstrings_from_str( msgids = self.extract_docstrings_from_str(
'''_("""doc""" r'str' u"ing")''') '''_("""doc""" r'str' u"ing")''')

View File

@ -0,0 +1 @@
Fix handling of module docstrings in :file:`Tools/i18n/pygettext.py`.

View File

@ -335,9 +335,10 @@ class TokenEater:
if ttype == tokenize.STRING and is_literal_string(tstring): if ttype == tokenize.STRING and is_literal_string(tstring):
self.__addentry(safe_eval(tstring), lineno, isdocstring=1) self.__addentry(safe_eval(tstring), lineno, isdocstring=1)
self.__freshmodule = 0 self.__freshmodule = 0
elif ttype not in (tokenize.COMMENT, tokenize.NL): return
self.__freshmodule = 0 if ttype in (tokenize.COMMENT, tokenize.NL, tokenize.ENCODING):
return return
self.__freshmodule = 0
# class or func/method docstring? # class or func/method docstring?
if ttype == tokenize.NAME and tstring in ('class', 'def'): if ttype == tokenize.NAME and tstring in ('class', 'def'):
self.__state = self.__suiteseen self.__state = self.__suiteseen