Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ca95edf

Browse filesBrowse files
authored
gh-104240: return code unit metadata from codegen (#104300)
1 parent c21f828 commit ca95edf
Copy full SHA for ca95edf

File tree

Expand file treeCollapse file tree

4 files changed

+50
-6
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+50
-6
lines changed

‎Lib/test/support/bytecode_helper.py

Copy file name to clipboardExpand all lines: Lib/test/support/bytecode_helper.py
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def complete_insts_info(self, insts):
124124
class CodegenTestCase(CompilationStepTestCase):
125125

126126
def generate_code(self, ast):
127-
insts = compiler_codegen(ast, "my_file.py", 0)
127+
insts, _ = compiler_codegen(ast, "my_file.py", 0)
128128
return insts
129129

130130

‎Lib/test/test_compiler_assemble.py

Copy file name to clipboardExpand all lines: Lib/test/test_compiler_assemble.py
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def test_simple_expr(self):
5252
'filename' : 'avg.py',
5353
'name' : 'avg',
5454
'qualname' : 'stats.avg',
55-
'consts' : [2],
55+
'consts' : {2 : 0},
5656
'argcount' : 2,
5757
'varnames' : {'x' : 0, 'y' : 1},
5858
}

‎Modules/_testinternalcapi.c

Copy file name to clipboardExpand all lines: Modules/_testinternalcapi.c
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -670,7 +670,7 @@ _testinternalcapi_assemble_code_object_impl(PyObject *module,
670670
umd.u_cellvars = PyDict_GetItemString(metadata, "cellvars");
671671
umd.u_freevars = PyDict_GetItemString(metadata, "freevars");
672672

673-
assert(PyList_Check(umd.u_consts));
673+
assert(PyDict_Check(umd.u_consts));
674674
assert(PyDict_Check(umd.u_names));
675675
assert(PyDict_Check(umd.u_varnames));
676676
assert(PyDict_Check(umd.u_cellvars));

‎Python/compile.c

Copy file name to clipboardExpand all lines: Python/compile.c
+47-3Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7261,6 +7261,7 @@ _PyCompile_CodeGen(PyObject *ast, PyObject *filename, PyCompilerFlags *pflags,
72617261
int optimize, int compile_mode)
72627262
{
72637263
PyObject *res = NULL;
7264+
PyObject *metadata = NULL;
72647265

72657266
if (!PyAST_Check(ast)) {
72667267
PyErr_SetString(PyExc_TypeError, "expected an AST");
@@ -7287,14 +7288,53 @@ _PyCompile_CodeGen(PyObject *ast, PyObject *filename, PyCompilerFlags *pflags,
72877288
if (compiler_codegen(c, mod) < 0) {
72887289
goto finally;
72897290
}
7291+
7292+
_PyCompile_CodeUnitMetadata *umd = &c->u->u_metadata;
7293+
metadata = PyDict_New();
7294+
if (metadata == NULL) {
7295+
goto finally;
7296+
}
7297+
#define SET_MATADATA_ITEM(key, value) \
7298+
if (value != NULL) { \
7299+
if (PyDict_SetItemString(metadata, key, value) < 0) goto finally; \
7300+
}
7301+
7302+
SET_MATADATA_ITEM("name", umd->u_name);
7303+
SET_MATADATA_ITEM("qualname", umd->u_qualname);
7304+
SET_MATADATA_ITEM("consts", umd->u_consts);
7305+
SET_MATADATA_ITEM("names", umd->u_names);
7306+
SET_MATADATA_ITEM("varnames", umd->u_varnames);
7307+
SET_MATADATA_ITEM("cellvars", umd->u_cellvars);
7308+
SET_MATADATA_ITEM("freevars", umd->u_freevars);
7309+
#undef SET_MATADATA_ITEM
7310+
7311+
#define SET_MATADATA_INT(key, value) do { \
7312+
PyObject *v = PyLong_FromLong((long)value); \
7313+
if (v == NULL) goto finally; \
7314+
int res = PyDict_SetItemString(metadata, key, v); \
7315+
Py_XDECREF(v); \
7316+
if (res < 0) goto finally; \
7317+
} while (0);
7318+
7319+
SET_MATADATA_INT("argcount", umd->u_argcount);
7320+
SET_MATADATA_INT("posonlyargcount", umd->u_posonlyargcount);
7321+
SET_MATADATA_INT("kwonlyargcount", umd->u_kwonlyargcount);
7322+
#undef SET_MATADATA_INT
7323+
72907324
int addNone = mod->kind != Expression_kind;
72917325
if (add_return_at_end(c, addNone) < 0) {
7292-
return NULL;
7326+
goto finally;
72937327
}
72947328

7295-
res = instr_sequence_to_instructions(INSTR_SEQUENCE(c));
7329+
PyObject *insts = instr_sequence_to_instructions(INSTR_SEQUENCE(c));
7330+
if (insts == NULL) {
7331+
goto finally;
7332+
}
7333+
res = PyTuple_Pack(2, insts, metadata);
7334+
Py_DECREF(insts);
72967335

72977336
finally:
7337+
Py_XDECREF(metadata);
72987338
compiler_exit_scope(c);
72997339
compiler_free(c);
73007340
_PyArena_Free(arena);
@@ -7375,10 +7415,14 @@ _PyCompile_Assemble(_PyCompile_CodeUnitMetadata *umd, PyObject *filename,
73757415
goto error;
73767416
}
73777417

7378-
PyObject *consts = umd->u_consts;
7418+
PyObject *consts = consts_dict_keys_inorder(umd->u_consts);
7419+
if (consts == NULL) {
7420+
goto error;
7421+
}
73797422
co = _PyAssemble_MakeCodeObject(umd, const_cache,
73807423
consts, maxdepth, &optimized_instrs,
73817424
nlocalsplus, code_flags, filename);
7425+
Py_DECREF(consts);
73827426

73837427
error:
73847428
Py_DECREF(const_cache);

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.