[Python-checkins] [3.11] gh-95731: Fix module docstring extraction in pygettext (GH-95732) (#98281)

JelleZijlstra webhook-mailer at python.org
Sun Oct 16 00:15:23 EDT 2022


https://github.com/python/cpython/commit/b5874fae0a618e4b0815a54242b0703bd92482be
commit: b5874fae0a618e4b0815a54242b0703bd92482be
branch: 3.11
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: JelleZijlstra <jelle.zijlstra at gmail.com>
date: 2022-10-15T21:15:17-07:00
summary:

[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 120b4ab2b68aebf96ce0de243eab89a25fc2d282)

Co-authored-by: Jakub Kuczys <me at jacken.men>

files:
A Misc/NEWS.d/next/Tools-Demos/2022-08-05-23-25-59.gh-issue-95731.N2KohU.rst
M Lib/test/test_tools/test_i18n.py
M Tools/i18n/pygettext.py

diff --git a/Lib/test/test_tools/test_i18n.py b/Lib/test/test_tools/test_i18n.py
index 7f18edaaa8ca..c083a04475e7 100644
--- a/Lib/test/test_tools/test_i18n.py
+++ b/Lib/test/test_tools/test_i18n.py
@@ -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")''')
diff --git a/Misc/NEWS.d/next/Tools-Demos/2022-08-05-23-25-59.gh-issue-95731.N2KohU.rst b/Misc/NEWS.d/next/Tools-Demos/2022-08-05-23-25-59.gh-issue-95731.N2KohU.rst
new file mode 100644
index 000000000000..6b214616c0a9
--- /dev/null
+++ b/Misc/NEWS.d/next/Tools-Demos/2022-08-05-23-25-59.gh-issue-95731.N2KohU.rst
@@ -0,0 +1 @@
+Fix handling of module docstrings in :file:`Tools/i18n/pygettext.py`.
diff --git a/Tools/i18n/pygettext.py b/Tools/i18n/pygettext.py
index 6f889adffe6c..7ada79105db1 100755
--- a/Tools/i18n/pygettext.py
+++ b/Tools/i18n/pygettext.py
@@ -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



More information about the Python-checkins mailing list