Skip to content

Commit

Permalink
[3.13] gh-121863: Immortalize names in code objects to avoid crash (G…
Browse files Browse the repository at this point in the history
…H-121903) (GH-121904)

(cherry picked from commit cffad5c)

Co-authored-by: Petr Viktorin <[email protected]>
  • Loading branch information
miss-islington and encukou authored Jul 17, 2024
1 parent 09ff4ec commit 72cd53e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
24 changes: 24 additions & 0 deletions Lib/test/test_scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
2 changes: 1 addition & 1 deletion Objects/codeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 72cd53e

Please sign in to comment.