@@ -7261,6 +7261,7 @@ _PyCompile_CodeGen(PyObject *ast, PyObject *filename, PyCompilerFlags *pflags,
7261
7261
int optimize , int compile_mode )
7262
7262
{
7263
7263
PyObject * res = NULL ;
7264
+ PyObject * metadata = NULL ;
7264
7265
7265
7266
if (!PyAST_Check (ast )) {
7266
7267
PyErr_SetString (PyExc_TypeError , "expected an AST" );
@@ -7287,14 +7288,53 @@ _PyCompile_CodeGen(PyObject *ast, PyObject *filename, PyCompilerFlags *pflags,
7287
7288
if (compiler_codegen (c , mod ) < 0 ) {
7288
7289
goto finally ;
7289
7290
}
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(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
+
7290
7324
int addNone = mod -> kind != Expression_kind ;
7291
7325
if (add_return_at_end (c , addNone ) < 0 ) {
7292
- return NULL ;
7326
+ goto finally ;
7293
7327
}
7294
7328
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 );
7296
7335
7297
7336
finally :
7337
+ Py_XDECREF (metadata );
7298
7338
compiler_exit_scope (c );
7299
7339
compiler_free (c );
7300
7340
_PyArena_Free (arena );
@@ -7375,10 +7415,14 @@ _PyCompile_Assemble(_PyCompile_CodeUnitMetadata *umd, PyObject *filename,
7375
7415
goto error ;
7376
7416
}
7377
7417
7378
- PyObject * consts = umd -> u_consts ;
7418
+ PyObject * consts = consts_dict_keys_inorder (umd -> u_consts );
7419
+ if (consts == NULL ) {
7420
+ goto error ;
7421
+ }
7379
7422
co = _PyAssemble_MakeCodeObject (umd , const_cache ,
7380
7423
consts , maxdepth , & optimized_instrs ,
7381
7424
nlocalsplus , code_flags , filename );
7425
+ Py_DECREF (consts );
7382
7426
7383
7427
error :
7384
7428
Py_DECREF (const_cache );
0 commit comments