Skip to content

Commit 72cd53e

Browse files
[3.13] gh-121863: Immortalize names in code objects to avoid crash (GH-121903) (GH-121904)
(cherry picked from commit cffad5c) Co-authored-by: Petr Viktorin <[email protected]>
1 parent 09ff4ec commit 72cd53e

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

Lib/test/test_scope.py

+24
Original file line numberDiff line numberDiff line change
@@ -810,6 +810,30 @@ def dig(self):
810810
gc_collect() # For PyPy or other GCs.
811811
self.assertIsNone(ref())
812812

813+
def test_multiple_nesting(self):
814+
# Regression test for https://github.com/python/cpython/issues/121863
815+
class MultiplyNested:
816+
def f1(self):
817+
__arg = 1
818+
class D:
819+
def g(self, __arg):
820+
return __arg
821+
return D().g(_MultiplyNested__arg=2)
822+
823+
def f2(self):
824+
__arg = 1
825+
class D:
826+
def g(self, __arg):
827+
return __arg
828+
return D().g
829+
830+
inst = MultiplyNested()
831+
with self.assertRaises(TypeError):
832+
inst.f1()
833+
834+
closure = inst.f2()
835+
with self.assertRaises(TypeError):
836+
closure(_MultiplyNested__arg=2)
813837

814838
if __name__ == '__main__':
815839
unittest.main()

Objects/codeobject.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ intern_strings(PyObject *tuple)
147147
"non-string found in code slot");
148148
return -1;
149149
}
150-
_PyUnicode_InternMortal(interp, &_PyTuple_ITEMS(tuple)[i]);
150+
_PyUnicode_InternImmortal(interp, &_PyTuple_ITEMS(tuple)[i]);
151151
}
152152
return 0;
153153
}

0 commit comments

Comments
 (0)