From d9c3672b2e766128ce7accfa1b6b7e4c39c50ac2 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Thu, 1 May 2025 10:09:39 -0600 Subject: [PATCH 01/22] gh-132775: Unrevert "Add _PyCode_GetVarCounts()" This reverts commit 811edcf9cda5fb09aa5189e88e693d35dee7a2d1 (gh-133128). --- Include/cpython/funcobject.h | 5 + Include/internal/pycore_code.h | 51 +++++++ Lib/test/test_code.py | 230 ++++++++++++++++++++++++++++++++ Modules/_testinternalcapi.c | 168 +++++++++++++++++++++++ Objects/codeobject.c | 235 +++++++++++++++++++++++++++++++++ 5 files changed, 689 insertions(+) diff --git a/Include/cpython/funcobject.h b/Include/cpython/funcobject.h index 598cd330bc9ca9..18249b95befe65 100644 --- a/Include/cpython/funcobject.h +++ b/Include/cpython/funcobject.h @@ -97,6 +97,11 @@ static inline PyObject* PyFunction_GET_GLOBALS(PyObject *func) { } #define PyFunction_GET_GLOBALS(func) PyFunction_GET_GLOBALS(_PyObject_CAST(func)) +static inline PyObject* PyFunction_GET_BUILTINS(PyObject *func) { + return _PyFunction_CAST(func)->func_builtins; +} +#define PyFunction_GET_BUILTINS(func) PyFunction_GET_BUILTINS(_PyObject_CAST(func)) + static inline PyObject* PyFunction_GET_MODULE(PyObject *func) { return _PyFunction_CAST(func)->func_module; } diff --git a/Include/internal/pycore_code.h b/Include/internal/pycore_code.h index 635d2b24f4bdff..9b02e2934aa49e 100644 --- a/Include/internal/pycore_code.h +++ b/Include/internal/pycore_code.h @@ -565,6 +565,57 @@ extern int _Py_ClearUnusedTLBC(PyInterpreterState *interp); #endif +typedef struct { + int total; + struct co_locals_counts { + int total; + struct { + int total; + int numposonly; + int numposorkw; + int numkwonly; + int varargs; + int varkwargs; + } args; + int numpure; + struct { + int total; + // numargs does not contribute to locals.total. + int numargs; + int numothers; + } cells; + struct { + int total; + int numpure; + int numcells; + } hidden; + } locals; + int numfree; // nonlocal + struct co_unbound_counts { + int total; + struct { + int total; + int numglobal; + int numbuiltin; + int numunknown; + } globals; + int numattrs; + int numunknown; + } unbound; +} _PyCode_var_counts_t; + +PyAPI_FUNC(void) _PyCode_GetVarCounts( + PyCodeObject *, + _PyCode_var_counts_t *); +PyAPI_FUNC(int) _PyCode_SetUnboundVarCounts( + PyThreadState *, + PyCodeObject *, + _PyCode_var_counts_t *, + PyObject *globalnames, + PyObject *attrnames, + PyObject *globalsns, + PyObject *builtinsns); + PyAPI_FUNC(int) _PyCode_ReturnsOnlyNone(PyCodeObject *); diff --git a/Lib/test/test_code.py b/Lib/test/test_code.py index 7cf09ee7847dc1..1b6dfe7c7890ad 100644 --- a/Lib/test/test_code.py +++ b/Lib/test/test_code.py @@ -777,6 +777,236 @@ def test_local_kinds(self): kinds = _testinternalcapi.get_co_localskinds(func.__code__) self.assertEqual(kinds, expected) + @unittest.skipIf(_testinternalcapi is None, "missing _testinternalcapi") + def test_var_counts(self): + self.maxDiff = None + def new_var_counts(*, + posonly=0, + posorkw=0, + kwonly=0, + varargs=0, + varkwargs=0, + purelocals=0, + argcells=0, + othercells=0, + freevars=0, + globalvars=0, + attrs=0, + unknown=0, + ): + nargvars = posonly + posorkw + kwonly + varargs + varkwargs + nlocals = nargvars + purelocals + othercells + if isinstance(globalvars, int): + globalvars = { + 'total': globalvars, + 'numglobal': 0, + 'numbuiltin': 0, + 'numunknown': globalvars, + } + else: + g_numunknown = 0 + if isinstance(globalvars, dict): + numglobal = globalvars['numglobal'] + numbuiltin = globalvars['numbuiltin'] + size = 2 + if 'numunknown' in globalvars: + g_numunknown = globalvars['numunknown'] + size += 1 + assert len(globalvars) == size, globalvars + else: + assert not isinstance(globalvars, str), repr(globalvars) + try: + numglobal, numbuiltin = globalvars + except ValueError: + numglobal, numbuiltin, g_numunknown = globalvars + globalvars = { + 'total': numglobal + numbuiltin + g_numunknown, + 'numglobal': numglobal, + 'numbuiltin': numbuiltin, + 'numunknown': g_numunknown, + } + unbound = globalvars['total'] + attrs + unknown + return { + 'total': nlocals + freevars + unbound, + 'locals': { + 'total': nlocals, + 'args': { + 'total': nargvars, + 'numposonly': posonly, + 'numposorkw': posorkw, + 'numkwonly': kwonly, + 'varargs': varargs, + 'varkwargs': varkwargs, + }, + 'numpure': purelocals, + 'cells': { + 'total': argcells + othercells, + 'numargs': argcells, + 'numothers': othercells, + }, + 'hidden': { + 'total': 0, + 'numpure': 0, + 'numcells': 0, + }, + }, + 'numfree': freevars, + 'unbound': { + 'total': unbound, + 'globals': globalvars, + 'numattrs': attrs, + 'numunknown': unknown, + }, + } + + import test._code_definitions as defs + funcs = { + defs.spam_minimal: new_var_counts(), + defs.spam_full: new_var_counts( + posonly=2, + posorkw=2, + kwonly=2, + varargs=1, + varkwargs=1, + purelocals=4, + globalvars=3, + attrs=1, + ), + defs.spam: new_var_counts( + posorkw=1, + ), + defs.spam_N: new_var_counts( + posorkw=1, + purelocals=1, + ), + defs.spam_C: new_var_counts( + posorkw=1, + purelocals=1, + argcells=1, + othercells=1, + ), + defs.spam_NN: new_var_counts( + posorkw=1, + purelocals=1, + ), + defs.spam_NC: new_var_counts( + posorkw=1, + purelocals=1, + argcells=1, + othercells=1, + ), + defs.spam_CN: new_var_counts( + posorkw=1, + purelocals=1, + argcells=1, + othercells=1, + ), + defs.spam_CC: new_var_counts( + posorkw=1, + purelocals=1, + argcells=1, + othercells=1, + ), + defs.eggs_nested: new_var_counts( + posorkw=1, + ), + defs.eggs_closure: new_var_counts( + posorkw=1, + freevars=2, + ), + defs.eggs_nested_N: new_var_counts( + posorkw=1, + purelocals=1, + ), + defs.eggs_nested_C: new_var_counts( + posorkw=1, + purelocals=1, + argcells=1, + freevars=2, + ), + defs.eggs_closure_N: new_var_counts( + posorkw=1, + purelocals=1, + freevars=2, + ), + defs.eggs_closure_C: new_var_counts( + posorkw=1, + purelocals=1, + argcells=1, + othercells=1, + freevars=2, + ), + defs.ham_nested: new_var_counts( + posorkw=1, + ), + defs.ham_closure: new_var_counts( + posorkw=1, + freevars=3, + ), + defs.ham_C_nested: new_var_counts( + posorkw=1, + ), + defs.ham_C_closure: new_var_counts( + posorkw=1, + freevars=4, + ), + } + assert len(funcs) == len(defs.FUNCTIONS), (len(funcs), len(defs.FUNCTIONS)) + for func in defs.FUNCTIONS: + with self.subTest(func): + expected = funcs[func] + 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 + with self.subTest(f'{func} code'): + expected = new_var_counts( + purelocals=4, + globalvars=5, + ) + 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), + ) + 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), + ) + 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, + ) + counts = _testinternalcapi.get_code_var_counts(func, globalsns={}, + builtinsns={}) + self.assertEqual(counts, expected) + + with self.subTest(f'{func} without builtins'): + expected = new_var_counts( + purelocals=4, + globalvars=(2, 0, 3), + ) + counts = _testinternalcapi.get_code_var_counts(func, builtinsns={}) + self.assertEqual(counts, expected) + def isinterned(s): return s is sys.intern(('_' + s + '_')[1:-1]) diff --git a/Modules/_testinternalcapi.c b/Modules/_testinternalcapi.c index 065f4135b75747..812737e294fcb7 100644 --- a/Modules/_testinternalcapi.c +++ b/Modules/_testinternalcapi.c @@ -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; +} + static PyObject * jit_enabled(PyObject *self, PyObject *arg) { @@ -2125,6 +2291,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}, {"jit_enabled", jit_enabled, METH_NOARGS, NULL}, #ifdef _Py_TIER2 {"add_executor_dependency", add_executor_dependency, METH_VARARGS, NULL}, diff --git a/Objects/codeobject.c b/Objects/codeobject.c index bf24a4af445356..d643eb9fd61ae9 100644 --- a/Objects/codeobject.c +++ b/Objects/codeobject.c @@ -1690,6 +1690,241 @@ PyCode_GetFreevars(PyCodeObject *code) } +static int +identify_unbound_names(PyThreadState *tstate, PyCodeObject *co, + PyObject *globalnames, PyObject *attrnames, + PyObject *globalsns, PyObject *builtinsns, + struct co_unbound_counts *counts) +{ + // This function is inspired by inspect.getclosurevars(). + // It would be nicer if we had something similar to co_localspluskinds, + // but for co_names. + assert(globalnames != NULL); + assert(PySet_Check(globalnames)); + assert(PySet_GET_SIZE(globalnames) == 0 || counts != NULL); + assert(attrnames != NULL); + assert(PySet_Check(attrnames)); + assert(PySet_GET_SIZE(attrnames) == 0 || counts != NULL); + assert(globalsns == NULL || PyDict_Check(globalsns)); + assert(builtinsns == NULL || PyDict_Check(builtinsns)); + assert(counts == NULL || counts->total == 0); + Py_ssize_t len = Py_SIZE(co); + for (int i = 0; i < len; i++) { + _Py_CODEUNIT inst = _Py_GetBaseCodeUnit(co, i); + if (inst.op.code == LOAD_ATTR) { + PyObject *name = PyTuple_GET_ITEM(co->co_names, inst.op.arg>>1); + if (counts != NULL) { + if (PySet_Contains(attrnames, name)) { + if (_PyErr_Occurred(tstate)) { + return -1; + } + continue; + } + counts->total += 1; + counts->numattrs += 1; + } + if (PySet_Add(attrnames, name) < 0) { + return -1; + } + } + else if (inst.op.code == LOAD_GLOBAL) { + PyObject *name = PyTuple_GET_ITEM(co->co_names, inst.op.arg>>1); + if (counts != NULL) { + if (PySet_Contains(globalnames, name)) { + if (_PyErr_Occurred(tstate)) { + return -1; + } + continue; + } + counts->total += 1; + counts->globals.total += 1; + counts->globals.numunknown += 1; + if (globalsns != NULL && PyDict_Contains(globalsns, name)) { + if (_PyErr_Occurred(tstate)) { + return -1; + } + counts->globals.numglobal += 1; + counts->globals.numunknown -= 1; + } + if (builtinsns != NULL && PyDict_Contains(builtinsns, name)) { + if (_PyErr_Occurred(tstate)) { + return -1; + } + counts->globals.numbuiltin += 1; + counts->globals.numunknown -= 1; + } + } + if (PySet_Add(globalnames, name) < 0) { + return -1; + } + } + } + return 0; +} + + +void +_PyCode_GetVarCounts(PyCodeObject *co, _PyCode_var_counts_t *counts) +{ + // Count the locals, cells, and free vars. + struct co_locals_counts locals = {0}; + int numfree = 0; + PyObject *kinds = co->co_localspluskinds; + Py_ssize_t numlocalplusfree = PyBytes_GET_SIZE(kinds); + for (int i = 0; i < numlocalplusfree; i++) { + _PyLocals_Kind kind = _PyLocals_GetKind(co->co_localspluskinds, i); + if (kind & CO_FAST_FREE) { + assert(!(kind & CO_FAST_LOCAL)); + assert(!(kind & CO_FAST_HIDDEN)); + assert(!(kind & CO_FAST_ARG)); + numfree += 1; + } + else { + // Apparently not all non-free vars a CO_FAST_LOCAL. + assert(kind); + locals.total += 1; + if (kind & CO_FAST_ARG) { + locals.args.total += 1; + if (kind & CO_FAST_ARG_VAR) { + if (kind & CO_FAST_ARG_POS) { + assert(!(kind & CO_FAST_ARG_KW)); + assert(!locals.args.varargs); + locals.args.varargs = 1; + } + else { + assert(kind & CO_FAST_ARG_KW); + assert(!locals.args.varkwargs); + locals.args.varkwargs = 1; + } + } + else if (kind & CO_FAST_ARG_POS) { + if (kind & CO_FAST_ARG_KW) { + locals.args.numposorkw += 1; + } + else { + locals.args.numposonly += 1; + } + } + else { + assert(kind & CO_FAST_ARG_KW); + locals.args.numkwonly += 1; + } + if (kind & CO_FAST_CELL) { + locals.cells.total += 1; + locals.cells.numargs += 1; + } + // Args are never hidden currently. + assert(!(kind & CO_FAST_HIDDEN)); + } + else { + if (kind & CO_FAST_CELL) { + locals.cells.total += 1; + locals.cells.numothers += 1; + if (kind & CO_FAST_HIDDEN) { + locals.hidden.total += 1; + locals.hidden.numcells += 1; + } + } + else { + locals.numpure += 1; + if (kind & CO_FAST_HIDDEN) { + locals.hidden.total += 1; + locals.hidden.numpure += 1; + } + } + } + } + } + assert(locals.args.total == ( + co->co_argcount + co->co_kwonlyargcount + + !!(co->co_flags & CO_VARARGS) + + !!(co->co_flags & CO_VARKEYWORDS))); + assert(locals.args.numposonly == co->co_posonlyargcount); + assert(locals.args.numposonly + locals.args.numposorkw == co->co_argcount); + assert(locals.args.numkwonly == co->co_kwonlyargcount); + assert(locals.cells.total == co->co_ncellvars); + assert(locals.args.total + locals.numpure == co->co_nlocals); + assert(locals.total + locals.cells.numargs == co->co_nlocals + co->co_ncellvars); + assert(locals.total + numfree == co->co_nlocalsplus); + assert(numfree == co->co_nfreevars); + + // Get the unbound counts. + assert(PyTuple_GET_SIZE(co->co_names) >= 0); + struct co_unbound_counts unbound = { + .total = (int)PyTuple_GET_SIZE(co->co_names), + // numglobal and numattrs can be set later + // with _PyCode_SetUnboundVarCounts(). + .numunknown = (int)PyTuple_GET_SIZE(co->co_names), + }; + + // "Return" the result. + *counts = (_PyCode_var_counts_t){ + .total = locals.total + numfree + unbound.total, + .locals = locals, + .numfree = numfree, + .unbound = unbound, + }; +} + +int +_PyCode_SetUnboundVarCounts(PyThreadState *tstate, + PyCodeObject *co, _PyCode_var_counts_t *counts, + PyObject *globalnames, PyObject *attrnames, + PyObject *globalsns, PyObject *builtinsns) +{ + int res = -1; + PyObject *globalnames_owned = NULL; + PyObject *attrnames_owned = NULL; + + // Prep the name sets. + if (globalnames == NULL) { + globalnames_owned = PySet_New(NULL); + if (globalnames_owned == NULL) { + goto finally; + } + globalnames = globalnames_owned; + } + else if (!PySet_Check(globalnames)) { + _PyErr_Format(tstate, PyExc_TypeError, + "expected a set for \"globalnames\", got %R", globalnames); + goto finally; + } + if (attrnames == NULL) { + attrnames_owned = PySet_New(NULL); + if (attrnames_owned == NULL) { + goto finally; + } + attrnames = attrnames_owned; + } + else if (!PySet_Check(attrnames)) { + _PyErr_Format(tstate, PyExc_TypeError, + "expected a set for \"attrnames\", got %R", attrnames); + goto finally; + } + + // Fill in unbound.globals and unbound.numattrs. + struct co_unbound_counts unbound = {0}; + if (identify_unbound_names( + tstate, co, globalnames, attrnames, globalsns, builtinsns, + &unbound) < 0) + { + goto finally; + } + assert(unbound.numunknown == 0); + assert(unbound.total <= counts->unbound.total); + assert(counts->unbound.numunknown == counts->unbound.total); + unbound.numunknown = counts->unbound.total - unbound.total; + unbound.total = counts->unbound.total; + counts->unbound = unbound; + res = 0; + +finally: + Py_XDECREF(globalnames_owned); + Py_XDECREF(attrnames_owned); + return res; +} + + /* 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". */ From 54b61920f2bae93335c20929f064f6d43d291194 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Thu, 1 May 2025 10:24:51 -0600 Subject: [PATCH 02/22] Avoid casting to int. --- Objects/codeobject.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Objects/codeobject.c b/Objects/codeobject.c index d643eb9fd61ae9..27ec6700f1a29a 100644 --- a/Objects/codeobject.c +++ b/Objects/codeobject.c @@ -1849,12 +1849,13 @@ _PyCode_GetVarCounts(PyCodeObject *co, _PyCode_var_counts_t *counts) assert(numfree == co->co_nfreevars); // Get the unbound counts. - assert(PyTuple_GET_SIZE(co->co_names) >= 0); + int numunbound = PyTuple_GET_SIZE(co->co_names); + assert(numunbound >= 0); struct co_unbound_counts unbound = { - .total = (int)PyTuple_GET_SIZE(co->co_names), + .total = numunbound, // numglobal and numattrs can be set later // with _PyCode_SetUnboundVarCounts(). - .numunknown = (int)PyTuple_GET_SIZE(co->co_names), + .numunknown = numunbound, }; // "Return" the result. From b97326fa6dfe9d47b48538e2b52b8d8d63958363 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Thu, 1 May 2025 11:45:10 -0600 Subject: [PATCH 03/22] Cast to int. --- Objects/codeobject.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Objects/codeobject.c b/Objects/codeobject.c index 27ec6700f1a29a..02b5236ed5556e 100644 --- a/Objects/codeobject.c +++ b/Objects/codeobject.c @@ -1849,8 +1849,9 @@ _PyCode_GetVarCounts(PyCodeObject *co, _PyCode_var_counts_t *counts) assert(numfree == co->co_nfreevars); // Get the unbound counts. - int numunbound = PyTuple_GET_SIZE(co->co_names); - assert(numunbound >= 0); + assert(PyTuple_GET_SIZE(co->co_names) >= 0); + assert(PyTuple_GET_SIZE(co->co_names) < INT_MAX); + int numunbound = (int)PyTuple_GET_SIZE(co->co_names); struct co_unbound_counts unbound = { .total = numunbound, // numglobal and numattrs can be set later From 1e6aa8223221f5003248ff111d52d4979b35819a Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Thu, 1 May 2025 11:46:44 -0600 Subject: [PATCH 04/22] Add an assert. --- Objects/codeobject.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Objects/codeobject.c b/Objects/codeobject.c index 02b5236ed5556e..843365163810f0 100644 --- a/Objects/codeobject.c +++ b/Objects/codeobject.c @@ -1766,6 +1766,8 @@ identify_unbound_names(PyThreadState *tstate, PyCodeObject *co, void _PyCode_GetVarCounts(PyCodeObject *co, _PyCode_var_counts_t *counts) { + assert(counts != NULL); + // Count the locals, cells, and free vars. struct co_locals_counts locals = {0}; int numfree = 0; From 881c7985758880f600d16dd5ee3259d5d753e710 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Thu, 1 May 2025 13:39:58 -0600 Subject: [PATCH 05/22] Add some helper macros. --- Objects/codeobject.c | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/Objects/codeobject.c b/Objects/codeobject.c index 843365163810f0..4374e48d29faa5 100644 --- a/Objects/codeobject.c +++ b/Objects/codeobject.c @@ -1690,6 +1690,26 @@ PyCode_GetFreevars(PyCodeObject *code) } +#define GET_OPARG(co, i, initial) (initial) +// We may want to move these macros to pycore_opcode_utils.h +// and use them in Python/bytecodes.c. +#define LOAD_GLOBAL_NAME_INDEX(oparg) ((oparg)>>1) +#define LOAD_ATTR_NAME_INDEX(oparg) ((oparg)>>1) + +#ifndef Py_DEBUG +#define GETITEM(v, i) PyTuple_GET_ITEM((v), (i)) +#else +static inline PyObject * +GETITEM(PyObject *v, Py_ssize_t i) +{ + assert(PyTuple_Check(v)); + assert(i >= 0); + assert(i < PyTuple_GET_SIZE(v)); + assert(PyTuple_GET_ITEM(v, i) != NULL); + return PyTuple_GET_ITEM(v, i); +} +#endif + static int identify_unbound_names(PyThreadState *tstate, PyCodeObject *co, PyObject *globalnames, PyObject *attrnames, @@ -1712,7 +1732,9 @@ identify_unbound_names(PyThreadState *tstate, PyCodeObject *co, for (int i = 0; i < len; i++) { _Py_CODEUNIT inst = _Py_GetBaseCodeUnit(co, i); if (inst.op.code == LOAD_ATTR) { - PyObject *name = PyTuple_GET_ITEM(co->co_names, inst.op.arg>>1); + int oparg = GET_OPARG(co, i, inst.op.arg); + int index = LOAD_ATTR_NAME_INDEX(oparg); + PyObject *name = GETITEM(co->co_names, index); if (counts != NULL) { if (PySet_Contains(attrnames, name)) { if (_PyErr_Occurred(tstate)) { @@ -1728,7 +1750,9 @@ identify_unbound_names(PyThreadState *tstate, PyCodeObject *co, } } else if (inst.op.code == LOAD_GLOBAL) { - PyObject *name = PyTuple_GET_ITEM(co->co_names, inst.op.arg>>1); + int oparg = GET_OPARG(co, i, inst.op.arg); + int index = LOAD_ATTR_NAME_INDEX(oparg); + PyObject *name = GETITEM(co->co_names, index); if (counts != NULL) { if (PySet_Contains(globalnames, name)) { if (_PyErr_Occurred(tstate)) { From ae1efb3fdd6210d9dee3587693efe1a2de1f8bda Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Thu, 1 May 2025 13:53:14 -0600 Subject: [PATCH 06/22] Track counts using a local variable. --- Objects/codeobject.c | 58 ++++++++++++++++++++++---------------------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/Objects/codeobject.c b/Objects/codeobject.c index 4374e48d29faa5..63585ed1871f6d 100644 --- a/Objects/codeobject.c +++ b/Objects/codeobject.c @@ -1728,6 +1728,7 @@ identify_unbound_names(PyThreadState *tstate, PyCodeObject *co, assert(globalsns == NULL || PyDict_Check(globalsns)); assert(builtinsns == NULL || PyDict_Check(builtinsns)); assert(counts == NULL || counts->total == 0); + struct co_unbound_counts unbound = {0}; Py_ssize_t len = Py_SIZE(co); for (int i = 0; i < len; i++) { _Py_CODEUNIT inst = _Py_GetBaseCodeUnit(co, i); @@ -1735,16 +1736,14 @@ identify_unbound_names(PyThreadState *tstate, PyCodeObject *co, int oparg = GET_OPARG(co, i, inst.op.arg); int index = LOAD_ATTR_NAME_INDEX(oparg); PyObject *name = GETITEM(co->co_names, index); - if (counts != NULL) { - if (PySet_Contains(attrnames, name)) { - if (_PyErr_Occurred(tstate)) { - return -1; - } - continue; + if (PySet_Contains(attrnames, name)) { + if (_PyErr_Occurred(tstate)) { + return -1; } - counts->total += 1; - counts->numattrs += 1; + continue; } + unbound.total += 1; + unbound.numattrs += 1; if (PySet_Add(attrnames, name) < 0) { return -1; } @@ -1753,36 +1752,37 @@ identify_unbound_names(PyThreadState *tstate, PyCodeObject *co, int oparg = GET_OPARG(co, i, inst.op.arg); int index = LOAD_ATTR_NAME_INDEX(oparg); PyObject *name = GETITEM(co->co_names, index); - if (counts != NULL) { - if (PySet_Contains(globalnames, name)) { - if (_PyErr_Occurred(tstate)) { - return -1; - } - continue; + if (PySet_Contains(globalnames, name)) { + if (_PyErr_Occurred(tstate)) { + return -1; } - counts->total += 1; - counts->globals.total += 1; - counts->globals.numunknown += 1; - if (globalsns != NULL && PyDict_Contains(globalsns, name)) { - if (_PyErr_Occurred(tstate)) { - return -1; - } - counts->globals.numglobal += 1; - counts->globals.numunknown -= 1; + continue; + } + unbound.total += 1; + unbound.globals.total += 1; + unbound.globals.numunknown += 1; + if (globalsns != NULL && PyDict_Contains(globalsns, name)) { + if (_PyErr_Occurred(tstate)) { + return -1; } - if (builtinsns != NULL && PyDict_Contains(builtinsns, name)) { - if (_PyErr_Occurred(tstate)) { - return -1; - } - counts->globals.numbuiltin += 1; - counts->globals.numunknown -= 1; + unbound.globals.numglobal += 1; + unbound.globals.numunknown -= 1; + } + if (builtinsns != NULL && PyDict_Contains(builtinsns, name)) { + if (_PyErr_Occurred(tstate)) { + return -1; } + unbound.globals.numbuiltin += 1; + unbound.globals.numunknown -= 1; } if (PySet_Add(globalnames, name) < 0) { return -1; } } } + if (counts != NULL) { + *counts = unbound; + } return 0; } From 5f6fe84a7889bb1a8220bed44ec21aa7b869dd52 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Thu, 1 May 2025 13:57:49 -0600 Subject: [PATCH 07/22] Do not double-count names. --- Objects/codeobject.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Objects/codeobject.c b/Objects/codeobject.c index 63585ed1871f6d..0394817c636661 100644 --- a/Objects/codeobject.c +++ b/Objects/codeobject.c @@ -1760,20 +1760,20 @@ identify_unbound_names(PyThreadState *tstate, PyCodeObject *co, } unbound.total += 1; unbound.globals.total += 1; - unbound.globals.numunknown += 1; if (globalsns != NULL && PyDict_Contains(globalsns, name)) { if (_PyErr_Occurred(tstate)) { return -1; } unbound.globals.numglobal += 1; - unbound.globals.numunknown -= 1; } - if (builtinsns != NULL && PyDict_Contains(builtinsns, name)) { + else if (builtinsns != NULL && PyDict_Contains(builtinsns, name)) { if (_PyErr_Occurred(tstate)) { return -1; } unbound.globals.numbuiltin += 1; - unbound.globals.numunknown -= 1; + } + else { + unbound.globals.numunknown += 1; } if (PySet_Add(globalnames, name) < 0) { return -1; From c9855f160715ce3d99a527c55290937bca13abcd Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Thu, 1 May 2025 20:35:26 -0600 Subject: [PATCH 08/22] Temporarily skip some code. --- Objects/codeobject.c | 1 + 1 file changed, 1 insertion(+) diff --git a/Objects/codeobject.c b/Objects/codeobject.c index 0394817c636661..c29cb9a014d92f 100644 --- a/Objects/codeobject.c +++ b/Objects/codeobject.c @@ -1728,6 +1728,7 @@ identify_unbound_names(PyThreadState *tstate, PyCodeObject *co, assert(globalsns == NULL || PyDict_Check(globalsns)); assert(builtinsns == NULL || PyDict_Check(builtinsns)); assert(counts == NULL || counts->total == 0); + return 0; struct co_unbound_counts unbound = {0}; Py_ssize_t len = Py_SIZE(co); for (int i = 0; i < len; i++) { From f1f9df92ab1412022f97408c960782f49874cba5 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Fri, 2 May 2025 09:55:39 -0600 Subject: [PATCH 09/22] Temporarily skip some code. --- Objects/codeobject.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Objects/codeobject.c b/Objects/codeobject.c index c29cb9a014d92f..307c883def4d50 100644 --- a/Objects/codeobject.c +++ b/Objects/codeobject.c @@ -1728,7 +1728,6 @@ identify_unbound_names(PyThreadState *tstate, PyCodeObject *co, assert(globalsns == NULL || PyDict_Check(globalsns)); assert(builtinsns == NULL || PyDict_Check(builtinsns)); assert(counts == NULL || counts->total == 0); - return 0; struct co_unbound_counts unbound = {0}; Py_ssize_t len = Py_SIZE(co); for (int i = 0; i < len; i++) { @@ -1737,6 +1736,8 @@ identify_unbound_names(PyThreadState *tstate, PyCodeObject *co, int oparg = GET_OPARG(co, i, inst.op.arg); int index = LOAD_ATTR_NAME_INDEX(oparg); PyObject *name = GETITEM(co->co_names, index); +// The previous 3 lines might be the problem. +continue; if (PySet_Contains(attrnames, name)) { if (_PyErr_Occurred(tstate)) { return -1; @@ -1753,6 +1754,8 @@ identify_unbound_names(PyThreadState *tstate, PyCodeObject *co, int oparg = GET_OPARG(co, i, inst.op.arg); int index = LOAD_ATTR_NAME_INDEX(oparg); PyObject *name = GETITEM(co->co_names, index); +// The previous 3 lines might be the problem. +continue; if (PySet_Contains(globalnames, name)) { if (_PyErr_Occurred(tstate)) { return -1; From 09678e403cf0999d6b83b16f7efa2fe515e7460c Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Fri, 2 May 2025 13:55:41 -0600 Subject: [PATCH 10/22] Temporarily skip some code. --- Objects/codeobject.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Objects/codeobject.c b/Objects/codeobject.c index 307c883def4d50..56eff215766e4b 100644 --- a/Objects/codeobject.c +++ b/Objects/codeobject.c @@ -1734,10 +1734,10 @@ identify_unbound_names(PyThreadState *tstate, PyCodeObject *co, _Py_CODEUNIT inst = _Py_GetBaseCodeUnit(co, i); if (inst.op.code == LOAD_ATTR) { int oparg = GET_OPARG(co, i, inst.op.arg); +// The next line might be the problem. +continue; int index = LOAD_ATTR_NAME_INDEX(oparg); PyObject *name = GETITEM(co->co_names, index); -// The previous 3 lines might be the problem. -continue; if (PySet_Contains(attrnames, name)) { if (_PyErr_Occurred(tstate)) { return -1; @@ -1752,10 +1752,10 @@ continue; } else if (inst.op.code == LOAD_GLOBAL) { int oparg = GET_OPARG(co, i, inst.op.arg); +// The next line might be the problem. +continue; int index = LOAD_ATTR_NAME_INDEX(oparg); PyObject *name = GETITEM(co->co_names, index); -// The previous 3 lines might be the problem. -continue; if (PySet_Contains(globalnames, name)) { if (_PyErr_Occurred(tstate)) { return -1; @@ -1784,9 +1784,9 @@ continue; } } } - if (counts != NULL) { - *counts = unbound; - } +// if (counts != NULL) { +// *counts = unbound; +// } return 0; } From 1ab3c284d9e08037be45d1a2a0ac1c8806c33684 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Fri, 2 May 2025 19:45:55 -0600 Subject: [PATCH 11/22] Temporarily skip some code. --- Objects/codeobject.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Objects/codeobject.c b/Objects/codeobject.c index 56eff215766e4b..f8f3d590c1721f 100644 --- a/Objects/codeobject.c +++ b/Objects/codeobject.c @@ -1733,9 +1733,9 @@ identify_unbound_names(PyThreadState *tstate, PyCodeObject *co, for (int i = 0; i < len; i++) { _Py_CODEUNIT inst = _Py_GetBaseCodeUnit(co, i); if (inst.op.code == LOAD_ATTR) { - int oparg = GET_OPARG(co, i, inst.op.arg); -// The next line might be the problem. +// Something outside this block might be the problem. continue; + int oparg = GET_OPARG(co, i, inst.op.arg); int index = LOAD_ATTR_NAME_INDEX(oparg); PyObject *name = GETITEM(co->co_names, index); if (PySet_Contains(attrnames, name)) { @@ -1751,9 +1751,9 @@ continue; } } else if (inst.op.code == LOAD_GLOBAL) { - int oparg = GET_OPARG(co, i, inst.op.arg); -// The next line might be the problem. +// Something outside this block might be the problem. continue; + int oparg = GET_OPARG(co, i, inst.op.arg); int index = LOAD_ATTR_NAME_INDEX(oparg); PyObject *name = GETITEM(co->co_names, index); if (PySet_Contains(globalnames, name)) { From 3ba74f6e2f733b04ef62d879ddd764e22e7de281 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Fri, 2 May 2025 21:02:03 -0600 Subject: [PATCH 12/22] Temporarily skip some code. --- Objects/codeobject.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/Objects/codeobject.c b/Objects/codeobject.c index f8f3d590c1721f..d43b1bb3131816 100644 --- a/Objects/codeobject.c +++ b/Objects/codeobject.c @@ -1716,6 +1716,7 @@ identify_unbound_names(PyThreadState *tstate, PyCodeObject *co, PyObject *globalsns, PyObject *builtinsns, struct co_unbound_counts *counts) { + return 0; // This function is inspired by inspect.getclosurevars(). // It would be nicer if we had something similar to co_localspluskinds, // but for co_names. @@ -1733,8 +1734,6 @@ identify_unbound_names(PyThreadState *tstate, PyCodeObject *co, for (int i = 0; i < len; i++) { _Py_CODEUNIT inst = _Py_GetBaseCodeUnit(co, i); if (inst.op.code == LOAD_ATTR) { -// Something outside this block might be the problem. -continue; int oparg = GET_OPARG(co, i, inst.op.arg); int index = LOAD_ATTR_NAME_INDEX(oparg); PyObject *name = GETITEM(co->co_names, index); @@ -1751,8 +1750,6 @@ continue; } } else if (inst.op.code == LOAD_GLOBAL) { -// Something outside this block might be the problem. -continue; int oparg = GET_OPARG(co, i, inst.op.arg); int index = LOAD_ATTR_NAME_INDEX(oparg); PyObject *name = GETITEM(co->co_names, index); From d6ed56070e98d558e6fe59b871c862abce2b0142 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Fri, 2 May 2025 21:29:32 -0600 Subject: [PATCH 13/22] Temporarily skip some code. --- Objects/codeobject.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Objects/codeobject.c b/Objects/codeobject.c index d43b1bb3131816..656da129201034 100644 --- a/Objects/codeobject.c +++ b/Objects/codeobject.c @@ -1716,7 +1716,6 @@ identify_unbound_names(PyThreadState *tstate, PyCodeObject *co, PyObject *globalsns, PyObject *builtinsns, struct co_unbound_counts *counts) { - return 0; // This function is inspired by inspect.getclosurevars(). // It would be nicer if we had something similar to co_localspluskinds, // but for co_names. @@ -1731,6 +1730,7 @@ identify_unbound_names(PyThreadState *tstate, PyCodeObject *co, assert(counts == NULL || counts->total == 0); struct co_unbound_counts unbound = {0}; Py_ssize_t len = Py_SIZE(co); +return 0; for (int i = 0; i < len; i++) { _Py_CODEUNIT inst = _Py_GetBaseCodeUnit(co, i); if (inst.op.code == LOAD_ATTR) { From 93377fe37717575cd3209ebf455c0e5b72894ba4 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Fri, 2 May 2025 21:42:23 -0600 Subject: [PATCH 14/22] Temporarily skip some code. --- Objects/codeobject.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Objects/codeobject.c b/Objects/codeobject.c index 656da129201034..ba749ed4817010 100644 --- a/Objects/codeobject.c +++ b/Objects/codeobject.c @@ -1730,8 +1730,8 @@ identify_unbound_names(PyThreadState *tstate, PyCodeObject *co, assert(counts == NULL || counts->total == 0); struct co_unbound_counts unbound = {0}; Py_ssize_t len = Py_SIZE(co); -return 0; for (int i = 0; i < len; i++) { +break; _Py_CODEUNIT inst = _Py_GetBaseCodeUnit(co, i); if (inst.op.code == LOAD_ATTR) { int oparg = GET_OPARG(co, i, inst.op.arg); From 170c354b6ca5f424af06037e50d51277e37e69bd Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Fri, 2 May 2025 23:12:34 -0600 Subject: [PATCH 15/22] Temporarily skip some code. --- Objects/codeobject.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Objects/codeobject.c b/Objects/codeobject.c index ba749ed4817010..65b6a17e2cb9c0 100644 --- a/Objects/codeobject.c +++ b/Objects/codeobject.c @@ -1781,9 +1781,9 @@ break; } } } -// if (counts != NULL) { -// *counts = unbound; -// } + if (counts != NULL) { + *counts = unbound; + } return 0; } From 01284043f25ca139632f445fa26778eb7700461e Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Sat, 3 May 2025 10:00:49 -0600 Subject: [PATCH 16/22] Temporarily skip some code. --- Objects/codeobject.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Objects/codeobject.c b/Objects/codeobject.c index 65b6a17e2cb9c0..706738316cdee8 100644 --- a/Objects/codeobject.c +++ b/Objects/codeobject.c @@ -1731,8 +1731,8 @@ identify_unbound_names(PyThreadState *tstate, PyCodeObject *co, struct co_unbound_counts unbound = {0}; Py_ssize_t len = Py_SIZE(co); for (int i = 0; i < len; i++) { -break; _Py_CODEUNIT inst = _Py_GetBaseCodeUnit(co, i); +continue; if (inst.op.code == LOAD_ATTR) { int oparg = GET_OPARG(co, i, inst.op.arg); int index = LOAD_ATTR_NAME_INDEX(oparg); From 4fa2ecfbcccb38e0c91f3b7447e483fe7ce39564 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Sat, 3 May 2025 13:45:05 -0600 Subject: [PATCH 17/22] Temporarily skip some code. --- Objects/codeobject.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Objects/codeobject.c b/Objects/codeobject.c index 706738316cdee8..7dc2985fb20250 100644 --- a/Objects/codeobject.c +++ b/Objects/codeobject.c @@ -1731,6 +1731,7 @@ identify_unbound_names(PyThreadState *tstate, PyCodeObject *co, struct co_unbound_counts unbound = {0}; Py_ssize_t len = Py_SIZE(co); for (int i = 0; i < len; i++) { +printf("%d\n", i); _Py_CODEUNIT inst = _Py_GetBaseCodeUnit(co, i); continue; if (inst.op.code == LOAD_ATTR) { @@ -1781,9 +1782,9 @@ continue; } } } - if (counts != NULL) { - *counts = unbound; - } +// if (counts != NULL) { +// *counts = unbound; +// } return 0; } From 4347b13454096eff271e4b07dec546c8222a12fa Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Sat, 3 May 2025 17:33:12 -0600 Subject: [PATCH 18/22] Print the ind=ex. --- Objects/codeobject.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Objects/codeobject.c b/Objects/codeobject.c index 7dc2985fb20250..0357299dc38dbe 100644 --- a/Objects/codeobject.c +++ b/Objects/codeobject.c @@ -1731,7 +1731,8 @@ identify_unbound_names(PyThreadState *tstate, PyCodeObject *co, struct co_unbound_counts unbound = {0}; Py_ssize_t len = Py_SIZE(co); for (int i = 0; i < len; i++) { -printf("%d\n", i); +fprintf(stderr, "%d\n", i); +fflush(stderr); _Py_CODEUNIT inst = _Py_GetBaseCodeUnit(co, i); continue; if (inst.op.code == LOAD_ATTR) { @@ -1782,9 +1783,9 @@ continue; } } } -// if (counts != NULL) { -// *counts = unbound; -// } + if (counts != NULL) { + *counts = unbound; + } return 0; } From c199564c3aad239b3eeed8de971c9788c4b79aca Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Sat, 3 May 2025 18:28:37 -0600 Subject: [PATCH 19/22] Print more stuff. --- Lib/test/test_code.py | 3 +++ Objects/codeobject.c | 2 ++ 2 files changed, 5 insertions(+) diff --git a/Lib/test/test_code.py b/Lib/test/test_code.py index 1b6dfe7c7890ad..943abcc6261ba9 100644 --- a/Lib/test/test_code.py +++ b/Lib/test/test_code.py @@ -966,6 +966,9 @@ def func_with_globals_and_builtins(): return callable(mod2), tuple(mods), list(mods), checks func = func_with_globals_and_builtins + dis.dis(func) + for i, instr in enumerate(dis.get_instructions(func)): + print(i, instr, end='') with self.subTest(f'{func} code'): expected = new_var_counts( purelocals=4, diff --git a/Objects/codeobject.c b/Objects/codeobject.c index 0357299dc38dbe..e191a851339c61 100644 --- a/Objects/codeobject.c +++ b/Objects/codeobject.c @@ -1730,6 +1730,8 @@ identify_unbound_names(PyThreadState *tstate, PyCodeObject *co, assert(counts == NULL || counts->total == 0); struct co_unbound_counts unbound = {0}; Py_ssize_t len = Py_SIZE(co); +fprintf(stderr, "\n"); +fprintf(stderr, "%s\n", PyUnicode_AsUTF8(co->co_name)); for (int i = 0; i < len; i++) { fprintf(stderr, "%d\n", i); fflush(stderr); From 88f7bebc0e25090d7537ef1571d92a22bc7a4411 Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Mon, 5 May 2025 09:37:10 -0600 Subject: [PATCH 20/22] Drop the temporary code. --- Lib/test/test_code.py | 3 --- Objects/codeobject.c | 5 ----- 2 files changed, 8 deletions(-) diff --git a/Lib/test/test_code.py b/Lib/test/test_code.py index 943abcc6261ba9..1b6dfe7c7890ad 100644 --- a/Lib/test/test_code.py +++ b/Lib/test/test_code.py @@ -966,9 +966,6 @@ def func_with_globals_and_builtins(): return callable(mod2), tuple(mods), list(mods), checks func = func_with_globals_and_builtins - dis.dis(func) - for i, instr in enumerate(dis.get_instructions(func)): - print(i, instr, end='') with self.subTest(f'{func} code'): expected = new_var_counts( purelocals=4, diff --git a/Objects/codeobject.c b/Objects/codeobject.c index e191a851339c61..0394817c636661 100644 --- a/Objects/codeobject.c +++ b/Objects/codeobject.c @@ -1730,13 +1730,8 @@ identify_unbound_names(PyThreadState *tstate, PyCodeObject *co, assert(counts == NULL || counts->total == 0); struct co_unbound_counts unbound = {0}; Py_ssize_t len = Py_SIZE(co); -fprintf(stderr, "\n"); -fprintf(stderr, "%s\n", PyUnicode_AsUTF8(co->co_name)); for (int i = 0; i < len; i++) { -fprintf(stderr, "%d\n", i); -fflush(stderr); _Py_CODEUNIT inst = _Py_GetBaseCodeUnit(co, i); -continue; if (inst.op.code == LOAD_ATTR) { int oparg = GET_OPARG(co, i, inst.op.arg); int index = LOAD_ATTR_NAME_INDEX(oparg); From c8877e45074045d9f48706380f2043914644f09a Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Mon, 5 May 2025 09:38:47 -0600 Subject: [PATCH 21/22] Respect op caches. --- Objects/codeobject.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Objects/codeobject.c b/Objects/codeobject.c index 0394817c636661..f3ad62748c44e8 100644 --- a/Objects/codeobject.c +++ b/Objects/codeobject.c @@ -1730,7 +1730,7 @@ identify_unbound_names(PyThreadState *tstate, PyCodeObject *co, assert(counts == NULL || counts->total == 0); struct co_unbound_counts unbound = {0}; Py_ssize_t len = Py_SIZE(co); - for (int i = 0; i < len; i++) { + for (int i = 0; i < len; i += _PyInstruction_GetLength(co, i)) { _Py_CODEUNIT inst = _Py_GetBaseCodeUnit(co, i); if (inst.op.code == LOAD_ATTR) { int oparg = GET_OPARG(co, i, inst.op.arg); @@ -1976,7 +1976,7 @@ _PyCode_ReturnsOnlyNone(PyCodeObject *co) // Walk the bytecode, looking for RETURN_VALUE. Py_ssize_t len = Py_SIZE(co); - for (int i = 0; i < len; i++) { + for (int i = 0; i < len; i += _PyInstruction_GetLength(co, i)) { _Py_CODEUNIT inst = _Py_GetBaseCodeUnit(co, i); if (IS_RETURN_OPCODE(inst.op.code)) { assert(i != 0); From 5b3c975c50fbe31f354134f33ab083ac6692215e Mon Sep 17 00:00:00 2001 From: Eric Snow Date: Mon, 5 May 2025 11:08:55 -0600 Subject: [PATCH 22/22] Add a critical section. --- Objects/codeobject.c | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/Objects/codeobject.c b/Objects/codeobject.c index f3ad62748c44e8..92eaf5e00bc7dd 100644 --- a/Objects/codeobject.c +++ b/Objects/codeobject.c @@ -1932,10 +1932,12 @@ _PyCode_SetUnboundVarCounts(PyThreadState *tstate, // Fill in unbound.globals and unbound.numattrs. struct co_unbound_counts unbound = {0}; - if (identify_unbound_names( + Py_BEGIN_CRITICAL_SECTION(co); + res = identify_unbound_names( tstate, co, globalnames, attrnames, globalsns, builtinsns, - &unbound) < 0) - { + &unbound); + Py_END_CRITICAL_SECTION(); + if (res < 0) { goto finally; } assert(unbound.numunknown == 0); @@ -1956,8 +1958,8 @@ _PyCode_SetUnboundVarCounts(PyThreadState *tstate, /* 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". */ -int -_PyCode_ReturnsOnlyNone(PyCodeObject *co) +static int +code_returns_only_none(PyCodeObject *co) { // Look up None in co_consts. Py_ssize_t nconsts = PyTuple_Size(co->co_consts); @@ -1994,6 +1996,16 @@ _PyCode_ReturnsOnlyNone(PyCodeObject *co) return 1; } +int +_PyCode_ReturnsOnlyNone(PyCodeObject *co) +{ + int res; + Py_BEGIN_CRITICAL_SECTION(co); + res = code_returns_only_none(co); + Py_END_CRITICAL_SECTION(); + return res; +} + #ifdef _Py_TIER2