Skip to content

gh-121954: Don't mutate tuples in _PyCode_New #122180

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 20 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Include/internal/pycore_global_objects_fini_generated.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Include/internal/pycore_global_strings.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ struct _Py_global_strings {
STRUCT_FOR_STR(dbl_percent, "%%")
STRUCT_FOR_STR(defaults, ".defaults")
STRUCT_FOR_STR(dot_locals, ".<locals>")
STRUCT_FOR_STR(dot_zero, ".0")
STRUCT_FOR_STR(empty, "")
STRUCT_FOR_STR(format, ".format")
STRUCT_FOR_STR(generic_base, ".generic_base")
Expand Down
1 change: 1 addition & 0 deletions Include/internal/pycore_runtime_init_generated.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Include/internal/pycore_unicodeobject_generated.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 47 additions & 7 deletions Lib/test/test_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,53 @@ def test_code_equal_with_instrumentation(self):
self.assertNotEqual(code1, code2)
sys.settrace(None)

@cpython_only
def test_names_strict_string(self):
class StrSubclass(str): pass

def f():
return name1 + name2

code1 = f.__code__
code2 = code1.replace(co_names=tuple(
StrSubclass(name) for name in code1.co_names
))
self.assertEqual(code1.co_names, ('name1' ,'name2'))
self.assertEqual(code2.co_names, ('name1' ,'name2'))
for name in code2.co_names:
with self.subTest(name=name):
self.assertIs(type(name), str)

@cpython_only
def test_varnames_strict_string(self):
class StrSubclass(str): pass

def f():
name1 = name2 = call()
return name1 + name2

code1 = f.__code__
code2 = code1.replace(co_varnames=tuple(
StrSubclass(name) for name in code1.co_varnames
))
self.assertEqual(code1.co_varnames, ('name1' ,'name2'))
self.assertEqual(code2.co_varnames, ('name1' ,'name2'))
for name in code2.co_varnames:
with self.subTest(name=name):
self.assertIs(type(name), str)

@cpython_only
def test_names_nonstring(self):
def f():
var1, var2 = name1, name2
return var1 + var2

code = f.__code__

with self.assertRaises(TypeError):
code.replace(co_names=(1, 2))
with self.assertRaises(TypeError):
code.replace(co_varnames=(1, 2))

def isinterned(s):
return s is sys.intern(('_' + s + '_')[1:-1])
Expand Down Expand Up @@ -570,13 +617,6 @@ def f(a='str_value'):
return a
self.assertIsInterned(f())

@cpython_only
@unittest.skipIf(Py_GIL_DISABLED, "free-threaded build interns all string constants")
def test_interned_string_with_null(self):
co = compile(r'res = "str\0value!"', '?', 'exec')
v = self.find_const(co.co_consts, 'str\0value!')
self.assertIsNotInterned(v)

@cpython_only
@unittest.skipUnless(Py_GIL_DISABLED, "does not intern all constants")
@skip_if_suppress_immortalization()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fixed issue where creating a :class:`code <types.CodeType>` object would
sometimes mutate argument tuples. This changes details of how code constants
and names are interned, immortalized and de-duplicated.
Loading
Loading