Skip to content

Commit b5874fa

Browse files
[3.11] gh-95731: Fix module docstring extraction in pygettext (GH-95732) (#98281)
gh-95731: Fix module docstring extraction in pygettext (GH-95732) (cherry picked from commit 120b4ab) Co-authored-by: Jakub Kuczys <me@jacken.men>
1 parent a2ae35d commit b5874fa

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

Lib/test/test_tools/test_i18n.py

+20
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,26 @@ class C:
155155
'''))
156156
self.assertFalse([msgid for msgid in msgids if 'doc' in msgid])
157157

158+
def test_moduledocstring(self):
159+
for doc in ('"""doc"""', "r'''doc'''", "R'doc'", 'u"doc"'):
160+
with self.subTest(doc):
161+
msgids = self.extract_docstrings_from_str(dedent('''\
162+
%s
163+
''' % doc))
164+
self.assertIn('doc', msgids)
165+
166+
def test_moduledocstring_bytes(self):
167+
msgids = self.extract_docstrings_from_str(dedent('''\
168+
b"""doc"""
169+
'''))
170+
self.assertFalse([msgid for msgid in msgids if 'doc' in msgid])
171+
172+
def test_moduledocstring_fstring(self):
173+
msgids = self.extract_docstrings_from_str(dedent('''\
174+
f"""doc"""
175+
'''))
176+
self.assertFalse([msgid for msgid in msgids if 'doc' in msgid])
177+
158178
def test_msgid(self):
159179
msgids = self.extract_docstrings_from_str(
160180
'''_("""doc""" r'str' u"ing")''')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix handling of module docstrings in :file:`Tools/i18n/pygettext.py`.

Tools/i18n/pygettext.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -335,9 +335,10 @@ def __waiting(self, ttype, tstring, lineno):
335335
if ttype == tokenize.STRING and is_literal_string(tstring):
336336
self.__addentry(safe_eval(tstring), lineno, isdocstring=1)
337337
self.__freshmodule = 0
338-
elif ttype not in (tokenize.COMMENT, tokenize.NL):
339-
self.__freshmodule = 0
340-
return
338+
return
339+
if ttype in (tokenize.COMMENT, tokenize.NL, tokenize.ENCODING):
340+
return
341+
self.__freshmodule = 0
341342
# class or func/method docstring?
342343
if ttype == tokenize.NAME and tstring in ('class', 'def'):
343344
self.__state = self.__suiteseen

0 commit comments

Comments
 (0)