mirror of
https://github.com/python/cpython.git
synced 2025-11-24 20:30:18 +00:00
gh-132775: Add _PyCode_VerifyStateless() (gh-133221)
"Stateless" code is a function or code object which does not rely on external state or internal state. It may rely on arguments and builtins, but not globals or a closure. I've left a comment in pycore_code.h that provides more detail. We also add _PyFunction_VerifyStateless(). The new functions will be used in several later changes that facilitate "sharing" functions and code objects between interpreters.
This commit is contained in:
parent
f610bbdf74
commit
d270bb5792
8 changed files with 442 additions and 38 deletions
|
|
@ -220,6 +220,7 @@ try:
|
|||
import _testinternalcapi
|
||||
except ModuleNotFoundError:
|
||||
_testinternalcapi = None
|
||||
import test._code_definitions as defs
|
||||
|
||||
COPY_FREE_VARS = opmap['COPY_FREE_VARS']
|
||||
|
||||
|
|
@ -671,9 +672,31 @@ class CodeTest(unittest.TestCase):
|
|||
VARARGS = CO_FAST_LOCAL | CO_FAST_ARG_VAR | CO_FAST_ARG_POS
|
||||
VARKWARGS = CO_FAST_LOCAL | CO_FAST_ARG_VAR | CO_FAST_ARG_KW
|
||||
|
||||
import test._code_definitions as defs
|
||||
funcs = {
|
||||
defs.spam_minimal: {},
|
||||
defs.spam_with_builtins: {
|
||||
'x': CO_FAST_LOCAL,
|
||||
'values': CO_FAST_LOCAL,
|
||||
'checks': CO_FAST_LOCAL,
|
||||
'res': CO_FAST_LOCAL,
|
||||
},
|
||||
defs.spam_with_globals_and_builtins: {
|
||||
'func1': CO_FAST_LOCAL,
|
||||
'func2': CO_FAST_LOCAL,
|
||||
'funcs': CO_FAST_LOCAL,
|
||||
'checks': CO_FAST_LOCAL,
|
||||
'res': CO_FAST_LOCAL,
|
||||
},
|
||||
defs.spam_returns_arg: {
|
||||
'x': POSORKW,
|
||||
},
|
||||
defs.spam_with_inner_not_closure: {
|
||||
'eggs': CO_FAST_LOCAL,
|
||||
},
|
||||
defs.spam_with_inner_closure: {
|
||||
'x': CO_FAST_CELL,
|
||||
'eggs': CO_FAST_LOCAL,
|
||||
},
|
||||
defs.spam_full: {
|
||||
'a': POSONLY,
|
||||
'b': POSONLY,
|
||||
|
|
@ -859,9 +882,26 @@ class CodeTest(unittest.TestCase):
|
|||
},
|
||||
}
|
||||
|
||||
import test._code_definitions as defs
|
||||
funcs = {
|
||||
defs.spam_minimal: new_var_counts(),
|
||||
defs.spam_with_builtins: new_var_counts(
|
||||
purelocals=4,
|
||||
globalvars=4,
|
||||
),
|
||||
defs.spam_with_globals_and_builtins: new_var_counts(
|
||||
purelocals=5,
|
||||
globalvars=6,
|
||||
),
|
||||
defs.spam_returns_arg: new_var_counts(
|
||||
posorkw=1,
|
||||
),
|
||||
defs.spam_with_inner_not_closure: new_var_counts(
|
||||
purelocals=1,
|
||||
),
|
||||
defs.spam_with_inner_closure: new_var_counts(
|
||||
othercells=1,
|
||||
purelocals=1,
|
||||
),
|
||||
defs.spam_full: new_var_counts(
|
||||
posonly=2,
|
||||
posorkw=2,
|
||||
|
|
@ -958,42 +998,35 @@ class CodeTest(unittest.TestCase):
|
|||
counts = _testinternalcapi.get_code_var_counts(func.__code__)
|
||||
self.assertEqual(counts, expected)
|
||||
|
||||
def func_with_globals_and_builtins():
|
||||
mod1 = _testinternalcapi
|
||||
mod2 = dis
|
||||
mods = (mod1, mod2)
|
||||
checks = tuple(callable(m) for m in mods)
|
||||
return callable(mod2), tuple(mods), list(mods), checks
|
||||
|
||||
func = func_with_globals_and_builtins
|
||||
func = defs.spam_with_globals_and_builtins
|
||||
with self.subTest(f'{func} code'):
|
||||
expected = new_var_counts(
|
||||
purelocals=4,
|
||||
globalvars=5,
|
||||
purelocals=5,
|
||||
globalvars=6,
|
||||
)
|
||||
counts = _testinternalcapi.get_code_var_counts(func.__code__)
|
||||
self.assertEqual(counts, expected)
|
||||
|
||||
with self.subTest(f'{func} with own globals and builtins'):
|
||||
expected = new_var_counts(
|
||||
purelocals=4,
|
||||
globalvars=(2, 3),
|
||||
purelocals=5,
|
||||
globalvars=(2, 4),
|
||||
)
|
||||
counts = _testinternalcapi.get_code_var_counts(func)
|
||||
self.assertEqual(counts, expected)
|
||||
|
||||
with self.subTest(f'{func} without globals'):
|
||||
expected = new_var_counts(
|
||||
purelocals=4,
|
||||
globalvars=(0, 3, 2),
|
||||
purelocals=5,
|
||||
globalvars=(0, 4, 2),
|
||||
)
|
||||
counts = _testinternalcapi.get_code_var_counts(func, globalsns={})
|
||||
self.assertEqual(counts, expected)
|
||||
|
||||
with self.subTest(f'{func} without both'):
|
||||
expected = new_var_counts(
|
||||
purelocals=4,
|
||||
globalvars=5,
|
||||
purelocals=5,
|
||||
globalvars=6,
|
||||
)
|
||||
counts = _testinternalcapi.get_code_var_counts(func, globalsns={},
|
||||
builtinsns={})
|
||||
|
|
@ -1001,12 +1034,34 @@ class CodeTest(unittest.TestCase):
|
|||
|
||||
with self.subTest(f'{func} without builtins'):
|
||||
expected = new_var_counts(
|
||||
purelocals=4,
|
||||
globalvars=(2, 0, 3),
|
||||
purelocals=5,
|
||||
globalvars=(2, 0, 4),
|
||||
)
|
||||
counts = _testinternalcapi.get_code_var_counts(func, builtinsns={})
|
||||
self.assertEqual(counts, expected)
|
||||
|
||||
@unittest.skipIf(_testinternalcapi is None, "missing _testinternalcapi")
|
||||
def test_stateless(self):
|
||||
self.maxDiff = None
|
||||
|
||||
for func in defs.STATELESS_CODE:
|
||||
with self.subTest((func, '(code)')):
|
||||
_testinternalcapi.verify_stateless_code(func.__code__)
|
||||
for func in defs.STATELESS_FUNCTIONS:
|
||||
with self.subTest((func, '(func)')):
|
||||
_testinternalcapi.verify_stateless_code(func)
|
||||
|
||||
for func in defs.FUNCTIONS:
|
||||
if func not in defs.STATELESS_CODE:
|
||||
with self.subTest((func, '(code)')):
|
||||
with self.assertRaises(Exception):
|
||||
_testinternalcapi.verify_stateless_code(func.__code__)
|
||||
|
||||
if func not in defs.STATELESS_FUNCTIONS:
|
||||
with self.subTest((func, '(func)')):
|
||||
with self.assertRaises(Exception):
|
||||
_testinternalcapi.verify_stateless_code(func)
|
||||
|
||||
|
||||
def isinterned(s):
|
||||
return s is sys.intern(('_' + s + '_')[1:-1])
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue