https://github.com/python/cpython/commit/72cd53ea15c6b304b826fe2ec69fa5afb1d3664e
commit: 72cd53ea15c6b304b826fe2ec69fa5afb1d3664e
branch: 3.13
author: Miss Islington (bot) <[email protected]>
committer: encukou <[email protected]>
date: 2024-07-17T09:55:22Z
summary:

[3.13] gh-121863: Immortalize names in code objects to avoid crash (GH-121903) 
(GH-121904)

(cherry picked from commit cffad5c6ef9371b26e32556296cea2bfe8358b1a)

Co-authored-by: Petr Viktorin <[email protected]>

files:
M Lib/test/test_scope.py
M Objects/codeobject.c

diff --git a/Lib/test/test_scope.py b/Lib/test/test_scope.py
index 6e46dfa96a664f..24a366efc6ca05 100644
--- a/Lib/test/test_scope.py
+++ b/Lib/test/test_scope.py
@@ -810,6 +810,30 @@ def dig(self):
         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()
diff --git a/Objects/codeobject.c b/Objects/codeobject.c
index 7b1244a8d5fd21..fbc1439d30c27a 100644
--- a/Objects/codeobject.c
+++ b/Objects/codeobject.c
@@ -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;
 }

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to