]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-112205: Require @getter and @setter to be methods (#113278)
authorErlend E. Aasland <erlend@python.org>
Tue, 19 Dec 2023 11:32:28 +0000 (12:32 +0100)
committerGitHub <noreply@github.com>
Tue, 19 Dec 2023 11:32:28 +0000 (12:32 +0100)
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Lib/test/test_clinic.py
Tools/clinic/clinic.py

index d3dbde88dd82a9e82eb3517726d382e1e41380e1..6c6bd4e75a0b0282916bdc96094d43f7c5080362 100644 (file)
@@ -2249,6 +2249,17 @@ class ClinicParserTest(TestCase):
                 expected_error = "Cannot apply both @getter and @setter to the same function!"
                 self.expect_failure(block, expected_error, lineno=3)
 
+    def test_getset_no_class(self):
+        for annotation in "@getter", "@setter":
+            with self.subTest(annotation=annotation):
+                block = f"""
+                    module m
+                    {annotation}
+                    m.func
+                """
+                expected_error = "@getter and @setter must be methods"
+                self.expect_failure(block, expected_error, lineno=2)
+
     def test_duplicate_coexist(self):
         err = "Called @coexist twice"
         block = """
index a9bf110291eadd22ed6f552bb461c80cf7b1a04f..87feef1b82ca39a28dc56a3bc9c410d59af512fa 100755 (executable)
@@ -5614,6 +5614,10 @@ class DSLParser:
         function_name = fields.pop()
         module, cls = self.clinic._module_and_class(fields)
 
+        if self.kind in {GETTER, SETTER}:
+            if not cls:
+                fail("@getter and @setter must be methods")
+
         self.update_function_kind(full_name)
         if self.kind is METHOD_INIT and not return_converter:
             return_converter = init_return_converter()