Skip to content

Latest commit

 

History

History
1963 lines (1784 loc) · 56.7 KB

compile.c

File metadata and controls

1963 lines (1784 loc) · 56.7 KB
 
Oct 20, 2005
Oct 20, 2005
1
/*
2
* This file compiles an abstract syntax tree (AST) into Python bytecode.
3
*
Mar 23, 2021
Mar 23, 2021
4
* The primary entry point is _PyAST_Compile(), which returns a
Oct 20, 2005
Oct 20, 2005
5
* PyCodeObject. The compiler makes several passes to build the code
6
* object:
7
* 1. Checks for future statements. See future.c
May 9, 2010
May 9, 2010
8
* 2. Builds a symbol table. See symtable.c.
Sep 9, 2024
Sep 9, 2024
9
* 3. Generate an instruction sequence. See compiler_mod() in this file, which
10
* calls functions from codegen.c.
Apr 11, 2023
Apr 11, 2023
11
* 4. Generate a control flow graph and run optimizations on it. See flowgraph.c.
12
* 5. Assemble the basic blocks into final code. See optimize_and_assemble() in
13
* this file, and assembler.c.
Oct 20, 2005
Oct 20, 2005
14
*
Sep 9, 2024
Sep 9, 2024
15
*/
Feb 26, 2021
Feb 26, 2021
16
Sep 9, 2024
Sep 9, 2024
17
#include "Python.h"
Mar 17, 2025
Mar 17, 2025
18
#include "pycore_ast.h" // PyAST_Check()
Mar 17, 2025
Mar 17, 2025
19
#include "pycore_code.h"
Sep 9, 2024
Sep 9, 2024
20
#include "pycore_compile.h"
Mar 17, 2025
Mar 17, 2025
21
#include "pycore_flowgraph.h" // _PyCfg_FromInstructionSequence()
Sep 9, 2024
Sep 9, 2024
22
#include "pycore_pystate.h" // _Py_GetConfig()
Mar 17, 2025
Mar 17, 2025
23
#include "pycore_runtime.h" // _Py_ID()
Sep 9, 2024
Sep 9, 2024
24
#include "pycore_setobject.h" // _PySet_NextEntry()
Mar 17, 2025
Mar 17, 2025
25
#include "pycore_stats.h"
Mar 11, 2026
Mar 11, 2026
26
#include "pycore_tuple.h" // _PyTuple_FromPair
Mar 17, 2025
Mar 17, 2025
27
#include "pycore_unicodeobject.h" // _PyUnicode_EqualToASCIIString()
May 2, 2021
May 2, 2021
28
Sep 9, 2024
Sep 9, 2024
29
#include "cpython/code.h"
Feb 26, 2021
Feb 26, 2021
30
Feb 28, 2025
Feb 28, 2025
31
#include <stdbool.h>
32
Mar 17, 2025
Mar 17, 2025
33
Sep 9, 2024
Sep 9, 2024
34
#undef SUCCESS
35
#undef ERROR
36
#define SUCCESS 0
37
#define ERROR -1
Sep 1, 2022
Sep 1, 2022
38
Sep 9, 2024
Sep 9, 2024
39
#define RETURN_IF_ERROR(X) \
Nov 7, 2024
Nov 7, 2024
40
do { \
41
if ((X) == -1) { \
42
return ERROR; \
43
} \
44
} while (0)
Sep 1, 2022
Sep 1, 2022
45
Sep 9, 2024
Sep 9, 2024
46
typedef _Py_SourceLocation location;
47
typedef _PyJumpTargetLabel jump_target_label;
48
typedef _PyInstructionSequence instr_sequence;
49
typedef struct _PyCfgBuilder cfg_builder;
50
typedef _PyCompile_FBlockInfo fblockinfo;
51
typedef enum _PyCompile_FBlockType fblocktype;
Aug 22, 2024
Aug 22, 2024
52
Sep 2, 2024
Sep 2, 2024
53
/* The following items change on entry and exit of code blocks.
54
They must be saved and restored when returning to a block.
55
*/
56
struct compiler_unit {
57
PySTEntryObject *u_ste;
58
59
int u_scope_type;
60
61
PyObject *u_private; /* for private name mangling */
62
PyObject *u_static_attributes; /* for class: attributes accessed via self.X */
63
PyObject *u_deferred_annotations; /* AnnAssign nodes deferred to the end of compilation */
Mar 26, 2025
Mar 26, 2025
64
PyObject *u_conditional_annotation_indices; /* indices of annotations that are conditionally executed (or -1 for unconditional annotations) */
65
long u_next_conditional_annotation_index; /* index of the next conditional annotation */
Sep 2, 2024
Sep 2, 2024
66
67
instr_sequence *u_instr_sequence; /* codegen output */
Apr 28, 2025
Apr 28, 2025
68
instr_sequence *u_stashed_instr_sequence; /* temporarily stashed parent instruction sequence */
Sep 2, 2024
Sep 2, 2024
69
70
int u_nfblocks;
Mar 26, 2025
Mar 26, 2025
71
int u_in_conditional_block;
Sep 2, 2024
Sep 2, 2024
72
Sep 9, 2024
Sep 9, 2024
73
_PyCompile_FBlockInfo u_fblock[CO_MAXBLOCKS];
Sep 2, 2024
Sep 2, 2024
74
75
_PyCompile_CodeUnitMetadata u_metadata;
76
};
77
78
/* This struct captures the global state of a compilation.
79
80
The u pointer points to the current compilation unit, while units
81
for enclosing blocks are stored in c_stack. The u and c_stack are
Sep 9, 2024
Sep 9, 2024
82
managed by _PyCompile_EnterScope() and _PyCompile_ExitScope().
Sep 2, 2024
Sep 2, 2024
83
84
Note that we don't track recursion levels during compilation - the
85
task of detecting and rejecting excessive levels of nesting is
86
handled by the symbol analysis pass.
87
88
*/
89
Sep 9, 2024
Sep 9, 2024
90
typedef struct _PyCompiler {
Sep 2, 2024
Sep 2, 2024
91
PyObject *c_filename;
92
struct symtable *c_st;
93
_PyFutureFeatures c_future; /* module's __future__ */
94
PyCompilerFlags c_flags;
95
96
int c_optimize; /* optimization level */
97
int c_interactive; /* true if in interactive mode */
98
PyObject *c_const_cache; /* Python dict holding all constants,
99
including names tuple */
100
struct compiler_unit *u; /* compiler state for current block */
101
PyObject *c_stack; /* Python list holding compiler_unit ptrs */
102
103
bool c_save_nested_seqs; /* if true, construct recursive instruction sequences
104
* (including instructions for nested code objects)
105
*/
Oct 14, 2025
Oct 14, 2025
106
int c_disable_warning;
Nov 13, 2025
Nov 13, 2025
107
PyObject *c_module;
Sep 9, 2024
Sep 9, 2024
108
} compiler;
Sep 2, 2024
Sep 2, 2024
109
Aug 30, 2024
Aug 30, 2024
110
static int
Sep 9, 2024
Sep 9, 2024
111
compiler_setup(compiler *c, mod_ty mod, PyObject *filename,
Nov 13, 2025
Nov 13, 2025
112
PyCompilerFlags *flags, int optimize, PyArena *arena,
113
PyObject *module)
Jul 8, 2024
Jul 8, 2024
114
{
Aug 30, 2024
Aug 30, 2024
115
PyCompilerFlags local_flags = _PyCompilerFlags_INIT;
Jul 8, 2024
Jul 8, 2024
116
Aug 30, 2024
Aug 30, 2024
117
c->c_const_cache = PyDict_New();
118
if (!c->c_const_cache) {
119
return ERROR;
120
}
Jul 10, 2024
Jul 10, 2024
121
Aug 30, 2024
Aug 30, 2024
122
c->c_stack = PyList_New(0);
123
if (!c->c_stack) {
124
return ERROR;
125
}
Jul 10, 2024
Jul 10, 2024
126
Aug 30, 2024
Aug 30, 2024
127
c->c_filename = Py_NewRef(filename);
128
if (!_PyFuture_FromAST(mod, filename, &c->c_future)) {
129
return ERROR;
130
}
Nov 13, 2025
Nov 13, 2025
131
c->c_module = Py_XNewRef(module);
Aug 30, 2024
Aug 30, 2024
132
if (!flags) {
133
flags = &local_flags;
134
}
135
int merged = c->c_future.ff_features | flags->cf_flags;
136
c->c_future.ff_features = merged;
137
flags->cf_flags = merged;
138
c->c_flags = *flags;
139
c->c_optimize = (optimize == -1) ? _Py_GetConfig()->optimization_level : optimize;
140
c->c_save_nested_seqs = false;
Jul 10, 2024
Jul 10, 2024
141
Nov 13, 2025
Nov 13, 2025
142
if (!_PyAST_Preprocess(mod, arena, filename, c->c_optimize, merged,
143
0, 1, module))
144
{
Aug 30, 2024
Aug 30, 2024
145
return ERROR;
146
}
147
c->c_st = _PySymtable_Build(mod, filename, &c->c_future);
148
if (c->c_st == NULL) {
149
if (!PyErr_Occurred()) {
150
PyErr_SetString(PyExc_SystemError, "no symtable");
151
}
152
return ERROR;
153
}
154
return SUCCESS;
Jul 10, 2024
Jul 10, 2024
155
}
156
Sep 9, 2024
Sep 9, 2024
157
static void
158
compiler_free(compiler *c)
159
{
160
if (c->c_st) {
161
_PySymtable_Free(c->c_st);
162
}
163
Py_XDECREF(c->c_filename);
Nov 13, 2025
Nov 13, 2025
164
Py_XDECREF(c->c_module);
Sep 9, 2024
Sep 9, 2024
165
Py_XDECREF(c->c_const_cache);
166
Py_XDECREF(c->c_stack);
167
PyMem_Free(c);
168
}
169
170
static compiler*
Aug 30, 2024
Aug 30, 2024
171
new_compiler(mod_ty mod, PyObject *filename, PyCompilerFlags *pflags,
Nov 13, 2025
Nov 13, 2025
172
int optimize, PyArena *arena, PyObject *module)
Aug 22, 2024
Aug 22, 2024
173
{
Sep 9, 2024
Sep 9, 2024
174
compiler *c = PyMem_Calloc(1, sizeof(compiler));
Aug 30, 2024
Aug 30, 2024
175
if (c == NULL) {
Jul 2, 2026
Jul 2, 2026
176
PyErr_NoMemory();
Aug 30, 2024
Aug 30, 2024
177
return NULL;
178
}
Nov 13, 2025
Nov 13, 2025
179
if (compiler_setup(c, mod, filename, pflags, optimize, arena, module) < 0) {
Aug 30, 2024
Aug 30, 2024
180
compiler_free(c);
181
return NULL;
182
}
183
return c;
Aug 22, 2024
Aug 22, 2024
184
}
185
Aug 30, 2024
Aug 30, 2024
186
static void
187
compiler_unit_free(struct compiler_unit *u)
188
{
189
Py_CLEAR(u->u_instr_sequence);
Apr 28, 2025
Apr 28, 2025
190
Py_CLEAR(u->u_stashed_instr_sequence);
Aug 30, 2024
Aug 30, 2024
191
Py_CLEAR(u->u_ste);
192
Py_CLEAR(u->u_metadata.u_name);
193
Py_CLEAR(u->u_metadata.u_qualname);
194
Py_CLEAR(u->u_metadata.u_consts);
195
Py_CLEAR(u->u_metadata.u_names);
196
Py_CLEAR(u->u_metadata.u_varnames);
197
Py_CLEAR(u->u_metadata.u_freevars);
198
Py_CLEAR(u->u_metadata.u_cellvars);
199
Py_CLEAR(u->u_metadata.u_fasthidden);
200
Py_CLEAR(u->u_private);
201
Py_CLEAR(u->u_static_attributes);
202
Py_CLEAR(u->u_deferred_annotations);
Mar 26, 2025
Mar 26, 2025
203
Py_CLEAR(u->u_conditional_annotation_indices);
Aug 30, 2024
Aug 30, 2024
204
PyMem_Free(u);
205
}
206
207
#define CAPSULE_NAME "compile.c compiler unit"
208
Sep 9, 2024
Sep 9, 2024
209
int
210
_PyCompile_MaybeAddStaticAttributeToClass(compiler *c, expr_ty e)
Aug 30, 2024
Aug 30, 2024
211
{
212
assert(e->kind == Attribute_kind);
213
expr_ty attr_value = e->v.Attribute.value;
214
if (attr_value->kind != Name_kind ||
215
e->v.Attribute.ctx != Store ||
216
!_PyUnicode_EqualToASCIIString(attr_value->v.Name.id, "self"))
217
{
218
return SUCCESS;
219
}
220
Py_ssize_t stack_size = PyList_GET_SIZE(c->c_stack);
221
for (Py_ssize_t i = stack_size - 1; i >= 0; i--) {
222
PyObject *capsule = PyList_GET_ITEM(c->c_stack, i);
223
struct compiler_unit *u = (struct compiler_unit *)PyCapsule_GetPointer(
224
capsule, CAPSULE_NAME);
225
assert(u);
Sep 9, 2024
Sep 9, 2024
226
if (u->u_scope_type == COMPILE_SCOPE_CLASS) {
Aug 30, 2024
Aug 30, 2024
227
assert(u->u_static_attributes);
228
RETURN_IF_ERROR(PySet_Add(u->u_static_attributes, e->v.Attribute.attr));
229
break;
230
}
231
}
232
return SUCCESS;
233
}
234
Aug 29, 2026
Aug 29, 2026
235
int
236
_PyCompile_SetQualname(compiler *c)
Aug 30, 2024
Aug 30, 2024
237
{
238
Py_ssize_t stack_size;
239
struct compiler_unit *u = c->u;
240
PyObject *name, *base;
241
Aug 29, 2026
Aug 29, 2026
242
if (u->u_scope_type == COMPILE_SCOPE_MODULE) {
243
return SUCCESS;
244
}
245
Aug 30, 2024
Aug 30, 2024
246
base = NULL;
247
stack_size = PyList_GET_SIZE(c->c_stack);
248
assert(stack_size >= 1);
249
if (stack_size > 1) {
250
int scope, force_global = 0;
251
struct compiler_unit *parent;
252
PyObject *mangled, *capsule;
253
254
capsule = PyList_GET_ITEM(c->c_stack, stack_size - 1);
255
parent = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME);
256
assert(parent);
Sep 9, 2024
Sep 9, 2024
257
if (parent->u_scope_type == COMPILE_SCOPE_ANNOTATIONS) {
Aug 30, 2024
Aug 30, 2024
258
/* The parent is an annotation scope, so we need to
259
look at the grandparent. */
260
if (stack_size == 2) {
261
// If we're immediately within the module, we can skip
262
// the rest and just set the qualname to be the same as name.
263
u->u_metadata.u_qualname = Py_NewRef(u->u_metadata.u_name);
264
return SUCCESS;
265
}
266
capsule = PyList_GET_ITEM(c->c_stack, stack_size - 2);
267
parent = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME);
268
assert(parent);
269
}
270
Sep 9, 2024
Sep 9, 2024
271
if (u->u_scope_type == COMPILE_SCOPE_FUNCTION
272
|| u->u_scope_type == COMPILE_SCOPE_ASYNC_FUNCTION
273
|| u->u_scope_type == COMPILE_SCOPE_CLASS) {
Aug 30, 2024
Aug 30, 2024
274
assert(u->u_metadata.u_name);
275
mangled = _Py_Mangle(parent->u_private, u->u_metadata.u_name);
276
if (!mangled) {
277
return ERROR;
278
}
279
280
scope = _PyST_GetScope(parent->u_ste, mangled);
281
Py_DECREF(mangled);
282
RETURN_IF_ERROR(scope);
283
assert(scope != GLOBAL_IMPLICIT);
284
if (scope == GLOBAL_EXPLICIT)
285
force_global = 1;
286
}
287
Sep 9, 2024
Sep 9, 2024
288
if (!force_global) {
289
if (parent->u_scope_type == COMPILE_SCOPE_FUNCTION
290
|| parent->u_scope_type == COMPILE_SCOPE_ASYNC_FUNCTION
291
|| parent->u_scope_type == COMPILE_SCOPE_LAMBDA)
292
{
293
_Py_DECLARE_STR(dot_locals, ".<locals>");
294
base = PyUnicode_Concat(parent->u_metadata.u_qualname,
295
&_Py_STR(dot_locals));
296
if (base == NULL) {
297
return ERROR;
298
}
299
}
300
else {
301
base = Py_NewRef(parent->u_metadata.u_qualname);
302
}
303
}
Apr 16, 2026
Apr 16, 2026
304
if (u->u_ste->ste_function_name != NULL) {
305
PyObject *tmp = base;
306
base = PyUnicode_FromFormat("%U.%U",
307
base,
308
u->u_ste->ste_function_name);
309
Py_DECREF(tmp);
310
if (base == NULL) {
311
return ERROR;
312
}
313
}
314
}
315
else if (u->u_ste->ste_function_name != NULL) {
316
base = Py_NewRef(u->u_ste->ste_function_name);
Sep 9, 2024
Sep 9, 2024
317
}
318
319
if (base != NULL) {
320
name = PyUnicode_Concat(base, _Py_LATIN1_CHR('.'));
321
Py_DECREF(base);
322
if (name == NULL) {
323
return ERROR;
324
}
325
PyUnicode_Append(&name, u->u_metadata.u_name);
326
if (name == NULL) {
327
return ERROR;
328
}
329
}
330
else {
331
name = Py_NewRef(u->u_metadata.u_name);
332
}
Oct 13, 2026
Oct 13, 2026
333
// The qualified name is interned when the code object is created,
334
// whatever its content. The body of a class also has it as a constant,
335
// and intern_final_constants() only interns the constants selected by
336
// _PyCode_ShouldInternString(). Intern the other qualified names now,
337
// so that the same object is always used for both.
338
if (!_PyCode_ShouldInternString(name)) {
339
_PyUnicode_InternMortal(_PyInterpreterState_GET(), &name);
340
}
Sep 9, 2024
Sep 9, 2024
341
u->u_metadata.u_qualname = name;
342
343
return SUCCESS;
344
}
345
346
/* Merge const *o* and return constant key object.
347
* If recursive, insert all elements if o is a tuple or frozen set.
348
*/
349
static PyObject*
350
const_cache_insert(PyObject *const_cache, PyObject *o, bool recursive)
351
{
352
assert(PyDict_CheckExact(const_cache));
353
// None and Ellipsis are immortal objects, and key is the singleton.
354
// No need to merge object and key.
355
if (o == Py_None || o == Py_Ellipsis) {
356
return o;
357
}
358
359
PyObject *key = _PyCode_ConstantKey(o);
360
if (key == NULL) {
361
return NULL;
362
}
363
364
PyObject *t;
365
int res = PyDict_SetDefaultRef(const_cache, key, key, &t);
366
if (res != 0) {
367
// o was not inserted into const_cache. t is either the existing value
368
// or NULL (on error).
369
Py_DECREF(key);
370
return t;
371
}
372
Py_DECREF(t);
373
374
if (!recursive) {
375
return key;
376
}
377
378
// We registered o in const_cache.
379
// When o is a tuple or frozenset, we want to merge its
380
// items too.
381
if (PyTuple_CheckExact(o)) {
382
Py_ssize_t len = PyTuple_GET_SIZE(o);
383
for (Py_ssize_t i = 0; i < len; i++) {
384
PyObject *item = PyTuple_GET_ITEM(o, i);
385
PyObject *u = const_cache_insert(const_cache, item, recursive);
386
if (u == NULL) {
387
Py_DECREF(key);
388
return NULL;
389
}
390
391
// See _PyCode_ConstantKey()
392
PyObject *v; // borrowed
393
if (PyTuple_CheckExact(u)) {
394
v = PyTuple_GET_ITEM(u, 1);
395
}
396
else {
397
v = u;
398
}
399
if (v != item) {
400
PyTuple_SET_ITEM(o, i, Py_NewRef(v));
401
Py_DECREF(item);
402
}
403
404
Py_DECREF(u);
405
}
406
}
407
else if (PyFrozenSet_CheckExact(o)) {
408
// *key* is tuple. And its first item is frozenset of
409
// constant keys.
410
// See _PyCode_ConstantKey() for detail.
411
assert(PyTuple_CheckExact(key));
412
assert(PyTuple_GET_SIZE(key) == 2);
413
414
Py_ssize_t len = PySet_GET_SIZE(o);
415
if (len == 0) { // empty frozenset should not be re-created.
416
return key;
417
}
418
PyObject *tuple = PyTuple_New(len);
419
if (tuple == NULL) {
420
Py_DECREF(key);
421
return NULL;
422
}
423
Py_ssize_t i = 0, pos = 0;
424
PyObject *item;
425
Py_hash_t hash;
426
while (_PySet_NextEntry(o, &pos, &item, &hash)) {
427
PyObject *k = const_cache_insert(const_cache, item, recursive);
428
if (k == NULL) {
429
Py_DECREF(tuple);
430
Py_DECREF(key);
431
return NULL;
432
}
433
PyObject *u;
434
if (PyTuple_CheckExact(k)) {
435
u = Py_NewRef(PyTuple_GET_ITEM(k, 1));
436
Py_DECREF(k);
Aug 30, 2024
Aug 30, 2024
437
}
438
else {
Sep 9, 2024
Sep 9, 2024
439
u = k;
Aug 30, 2024
Aug 30, 2024
440
}
Sep 9, 2024
Sep 9, 2024
441
PyTuple_SET_ITEM(tuple, i, u); // Steals reference of u.
442
i++;
443
}
444
445
// Instead of rewriting o, we create new frozenset and embed in the
446
// key tuple. Caller should get merged frozenset from the key tuple.
447
PyObject *new = PyFrozenSet_New(tuple);
448
Py_DECREF(tuple);
449
if (new == NULL) {
450
Py_DECREF(key);
451
return NULL;
Aug 30, 2024
Aug 30, 2024
452
}
Sep 9, 2024
Sep 9, 2024
453
assert(PyTuple_GET_ITEM(key, 1) == o);
454
Py_DECREF(o);
455
PyTuple_SET_ITEM(key, 1, new);
Aug 30, 2024
Aug 30, 2024
456
}
457
Sep 9, 2024
Sep 9, 2024
458
return key;
459
}
460
461
static PyObject*
462
merge_consts_recursive(PyObject *const_cache, PyObject *o)
463
{
464
return const_cache_insert(const_cache, o, true);
465
}
466
467
Py_ssize_t
468
_PyCompile_DictAddObj(PyObject *dict, PyObject *o)
469
{
470
PyObject *v;
471
Py_ssize_t arg;
472
473
if (PyDict_GetItemRef(dict, o, &v) < 0) {
474
return ERROR;
475
}
476
if (!v) {
477
arg = PyDict_GET_SIZE(dict);
478
v = PyLong_FromSsize_t(arg);
479
if (!v) {
Aug 30, 2024
Aug 30, 2024
480
return ERROR;
481
}
Sep 9, 2024
Sep 9, 2024
482
if (PyDict_SetItem(dict, o, v) < 0) {
483
Py_DECREF(v);
Aug 30, 2024
Aug 30, 2024
484
return ERROR;
485
}
486
}
Sep 9, 2024
Sep 9, 2024
487
else
488
arg = PyLong_AsLong(v);
489
Py_DECREF(v);
490
return arg;
Aug 30, 2024
Aug 30, 2024
491
}
492
Sep 9, 2024
Sep 9, 2024
493
Py_ssize_t
494
_PyCompile_AddConst(compiler *c, PyObject *o)
Aug 30, 2024
Aug 30, 2024
495
{
496
PyObject *key = merge_consts_recursive(c->c_const_cache, o);
497
if (key == NULL) {
498
return ERROR;
499
}
500
Sep 9, 2024
Sep 9, 2024
501
Py_ssize_t arg = _PyCompile_DictAddObj(c->u->u_metadata.u_consts, key);
Aug 30, 2024
Aug 30, 2024
502
Py_DECREF(key);
503
return arg;
504
}
505
506
static PyObject *
507
list2dict(PyObject *list)
508
{
509
Py_ssize_t i, n;
510
PyObject *v, *k;
511
PyObject *dict = PyDict_New();
512
if (!dict) return NULL;
513
514
n = PyList_Size(list);
515
for (i = 0; i < n; i++) {
516
v = PyLong_FromSsize_t(i);
517
if (!v) {
518
Py_DECREF(dict);
519
return NULL;
520
}
521
k = PyList_GET_ITEM(list, i);
522
if (PyDict_SetItem(dict, k, v) < 0) {
523
Py_DECREF(v);
524
Py_DECREF(dict);
525
return NULL;
526
}
527
Py_DECREF(v);
528
}
529
return dict;
530
}
531
532
/* Return new dict containing names from src that match scope(s).
533
534
src is a symbol table dictionary. If the scope of a name matches
535
either scope_type or flag is set, insert it into the new dict. The
536
values are integers, starting at offset and increasing by one for
537
each key.
538
*/
539
540
static PyObject *
541
dictbytype(PyObject *src, int scope_type, int flag, Py_ssize_t offset)
542
{
543
Py_ssize_t i = offset, num_keys, key_i;
544
PyObject *k, *v, *dest = PyDict_New();
545
PyObject *sorted_keys;
546
547
assert(offset >= 0);
548
if (dest == NULL)
549
return NULL;
550
551
/* Sort the keys so that we have a deterministic order on the indexes
552
saved in the returned dictionary. These indexes are used as indexes
553
into the free and cell var storage. Therefore if they aren't
554
deterministic, then the generated bytecode is not deterministic.
555
*/
556
sorted_keys = PyDict_Keys(src);
557
if (sorted_keys == NULL) {
558
Py_DECREF(dest);
559
return NULL;
560
}
561
if (PyList_Sort(sorted_keys) != 0) {
562
Py_DECREF(sorted_keys);
563
Py_DECREF(dest);
564
return NULL;
565
}
566
num_keys = PyList_GET_SIZE(sorted_keys);
567
568
for (key_i = 0; key_i < num_keys; key_i++) {
569
k = PyList_GET_ITEM(sorted_keys, key_i);
570
v = PyDict_GetItemWithError(src, k);
571
if (!v) {
572
if (!PyErr_Occurred()) {
573
PyErr_SetObject(PyExc_KeyError, k);
574
}
575
Py_DECREF(sorted_keys);
576
Py_DECREF(dest);
577
return NULL;
578
}
579
long vi = PyLong_AsLong(v);
580
if (vi == -1 && PyErr_Occurred()) {
581
Py_DECREF(sorted_keys);
582
Py_DECREF(dest);
583
return NULL;
584
}
585
if (SYMBOL_TO_SCOPE(vi) == scope_type || vi & flag) {
586
PyObject *item = PyLong_FromSsize_t(i);
587
if (item == NULL) {
588
Py_DECREF(sorted_keys);
589
Py_DECREF(dest);
590
return NULL;
591
}
592
i++;
593
if (PyDict_SetItem(dest, k, item) < 0) {
594
Py_DECREF(sorted_keys);
595
Py_DECREF(item);
596
Py_DECREF(dest);
597
return NULL;
598
}
599
Py_DECREF(item);
600
}
601
}
602
Py_DECREF(sorted_keys);
603
return dest;
604
}
605
Sep 23, 2026
Sep 23, 2026
606
static int
607
add_cell_names_from_symbols(PyObject *symbols, PyObject *names)
608
{
609
Py_ssize_t pos = 0;
610
PyObject *k, *v;
611
while (PyDict_Next(symbols, &pos, &k, &v)) {
612
long flags = PyLong_AsLong(v);
613
if (flags == -1 && PyErr_Occurred()) {
614
return ERROR;
615
}
616
if (SYMBOL_TO_SCOPE(flags) == CELL) {
617
if (PySet_Add(names, k) < 0) {
618
return ERROR;
619
}
620
}
621
}
622
return SUCCESS;
623
}
624
625
static int
626
add_inlined_comprehension_cell_names(PySTEntryObject *ste, PyObject *names)
627
{
628
for (Py_ssize_t i = 0; i < PyList_GET_SIZE(ste->ste_children); i++) {
629
PySTEntryObject *child =
630
(PySTEntryObject *)PyList_GET_ITEM(ste->ste_children, i);
631
if (child->ste_type != InlinedComprehensionBlock) {
632
continue;
633
}
634
if (add_cell_names_from_symbols(child->ste_symbols, names) < 0) {
635
return ERROR;
636
}
637
if (add_inlined_comprehension_cell_names(child, names) < 0) {
638
return ERROR;
639
}
640
}
641
return SUCCESS;
642
}
643
644
/* Cells of the shared unit: this table's CELL names, plus cells that live
645
* only on inlined comprehension children. */
646
static PyObject *
647
compiler_cellvars(PySTEntryObject *ste)
648
{
649
PyObject *names = PySet_New(NULL);
650
if (names == NULL) {
651
return NULL;
652
}
653
if (add_cell_names_from_symbols(ste->ste_symbols, names) < 0) {
654
Py_DECREF(names);
655
return NULL;
656
}
657
if (add_inlined_comprehension_cell_names(ste, names) < 0) {
658
Py_DECREF(names);
659
return NULL;
660
}
661
PyObject *sorted = PySequence_List(names);
662
Py_DECREF(names);
663
if (sorted == NULL) {
664
return NULL;
665
}
666
if (PyList_Sort(sorted) < 0) {
667
Py_DECREF(sorted);
668
return NULL;
669
}
670
PyObject *cellvars = list2dict(sorted);
671
Py_DECREF(sorted);
672
return cellvars;
673
}
674
Sep 9, 2024
Sep 9, 2024
675
int
676
_PyCompile_EnterScope(compiler *c, identifier name, int scope_type,
677
void *key, int lineno, PyObject *private,
678
_PyCompile_CodeUnitMetadata *umd)
Aug 30, 2024
Aug 30, 2024
679
{
680
struct compiler_unit *u;
681
u = (struct compiler_unit *)PyMem_Calloc(1, sizeof(struct compiler_unit));
682
if (!u) {
683
PyErr_NoMemory();
684
return ERROR;
685
}
686
u->u_scope_type = scope_type;
687
if (umd != NULL) {
688
u->u_metadata = *umd;
689
}
690
else {
691
u->u_metadata.u_argcount = 0;
692
u->u_metadata.u_posonlyargcount = 0;
693
u->u_metadata.u_kwonlyargcount = 0;
694
}
695
u->u_ste = _PySymtable_Lookup(c->c_st, key);
696
if (!u->u_ste) {
697
compiler_unit_free(u);
698
return ERROR;
699
}
700
u->u_metadata.u_name = Py_NewRef(name);
701
u->u_metadata.u_varnames = list2dict(u->u_ste->ste_varnames);
702
if (!u->u_metadata.u_varnames) {
703
compiler_unit_free(u);
704
return ERROR;
705
}
Sep 23, 2026
Sep 23, 2026
706
u->u_metadata.u_cellvars = compiler_cellvars(u->u_ste);
Aug 30, 2024
Aug 30, 2024
707
if (!u->u_metadata.u_cellvars) {
708
compiler_unit_free(u);
709
return ERROR;
710
}
711
if (u->u_ste->ste_needs_class_closure) {
712
/* Cook up an implicit __class__ cell. */
713
Py_ssize_t res;
Sep 9, 2024
Sep 9, 2024
714
assert(u->u_scope_type == COMPILE_SCOPE_CLASS);
715
res = _PyCompile_DictAddObj(u->u_metadata.u_cellvars, &_Py_ID(__class__));
Aug 30, 2024
Aug 30, 2024
716
if (res < 0) {
717
compiler_unit_free(u);
718
return ERROR;
719
}
720
}
721
if (u->u_ste->ste_needs_classdict) {
722
/* Cook up an implicit __classdict__ cell. */
723
Py_ssize_t res;
Sep 9, 2024
Sep 9, 2024
724
assert(u->u_scope_type == COMPILE_SCOPE_CLASS);
725
res = _PyCompile_DictAddObj(u->u_metadata.u_cellvars, &_Py_ID(__classdict__));
Aug 30, 2024
Aug 30, 2024
726
if (res < 0) {
727
compiler_unit_free(u);
728
return ERROR;
729
}
730
}
Mar 26, 2025
Mar 26, 2025
731
if (u->u_ste->ste_has_conditional_annotations) {
Jan 3, 2026
Jan 3, 2026
732
/* Cook up an implicit __conditional_annotations__ cell */
Mar 26, 2025
Mar 26, 2025
733
Py_ssize_t res;
734
assert(u->u_scope_type == COMPILE_SCOPE_CLASS || u->u_scope_type == COMPILE_SCOPE_MODULE);
735
res = _PyCompile_DictAddObj(u->u_metadata.u_cellvars, &_Py_ID(__conditional_annotations__));
736
if (res < 0) {
737
compiler_unit_free(u);
738
return ERROR;
739
}
740
}
Aug 30, 2024
Aug 30, 2024
741
742
u->u_metadata.u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS,
743
PyDict_GET_SIZE(u->u_metadata.u_cellvars));
744
if (!u->u_metadata.u_freevars) {
745
compiler_unit_free(u);
746
return ERROR;
747
}
748
Sep 23, 2026
Sep 23, 2026
749
if (scope_type == COMPILE_SCOPE_MODULE || scope_type == COMPILE_SCOPE_CLASS) {
750
u->u_metadata.u_fasthidden = PySet_New(NULL);
751
if (!u->u_metadata.u_fasthidden) {
752
compiler_unit_free(u);
753
return ERROR;
754
}
755
}
756
else {
757
u->u_metadata.u_fasthidden = NULL;
Aug 30, 2024
Aug 30, 2024
758
}
759
760
u->u_nfblocks = 0;
761
u->u_metadata.u_firstlineno = lineno;
762
u->u_metadata.u_consts = PyDict_New();
763
if (!u->u_metadata.u_consts) {
764
compiler_unit_free(u);
765
return ERROR;
766
}
767
u->u_metadata.u_names = PyDict_New();
768
if (!u->u_metadata.u_names) {
769
compiler_unit_free(u);
770
return ERROR;
771
}
772
773
u->u_deferred_annotations = NULL;
Mar 26, 2025
Mar 26, 2025
774
u->u_conditional_annotation_indices = NULL;
775
u->u_next_conditional_annotation_index = 0;
Sep 9, 2024
Sep 9, 2024
776
if (scope_type == COMPILE_SCOPE_CLASS) {
Aug 30, 2024
Aug 30, 2024
777
u->u_static_attributes = PySet_New(0);
778
if (!u->u_static_attributes) {
779
compiler_unit_free(u);
780
return ERROR;
781
}
782
}
783
else {
784
u->u_static_attributes = NULL;
785
}
786
787
u->u_instr_sequence = (instr_sequence*)_PyInstructionSequence_New();
788
if (!u->u_instr_sequence) {
789
compiler_unit_free(u);
790
return ERROR;
791
}
Apr 28, 2025
Apr 28, 2025
792
u->u_stashed_instr_sequence = NULL;
Aug 30, 2024
Aug 30, 2024
793
794
/* Push the old compiler_unit on the stack. */
795
if (c->u) {
796
PyObject *capsule = PyCapsule_New(c->u, CAPSULE_NAME, NULL);
797
if (!capsule || PyList_Append(c->c_stack, capsule) < 0) {
798
Py_XDECREF(capsule);
799
compiler_unit_free(u);
800
return ERROR;
801
}
802
Py_DECREF(capsule);
803
if (private == NULL) {
804
private = c->u->u_private;
805
}
806
}
807
808
u->u_private = Py_XNewRef(private);
809
810
c->u = u;
811
return SUCCESS;
812
}
813
Sep 9, 2024
Sep 9, 2024
814
void
815
_PyCompile_ExitScope(compiler *c)
Aug 30, 2024
Aug 30, 2024
816
{
817
// Don't call PySequence_DelItem() with an exception raised
818
PyObject *exc = PyErr_GetRaisedException();
819
820
instr_sequence *nested_seq = NULL;
821
if (c->c_save_nested_seqs) {
822
nested_seq = c->u->u_instr_sequence;
823
Py_INCREF(nested_seq);
824
}
825
compiler_unit_free(c->u);
826
/* Restore c->u to the parent unit. */
827
Py_ssize_t n = PyList_GET_SIZE(c->c_stack) - 1;
828
if (n >= 0) {
829
PyObject *capsule = PyList_GET_ITEM(c->c_stack, n);
830
c->u = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME);
831
assert(c->u);
832
/* we are deleting from a list so this really shouldn't fail */
833
if (PySequence_DelItem(c->c_stack, n) < 0) {
Jan 31, 2025
Jan 31, 2025
834
PyErr_FormatUnraisable("Exception ignored while removing "
Aug 30, 2024
Aug 30, 2024
835
"the last compiler stack item");
836
}
837
if (nested_seq != NULL) {
838
if (_PyInstructionSequence_AddNested(c->u->u_instr_sequence, nested_seq) < 0) {
Jan 31, 2025
Jan 31, 2025
839
PyErr_FormatUnraisable("Exception ignored while appending "
Aug 30, 2024
Aug 30, 2024
840
"nested instruction sequence");
841
}
842
}
843
}
844
else {
845
c->u = NULL;
846
}
847
Py_XDECREF(nested_seq);
848
849
PyErr_SetRaisedException(exc);
850
}
851
852
/*
853
* Frame block handling functions
854
*/
855
Sep 9, 2024
Sep 9, 2024
856
int
857
_PyCompile_PushFBlock(compiler *c, location loc,
858
fblocktype t, jump_target_label block_label,
Aug 30, 2024
Aug 30, 2024
859
jump_target_label exit, void *datum)
860
{
Sep 9, 2024
Sep 9, 2024
861
fblockinfo *f;
Aug 30, 2024
Aug 30, 2024
862
if (c->u->u_nfblocks >= CO_MAXBLOCKS) {
Sep 9, 2024
Sep 9, 2024
863
return _PyCompile_Error(c, loc, "too many statically nested blocks");
Aug 30, 2024
Aug 30, 2024
864
}
865
f = &c->u->u_fblock[c->u->u_nfblocks++];
866
f->fb_type = t;
867
f->fb_block = block_label;
868
f->fb_loc = loc;
869
f->fb_exit = exit;
870
f->fb_datum = datum;
Oct 14, 2025
Oct 14, 2025
871
if (t == COMPILE_FBLOCK_FINALLY_END) {
872
c->c_disable_warning++;
873
}
Aug 30, 2024
Aug 30, 2024
874
return SUCCESS;
875
}
876
Sep 9, 2024
Sep 9, 2024
877
void
878
_PyCompile_PopFBlock(compiler *c, fblocktype t, jump_target_label block_label)
Aug 30, 2024
Aug 30, 2024
879
{
880
struct compiler_unit *u = c->u;
881
assert(u->u_nfblocks > 0);
882
u->u_nfblocks--;
883
assert(u->u_fblock[u->u_nfblocks].fb_type == t);
Sep 9, 2024
Sep 9, 2024
884
assert(SAME_JUMP_TARGET_LABEL(u->u_fblock[u->u_nfblocks].fb_block, block_label));
Oct 14, 2025
Oct 14, 2025
885
if (t == COMPILE_FBLOCK_FINALLY_END) {
886
c->c_disable_warning--;
887
}
Aug 30, 2024
Aug 30, 2024
888
}
889
Sep 9, 2024
Sep 9, 2024
890
fblockinfo *
891
_PyCompile_TopFBlock(compiler *c)
Aug 30, 2024
Aug 30, 2024
892
{
893
if (c->u->u_nfblocks == 0) {
894
return NULL;
895
}
896
return &c->u->u_fblock[c->u->u_nfblocks - 1];
897
}
898
Feb 12, 2026
Feb 12, 2026
899
bool
900
_PyCompile_InExceptionHandler(compiler *c)
901
{
902
for (Py_ssize_t i = 0; i < c->u->u_nfblocks; i++) {
903
fblockinfo *block = &c->u->u_fblock[i];
904
switch (block->fb_type) {
905
case COMPILE_FBLOCK_TRY_EXCEPT:
906
case COMPILE_FBLOCK_FINALLY_TRY:
907
case COMPILE_FBLOCK_FINALLY_END:
908
case COMPILE_FBLOCK_EXCEPTION_HANDLER:
909
case COMPILE_FBLOCK_EXCEPTION_GROUP_HANDLER:
910
case COMPILE_FBLOCK_HANDLER_CLEANUP:
911
return true;
912
default:
913
break;
914
}
915
}
916
return false;
917
}
918
Mar 26, 2025
Mar 26, 2025
919
void
920
_PyCompile_DeferredAnnotations(compiler *c,
921
PyObject **deferred_annotations,
922
PyObject **conditional_annotation_indices)
Aug 30, 2024
Aug 30, 2024
923
{
Mar 26, 2025
Mar 26, 2025
924
*deferred_annotations = Py_XNewRef(c->u->u_deferred_annotations);
925
*conditional_annotation_indices = Py_XNewRef(c->u->u_conditional_annotation_indices);
Aug 30, 2024
Aug 30, 2024
926
}
927
Sep 9, 2024
Sep 9, 2024
928
static location
929
start_location(asdl_stmt_seq *stmts)
930
{
931
if (asdl_seq_LEN(stmts) > 0) {
932
/* Set current line number to the line number of first statement.
933
* This way line number for SETUP_ANNOTATIONS will always
934
* coincide with the line number of first "real" statement in module.
935
* If body is empty, then lineno will be set later in the assembly stage.
936
*/
937
stmt_ty st = (stmt_ty)asdl_seq_GET(stmts, 0);
938
return SRC_LOCATION_FROM_AST(st);
939
}
940
return (const _Py_SourceLocation){1, 1, 0, 0};
941
}
942
Aug 30, 2024
Aug 30, 2024
943
static int
Sep 9, 2024
Sep 9, 2024
944
compiler_codegen(compiler *c, mod_ty mod)
Aug 30, 2024
Aug 30, 2024
945
{
Sep 9, 2024
Sep 9, 2024
946
RETURN_IF_ERROR(_PyCodegen_EnterAnonymousScope(c, mod));
947
assert(c->u->u_scope_type == COMPILE_SCOPE_MODULE);
Aug 30, 2024
Aug 30, 2024
948
switch (mod->kind) {
949
case Module_kind: {
950
asdl_stmt_seq *stmts = mod->v.Module.body;
Mar 26, 2025
Mar 26, 2025
951
RETURN_IF_ERROR(_PyCodegen_Module(c, start_location(stmts), stmts, false));
Aug 30, 2024
Aug 30, 2024
952
break;
953
}
954
case Interactive_kind: {
955
c->c_interactive = 1;
956
asdl_stmt_seq *stmts = mod->v.Interactive.body;
Mar 26, 2025
Mar 26, 2025
957
RETURN_IF_ERROR(_PyCodegen_Module(c, start_location(stmts), stmts, true));
Aug 30, 2024
Aug 30, 2024
958
break;
959
}
960
case Expression_kind: {
Sep 9, 2024
Sep 9, 2024
961
RETURN_IF_ERROR(_PyCodegen_Expression(c, mod->v.Expression.body));
Aug 30, 2024
Aug 30, 2024
962
break;
963
}
964
default: {
965
PyErr_Format(PyExc_SystemError,
966
"module kind %d should not be possible",
967
mod->kind);
968
return ERROR;
969
}}
970
return SUCCESS;
971
}
972
973
static PyCodeObject *
Sep 9, 2024
Sep 9, 2024
974
compiler_mod(compiler *c, mod_ty mod)
Aug 30, 2024
Aug 30, 2024
975
{
976
PyCodeObject *co = NULL;
977
int addNone = mod->kind != Expression_kind;
Jun 11, 2026
Jun 11, 2026
978
assert(c->u == NULL);
Aug 30, 2024
Aug 30, 2024
979
if (compiler_codegen(c, mod) < 0) {
980
goto finally;
981
}
Sep 9, 2024
Sep 9, 2024
982
co = _PyCompile_OptimizeAndAssemble(c, addNone);
Aug 30, 2024
Aug 30, 2024
983
finally:
Jun 11, 2026
Jun 11, 2026
984
if (c->u != NULL) {
985
_PyCompile_ExitScope(c);
986
}
Aug 30, 2024
Aug 30, 2024
987
return co;
988
}
989
Sep 23, 2026
Sep 23, 2026
990
/* Inlined comprehensions are compiled in the enclosing unit. If a name is
991
* FREE in the comprehension, or is absent from its table (scope 0), resolve
992
* it in enclosing tables until it is bound. Stop if the next table is a class:
993
* nested scopes (including inlined comprehensions) do not see class locals, so
994
* the name stays FREE. __class__ and friends are not allowed to be free
995
* through a class; treat those loads as implicit globals.
996
*
997
* Names with no entry (scope 0) include loads synthesized by codegen, such as
998
* the implicit receiver for zero-arg super(). */
999
static int
1000
compiler_resolve_inlined_free(PySTEntryObject **ste, PyObject *name)