Skip to content
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

gh-104240: return code unit metadata from codegen #104300

Merged
merged 2 commits into from
May 9, 2023
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
2 changes: 1 addition & 1 deletion Lib/test/support/bytecode_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def complete_insts_info(self, insts):
class CodegenTestCase(CompilationStepTestCase):

def generate_code(self, ast):
insts = compiler_codegen(ast, "my_file.py", 0)
insts, _ = compiler_codegen(ast, "my_file.py", 0)
return insts


Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_compiler_assemble.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def test_simple_expr(self):
'filename' : 'avg.py',
'name' : 'avg',
'qualname' : 'stats.avg',
'consts' : [2],
'consts' : {2 : 0},
'argcount' : 2,
'varnames' : {'x' : 0, 'y' : 1},
}
Expand Down
2 changes: 1 addition & 1 deletion Modules/_testinternalcapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ _testinternalcapi_assemble_code_object_impl(PyObject *module,
umd.u_cellvars = PyDict_GetItemString(metadata, "cellvars");
umd.u_freevars = PyDict_GetItemString(metadata, "freevars");

assert(PyList_Check(umd.u_consts));
assert(PyDict_Check(umd.u_consts));
assert(PyDict_Check(umd.u_names));
assert(PyDict_Check(umd.u_varnames));
assert(PyDict_Check(umd.u_cellvars));
Expand Down
50 changes: 47 additions & 3 deletions Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -7261,6 +7261,7 @@ _PyCompile_CodeGen(PyObject *ast, PyObject *filename, PyCompilerFlags *pflags,
int optimize, int compile_mode)
{
PyObject *res = NULL;
PyObject *metadata = NULL;

if (!PyAST_Check(ast)) {
PyErr_SetString(PyExc_TypeError, "expected an AST");
Expand All @@ -7287,14 +7288,53 @@ _PyCompile_CodeGen(PyObject *ast, PyObject *filename, PyCompilerFlags *pflags,
if (compiler_codegen(c, mod) < 0) {
goto finally;
}

_PyCompile_CodeUnitMetadata *umd = &c->u->u_metadata;
metadata = PyDict_New();
if (metadata == NULL) {
goto finally;
}
#define SET_MATADATA_ITEM(key, value) \
if (value != NULL) { \
if (PyDict_SetItemString(metadata, key, value) < 0) goto finally; \
}

SET_MATADATA_ITEM("name", umd->u_name);
SET_MATADATA_ITEM("qualname", umd->u_qualname);
SET_MATADATA_ITEM("consts", umd->u_consts);
SET_MATADATA_ITEM("names", umd->u_names);
SET_MATADATA_ITEM("varnames", umd->u_varnames);
SET_MATADATA_ITEM("cellvars", umd->u_cellvars);
SET_MATADATA_ITEM("freevars", umd->u_freevars);
#undef SET_MATADATA_ITEM

#define SET_MATADATA_INT(key, value) do { \
PyObject *v = PyLong_FromLong(value); \
if (v == NULL) goto finally; \
int res = PyDict_SetItemString(metadata, key, v); \
Py_XDECREF(v); \
if (res < 0) goto finally; \
} while (0);

SET_MATADATA_INT("argcount", umd->u_argcount);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like MSVC wants an explicit cast here.

SET_MATADATA_INT("posonlyargcount", umd->u_posonlyargcount);
SET_MATADATA_INT("kwonlyargcount", umd->u_kwonlyargcount);
#undef SET_MATADATA_INT

int addNone = mod->kind != Expression_kind;
if (add_return_at_end(c, addNone) < 0) {
return NULL;
goto finally;
}

res = instr_sequence_to_instructions(INSTR_SEQUENCE(c));
PyObject *insts = instr_sequence_to_instructions(INSTR_SEQUENCE(c));
if (insts == NULL) {
goto finally;
}
res = PyTuple_Pack(2, insts, metadata);
Py_DECREF(insts);

finally:
Py_XDECREF(metadata);
compiler_exit_scope(c);
compiler_free(c);
_PyArena_Free(arena);
Expand Down Expand Up @@ -7375,10 +7415,14 @@ _PyCompile_Assemble(_PyCompile_CodeUnitMetadata *umd, PyObject *filename,
goto error;
}

PyObject *consts = umd->u_consts;
PyObject *consts = consts_dict_keys_inorder(umd->u_consts);
if (consts == NULL) {
goto error;
}
iritkatriel marked this conversation as resolved.
Show resolved Hide resolved
co = _PyAssemble_MakeCodeObject(umd, const_cache,
consts, maxdepth, &optimized_instrs,
nlocalsplus, code_flags, filename);
Py_DECREF(consts);

error:
Py_DECREF(const_cache);
Expand Down