]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.13] gh-121863: Immortalize names in code objects to avoid crash (GH-121903) (GH...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Wed, 17 Jul 2024 09:55:22 +0000 (11:55 +0200)
committerGitHub <noreply@github.com>
Wed, 17 Jul 2024 09:55:22 +0000 (09:55 +0000)
(cherry picked from commit cffad5c6ef9371b26e32556296cea2bfe8358b1a)

Co-authored-by: Petr Viktorin <encukou@gmail.com>
Lib/test/test_scope.py
Objects/codeobject.c

index 6e46dfa96a664f20a9f50153123ee1bad958d0b3..24a366efc6ca05d942f7b68b5139e76b0f3efe70 100644 (file)
@@ -810,6 +810,30 @@ class ScopeTests(unittest.TestCase):
         gc_collect()  # For PyPy or other GCs.
         self.assertIsNone(ref())
 
+    def test_multiple_nesting(self):
+        # Regression test for https://github.com/python/cpython/issues/121863
+        class MultiplyNested:
+            def f1(self):
+                __arg = 1
+                class D:
+                    def g(self, __arg):
+                        return __arg
+                return D().g(_MultiplyNested__arg=2)
+
+            def f2(self):
+                __arg = 1
+                class D:
+                    def g(self, __arg):
+                        return __arg
+                return D().g
+
+        inst = MultiplyNested()
+        with self.assertRaises(TypeError):
+            inst.f1()
+
+        closure = inst.f2()
+        with self.assertRaises(TypeError):
+            closure(_MultiplyNested__arg=2)
 
 if __name__ == '__main__':
     unittest.main()
index 7b1244a8d5fd2150cfeda9048c4263c323346afd..fbc1439d30c27a2c054fade94c1b02cbeddd2a07 100644 (file)
@@ -147,7 +147,7 @@ intern_strings(PyObject *tuple)
                             "non-string found in code slot");
             return -1;
         }
-        _PyUnicode_InternMortal(interp, &_PyTuple_ITEMS(tuple)[i]);
+        _PyUnicode_InternImmortal(interp, &_PyTuple_ITEMS(tuple)[i]);
     }
     return 0;
 }