mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +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
|
@ -1165,6 +1165,48 @@ error:
|
|||
return NULL;
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
verify_stateless_code(PyObject *self, PyObject *args, PyObject *kwargs)
|
||||
{
|
||||
PyThreadState *tstate = _PyThreadState_GET();
|
||||
PyObject *codearg;
|
||||
PyObject *globalnames = NULL;
|
||||
PyObject *globalsns = NULL;
|
||||
PyObject *builtinsns = NULL;
|
||||
static char *kwlist[] = {"code", "globalnames",
|
||||
"globalsns", "builtinsns", NULL};
|
||||
if (!PyArg_ParseTupleAndKeywords(args, kwargs,
|
||||
"O|O!O!O!:get_code_var_counts", kwlist,
|
||||
&codearg, &PySet_Type, &globalnames,
|
||||
&PyDict_Type, &globalsns, &PyDict_Type, &builtinsns))
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
if (PyFunction_Check(codearg)) {
|
||||
if (globalsns == NULL) {
|
||||
globalsns = PyFunction_GET_GLOBALS(codearg);
|
||||
}
|
||||
if (builtinsns == NULL) {
|
||||
builtinsns = PyFunction_GET_BUILTINS(codearg);
|
||||
}
|
||||
codearg = PyFunction_GET_CODE(codearg);
|
||||
}
|
||||
else if (!PyCode_Check(codearg)) {
|
||||
PyErr_SetString(PyExc_TypeError,
|
||||
"argument must be a code object or a function");
|
||||
return NULL;
|
||||
}
|
||||
PyCodeObject *code = (PyCodeObject *)codearg;
|
||||
|
||||
if (_PyCode_VerifyStateless(
|
||||
tstate, code, globalnames, globalsns, builtinsns) < 0)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
|
||||
static PyObject *
|
||||
jit_enabled(PyObject *self, PyObject *arg)
|
||||
{
|
||||
|
@ -2293,6 +2335,8 @@ static PyMethodDef module_functions[] = {
|
|||
{"get_co_localskinds", get_co_localskinds, METH_O, NULL},
|
||||
{"get_code_var_counts", _PyCFunction_CAST(get_code_var_counts),
|
||||
METH_VARARGS | METH_KEYWORDS, NULL},
|
||||
{"verify_stateless_code", _PyCFunction_CAST(verify_stateless_code),
|
||||
METH_VARARGS | METH_KEYWORDS, NULL},
|
||||
{"jit_enabled", jit_enabled, METH_NOARGS, NULL},
|
||||
#ifdef _Py_TIER2
|
||||
{"add_executor_dependency", add_executor_dependency, METH_VARARGS, NULL},
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue