8000 GH-133231: Add JIT utilities in `sys._jit` by brandtbucher · Pull Request #133233 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

GH-133231: Add JIT utilities in sys._jit #133233

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 17 commits into from
May 5, 2025
Merged
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
Merge branch 'main' into sys-jit
  • Loading branch information
brandtbucher authored May 5, 2025
commit 614599c76a1b3f8c7b92d1d3cb1795157ed568c9
168 changes: 168 additions & 0 deletions Modules/_testinternalcapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -999,6 +999,172 @@ get_co_localskinds(PyObject *self, PyObject *arg)
return kinds;
}

static PyObject *
get_code_var_counts(PyObject *self, PyObject *_args, PyObject *_kwargs)
{
PyThreadState *tstate = _PyThreadState_GET();
PyObject *codearg;
PyObject *globalnames = NULL;
PyObject *attrnames = NULL;
PyObject *globalsns = NULL;
PyObject *builtinsns = NULL;
static char *kwlist[] = {"code", "globalnames", "attrnames", "globalsns",
"builtinsns", NULL};
if (!PyArg_ParseTupleAndKeywords(_args, _kwargs,
"O|OOO!O!:get_code_var_counts", kwlist,
&codearg, &globalnames, &attrnames,
&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;

_PyCode_var_counts_t counts = {0};
_PyCode_GetVarCounts(code, &counts);
if (_PyCode_SetUnboundVarCounts(
tstate, code, &counts, globalnames, attrnames,
globalsns, builtinsns) < 0)
{
return NULL;
}

#define SET_COUNT(DICT, STRUCT, NAME) \
do { \
PyObject *count = PyLong_FromLong(STRUCT.NAME); \
int res = PyDict_SetItemString(DICT, #NAME, count); \
Py_DECREF(count); \
if (res < 0) { \
goto error; \
} \
} while (0)

PyObject *locals = NULL;
PyObject *args = NULL;
PyObject *cells = NULL;
PyObject *hidden = NULL;
PyObject *unbound = NULL;
PyObject *globals = NULL;
PyObject *countsobj = PyDict_New();
if (countsobj == NULL) {
return NULL;
}
SET_COUNT(countsobj, counts, total);

// locals
locals = PyDict_New();
if (locals == NULL) {
goto error;
}
if (PyDict_SetItemString(countsobj, "locals", locals) < 0) {
goto error;
}
SET_COUNT(locals, counts.locals, total);

// locals.args
args = PyDict_New();
if (args == NULL) {
goto error;
}
if (PyDict_SetItemString(locals, "args", args) < 0) {
goto error;
}
SET_COUNT(args, counts.locals.args, total);
SET_COUNT(args, counts.locals.args, numposonly);
SET_COUNT(args, counts.locals.args, numposorkw);
SET_COUNT(args, counts.locals.args, numkwonly);
SET_COUNT(args, counts.locals.args, varargs);
SET_COUNT(args, counts.locals.args, varkwargs);

// locals.numpure
SET_COUNT(locals, counts.locals, numpure);

// locals.cells
cells = PyDict_New();
if (cells == NULL) {
goto error;
}
if (PyDict_SetItemString(locals, "cells", cells) < 0) {
goto error;
}
SET_COUNT(cells, counts.locals.cells, total);
SET_COUNT(cells, counts.locals.cells, numargs);
SET_COUNT(cells, counts.locals.cells, numothers);

// locals.hidden
hidden = PyDict_New();
if (hidden == NULL) {
goto error;
}
if (PyDict_SetItemString(locals, "hidden", hidden) < 0) {
goto error;
}
SET_COUNT(hidden, counts.locals.hidden, total);
SET_COUNT(hidden, counts.locals.hidden, numpure);
SET_COUNT(hidden, counts.locals.hidden, numcells);

// numfree
SET_COUNT(countsobj, counts, numfree);

// unbound
unbound = PyDict_New();
if (unbound == NULL) {
goto error;
}
if (PyDict_SetItemString(countsobj, "unbound", unbound) < 0) {
goto error;
}
SET_COUNT(unbound, counts.unbound, total);
SET_COUNT(unbound, counts.unbound, numattrs);
SET_COUNT(unbound, counts.unbound, numunknown);

// unbound.globals
globals = PyDict_New();
if (globals == NULL) {
goto error;
}
if (PyDict_SetItemString(unbound, "globals", globals) < 0) {
goto error;
}
SET_COUNT(globals, counts.unbound.globals, total);
SET_COUNT(globals, counts.unbound.globals, numglobal);
SET_COUNT(globals, counts.unbound.globals, numbuiltin);
SET_COUNT(globals, counts.unbound.globals, numunknown);

#undef SET_COUNT

Py_DECREF(locals);
Py_DECREF(args);
Py_DECREF(cells);
Py_DECREF(hidden);
Py_DECREF(unbound);
Py_DECREF(globals);
return countsobj;

error:
Py_DECREF(countsobj);
Py_XDECREF(locals);
Py_XDECREF(args);
Py_XDECREF(cells);
Py_XDECREF(hidden);
Py_XDECREF(unbound);
Py_XDECREF(globals);
return NULL;
}

#ifdef _Py_TIER2

static PyObject *
Expand Down Expand Up @@ -2119,6 +2285,8 @@ static PyMethodDef module_functions[] = {
{"code_returns_only_none", code_returns_only_none, METH_O, NULL},
{"get_co_framesize", get_co_framesize, METH_O, NULL},
{"get_co_localskinds", get_co_localskinds, METH_O, NULL},
{"get_code_var_counts", _PyCFunction_CAST(get_code_var_counts),
METH_VARARGS | METH_KEYWORDS, NULL},
#ifdef _Py_TIER2
{"add_executor_dependency", add_executor_dependency, METH_VARARGS, NULL},
{"invalidate_executors", invalidate_executors, METH_O, NULL},
Expand Down
Loading
You are viewing a condensed version of this merge commit. You can view the full changes here.
0