Skip to content

Commit

Permalink
[3.11] pythongh-95731: Fix module docstring extraction in pygettext (p…
Browse files Browse the repository at this point in the history
…ythonGH-95732) (python#98281)

pythongh-95731: Fix module docstring extraction in pygettext (pythonGH-95732)
(cherry picked from commit 120b4ab)

Co-authored-by: Jakub Kuczys <me@jacken.men>
  • Loading branch information
miss-islington and Jackenmen authored Oct 16, 2022
1 parent a2ae35d commit b5874fa
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
20 changes: 20 additions & 0 deletions Lib/test/test_tools/test_i18n.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,26 @@ class C:
'''))
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):
msgids = self.extract_docstrings_from_str(
'''_("""doc""" r'str' u"ing")''')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix handling of module docstrings in :file:`Tools/i18n/pygettext.py`.
7 changes: 4 additions & 3 deletions Tools/i18n/pygettext.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,9 +335,10 @@ def __waiting(self, ttype, tstring, lineno):
if ttype == tokenize.STRING and is_literal_string(tstring):
self.__addentry(safe_eval(tstring), lineno, isdocstring=1)
self.__freshmodule = 0
elif ttype not in (tokenize.COMMENT, tokenize.NL):
self.__freshmodule = 0
return
return
if ttype in (tokenize.COMMENT, tokenize.NL, tokenize.ENCODING):
return
self.__freshmodule = 0
# class or func/method docstring?
if ttype == tokenize.NAME and tstring in ('class', 'def'):
self.__state = self.__suiteseen
Expand Down

0 comments on commit b5874fa

Please sign in to comment.