8000 gh-132775: Add _PyCode_VerifyStateless() by ericsnowcurrently · Pull Request #133221 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-132775: Add _PyCode_VerifyStateless() #133221

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

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add _PyCode_VerifyStateless().
  • Loading branch information
ericsnowcurrently committed Apr 30, 2025
commit a27039c0a13225c51ded0f117fe72cccba1422cd
11 changes: 11 additions & 0 deletions Include/internal/pycore_code.h
8000
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,17 @@ PyAPI_FUNC(int) _PyCode_SetUnboundVarCounts(
PyObject *globalsns,
PyObject *builtinsns);

PyAPI_FUNC(const char *) _PyCode_CheckStatelessTypes(PyCodeObject *);
PyAPI_FUNC(const char *) _PyCode_CheckStatelessValues(
PyCodeObject *,
_PyCode_var_counts_t *);
PyAPI_FUNC(int) _PyCode_VerifyStateless(
PyThreadState *,
PyCodeObject *,
PyObject *globalnames,
PyObject *globalsns,
PyObject *builtinsns);

PyAPI_FUNC(const char *) _PyCode_CheckPureFunction(PyCodeObject *);
PyAPI_FUNC(int) _PyCode_ReturnsOnlyNone(PyCodeObject *);

Expand Down
64 changes: 63 additions & 1 deletion Objects/codeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1925,6 +1925,69 @@ _PyCode_SetUnboundVarCounts(PyThreadState *tstate,
}


const char *
_PyCode_CheckStatelessValues(PyCodeObject *co, _PyCode_var_counts_t *counts)
{
if (counts->locals.cells.total > 0) {
return "nesting not supported";
}
if (counts->numfree > 0) { // There's a closure.
return "closures not supported";
}
assert(counts->locals.hidden.total == 0);

if (counts->unbound.globals.numglobal > 0) {
return "globals not supported";
}
else if (counts->unbound.globals.numbuiltin > 0
&& counts->unbound.globals.numunknown > 0) {
return "globals not supported";
}
// Otherwise we don't check counts.unbound.globals.numunknown since we can't
// distinguish beween globals and builtins here.

// Check other factors.
// We may consider relaxing these if it becomes a problem.
if (_PyCode_HAS_EXECUTORS(co) || _PyCode_HAS_INSTRUMENTATION(co)) {
return "only basic code objects are supported";
}
if (co->_co_monitoring != NULL) {
return "only basic code objects are supported";
}
if (co->co_extra != NULL) {
return "only basic code objects are supported";
}

return NULL;
}

int
_PyCode_VerifyStateless(PyThreadState *tstate,
PyCodeObject *co, PyObject *globalnames,
PyObject *globalsns, PyObject *builtinsns)
{
const char *errmsg = _PyCode_CheckPureFunction(co);
if (errmsg != NULL) {
_PyErr_SetString(tstate, PyExc_TypeError, errmsg);
return -1;
}
_PyCode_var_counts_t counts = {0};
_PyCode_GetVarCounts(co, &counts);
if (_PyCode_SetUnboundVarCounts(
tstate, co, &counts, globalnames, NULL,
globalsns, builtinsns) < 0)
{
return -1;
}
errmsg = _PyCode_CheckStatelessValues(co, &counts);
if (errmsg != NULL) {
_PyErr_SetString(tstate, PyExc_ValueError, errmsg);
return -1;
}
return 0;
}


const char *
_PyCode_CheckPureFunction(PyCodeObject *co)
{
Expand All @@ -1943,7 +2006,6 @@ _PyCode_CheckPureFunction(PyCodeObject *co)
return NULL;
}


/* Here "value" means a non-None value, since a bare return is identical
* to returning None explicitly. Likewise a missing return statement
* at the end of the function is turned into "return None". */
Expand Down
0