]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-107559: Argument Clinic: complain about non-ASCII chars in param docstrings (...
authorErlend E. Aasland <erlend@python.org>
Wed, 2 Aug 2023 12:40:23 +0000 (14:40 +0200)
committerGitHub <noreply@github.com>
Wed, 2 Aug 2023 12:40:23 +0000 (12:40 +0000)
Previously, only function docstrings were checked for non-ASCII characters.
Also, improve the warn() message.

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Lib/test/test_clinic.py
Tools/clinic/clinic.py

index 6bdc571dd4d5ac460d6f39818a6d8d2221fd0450..6f53036366891feab63f0f191711cf638b38c61f 100644 (file)
@@ -1427,6 +1427,25 @@ Couldn't find existing function 'fooooooooooooooooooooooo'!
         actual = stdout.getvalue()
         self.assertEqual(actual, expected)
 
+    def test_non_ascii_character_in_docstring(self):
+        block = """
+            module test
+            test.fn
+                a: int
+                    á param docstring
+            docstring fü bár baß
+        """
+        with support.captured_stdout() as stdout:
+            self.parse(block)
+        # The line numbers are off; this is a known limitation.
+        expected = dedent("""\
+            Warning on line 0:
+            Non-ascii characters are not allowed in docstrings: 'á'
+            Warning on line 0:
+            Non-ascii characters are not allowed in docstrings: 'ü', 'á', 'ß'
+        """)
+        self.assertEqual(stdout.getvalue(), expected)
+
 
 class ClinicExternalTest(TestCase):
     maxDiff = None
index 5f7d41e4415515aa7a8fe230410d22873d3c9521..1f461665003c8155007ac6effa66349b786cb739 100755 (executable)
@@ -785,9 +785,6 @@ class CLanguage(Language):
             self,
             f: Function
     ) -> str:
-        if re.search(r'[^\x00-\x7F]', f.docstring):
-            warn("Non-ascii character appear in docstring.")
-
         text, add, output = _text_accumulator()
         # turn docstring into a properly quoted C string
         for line in f.docstring.split('\n'):
@@ -5266,6 +5263,11 @@ class DSLParser:
 
     def docstring_append(self, obj: Function | Parameter, line: str) -> None:
         """Add a rstripped line to the current docstring."""
+        matches = re.finditer(r'[^\x00-\x7F]', line)
+        if offending := ", ".join([repr(m[0]) for m in matches]):
+            warn("Non-ascii characters are not allowed in docstrings:",
+                 offending)
+
         docstring = obj.docstring
         if docstring:
             docstring += "\n"