@@ -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
72977336finally :
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
73837427error :
73847428 Py_DECREF (const_cache );
0 commit comments