/src/cpython/Python/compile.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * This file compiles an abstract syntax tree (AST) into Python bytecode. |
3 | | * |
4 | | * The primary entry point is _PyAST_Compile(), which returns a |
5 | | * PyCodeObject. The compiler makes several passes to build the code |
6 | | * object: |
7 | | * 1. Checks for future statements. See future.c |
8 | | * 2. Builds a symbol table. See symtable.c. |
9 | | * 3. Generate an instruction sequence. See compiler_mod() in this file, which |
10 | | * calls functions from codegen.c. |
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. |
14 | | * |
15 | | */ |
16 | | |
17 | | #include "Python.h" |
18 | | #include "pycore_ast.h" // PyAST_Check() |
19 | | #include "pycore_code.h" |
20 | | #include "pycore_compile.h" |
21 | | #include "pycore_flowgraph.h" // _PyCfg_FromInstructionSequence() |
22 | | #include "pycore_pystate.h" // _Py_GetConfig() |
23 | | #include "pycore_runtime.h" // _Py_ID() |
24 | | #include "pycore_setobject.h" // _PySet_NextEntry() |
25 | | #include "pycore_stats.h" |
26 | | #include "pycore_unicodeobject.h" // _PyUnicode_EqualToASCIIString() |
27 | | |
28 | | #include "cpython/code.h" |
29 | | |
30 | | #include <stdbool.h> |
31 | | |
32 | | |
33 | | #undef SUCCESS |
34 | | #undef ERROR |
35 | 149k | #define SUCCESS 0 |
36 | 0 | #define ERROR -1 |
37 | | |
38 | | #define RETURN_IF_ERROR(X) \ |
39 | 56.0k | do { \ |
40 | 56.0k | if ((X) == -1) { \ |
41 | 0 | return ERROR; \ |
42 | 0 | } \ |
43 | 56.0k | } while (0) |
44 | | |
45 | | typedef _Py_SourceLocation location; |
46 | | typedef _PyJumpTargetLabel jump_target_label; |
47 | | typedef _PyInstructionSequence instr_sequence; |
48 | | typedef struct _PyCfgBuilder cfg_builder; |
49 | | typedef _PyCompile_FBlockInfo fblockinfo; |
50 | | typedef enum _PyCompile_FBlockType fblocktype; |
51 | | |
52 | | /* The following items change on entry and exit of code blocks. |
53 | | They must be saved and restored when returning to a block. |
54 | | */ |
55 | | struct compiler_unit { |
56 | | PySTEntryObject *u_ste; |
57 | | |
58 | | int u_scope_type; |
59 | | |
60 | | PyObject *u_private; /* for private name mangling */ |
61 | | PyObject *u_static_attributes; /* for class: attributes accessed via self.X */ |
62 | | PyObject *u_deferred_annotations; /* AnnAssign nodes deferred to the end of compilation */ |
63 | | PyObject *u_conditional_annotation_indices; /* indices of annotations that are conditionally executed (or -1 for unconditional annotations) */ |
64 | | long u_next_conditional_annotation_index; /* index of the next conditional annotation */ |
65 | | |
66 | | instr_sequence *u_instr_sequence; /* codegen output */ |
67 | | instr_sequence *u_stashed_instr_sequence; /* temporarily stashed parent instruction sequence */ |
68 | | |
69 | | int u_nfblocks; |
70 | | int u_in_inlined_comp; |
71 | | int u_in_conditional_block; |
72 | | |
73 | | _PyCompile_FBlockInfo u_fblock[CO_MAXBLOCKS]; |
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 |
82 | | managed by _PyCompile_EnterScope() and _PyCompile_ExitScope(). |
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 | | |
90 | | typedef struct _PyCompiler { |
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 | | */ |
106 | | } compiler; |
107 | | |
108 | | static int |
109 | | compiler_setup(compiler *c, mod_ty mod, PyObject *filename, |
110 | | PyCompilerFlags *flags, int optimize, PyArena *arena) |
111 | 307 | { |
112 | 307 | PyCompilerFlags local_flags = _PyCompilerFlags_INIT; |
113 | | |
114 | 307 | c->c_const_cache = PyDict_New(); |
115 | 307 | if (!c->c_const_cache) { |
116 | 0 | return ERROR; |
117 | 0 | } |
118 | | |
119 | 307 | c->c_stack = PyList_New(0); |
120 | 307 | if (!c->c_stack) { |
121 | 0 | return ERROR; |
122 | 0 | } |
123 | | |
124 | 307 | c->c_filename = Py_NewRef(filename); |
125 | 307 | if (!_PyFuture_FromAST(mod, filename, &c->c_future)) { |
126 | 0 | return ERROR; |
127 | 0 | } |
128 | 307 | if (!flags) { |
129 | 48 | flags = &local_flags; |
130 | 48 | } |
131 | 307 | int merged = c->c_future.ff_features | flags->cf_flags; |
132 | 307 | c->c_future.ff_features = merged; |
133 | 307 | flags->cf_flags = merged; |
134 | 307 | c->c_flags = *flags; |
135 | 307 | c->c_optimize = (optimize == -1) ? _Py_GetConfig()->optimization_level : optimize; |
136 | 307 | c->c_save_nested_seqs = false; |
137 | | |
138 | 307 | if (!_PyAST_Preprocess(mod, arena, filename, c->c_optimize, merged, 0)) { |
139 | 0 | return ERROR; |
140 | 0 | } |
141 | 307 | c->c_st = _PySymtable_Build(mod, filename, &c->c_future); |
142 | 307 | if (c->c_st == NULL) { |
143 | 0 | if (!PyErr_Occurred()) { |
144 | 0 | PyErr_SetString(PyExc_SystemError, "no symtable"); |
145 | 0 | } |
146 | 0 | return ERROR; |
147 | 0 | } |
148 | 307 | return SUCCESS; |
149 | 307 | } |
150 | | |
151 | | static void |
152 | | compiler_free(compiler *c) |
153 | 307 | { |
154 | 307 | if (c->c_st) { |
155 | 307 | _PySymtable_Free(c->c_st); |
156 | 307 | } |
157 | 307 | Py_XDECREF(c->c_filename); |
158 | 307 | Py_XDECREF(c->c_const_cache); |
159 | 307 | Py_XDECREF(c->c_stack); |
160 | 307 | PyMem_Free(c); |
161 | 307 | } |
162 | | |
163 | | static compiler* |
164 | | new_compiler(mod_ty mod, PyObject *filename, PyCompilerFlags *pflags, |
165 | | int optimize, PyArena *arena) |
166 | 307 | { |
167 | 307 | compiler *c = PyMem_Calloc(1, sizeof(compiler)); |
168 | 307 | if (c == NULL) { |
169 | 0 | return NULL; |
170 | 0 | } |
171 | 307 | if (compiler_setup(c, mod, filename, pflags, optimize, arena) < 0) { |
172 | 0 | compiler_free(c); |
173 | 0 | return NULL; |
174 | 0 | } |
175 | 307 | return c; |
176 | 307 | } |
177 | | |
178 | | static void |
179 | | compiler_unit_free(struct compiler_unit *u) |
180 | 5.78k | { |
181 | 5.78k | Py_CLEAR(u->u_instr_sequence); |
182 | 5.78k | Py_CLEAR(u->u_stashed_instr_sequence); |
183 | 5.78k | Py_CLEAR(u->u_ste); |
184 | 5.78k | Py_CLEAR(u->u_metadata.u_name); |
185 | 5.78k | Py_CLEAR(u->u_metadata.u_qualname); |
186 | 5.78k | Py_CLEAR(u->u_metadata.u_consts); |
187 | 5.78k | Py_CLEAR(u->u_metadata.u_names); |
188 | 5.78k | Py_CLEAR(u->u_metadata.u_varnames); |
189 | 5.78k | Py_CLEAR(u->u_metadata.u_freevars); |
190 | 5.78k | Py_CLEAR(u->u_metadata.u_cellvars); |
191 | 5.78k | Py_CLEAR(u->u_metadata.u_fasthidden); |
192 | 5.78k | Py_CLEAR(u->u_private); |
193 | 5.78k | Py_CLEAR(u->u_static_attributes); |
194 | 5.78k | Py_CLEAR(u->u_deferred_annotations); |
195 | 5.78k | Py_CLEAR(u->u_conditional_annotation_indices); |
196 | 5.78k | PyMem_Free(u); |
197 | 5.78k | } |
198 | | |
199 | 15.1k | #define CAPSULE_NAME "compile.c compiler unit" |
200 | | |
201 | | int |
202 | | _PyCompile_MaybeAddStaticAttributeToClass(compiler *c, expr_ty e) |
203 | 11.4k | { |
204 | 11.4k | assert(e->kind == Attribute_kind); |
205 | 11.4k | expr_ty attr_value = e->v.Attribute.value; |
206 | 11.4k | if (attr_value->kind != Name_kind || |
207 | 11.4k | e->v.Attribute.ctx != Store || |
208 | 11.4k | !_PyUnicode_EqualToASCIIString(attr_value->v.Name.id, "self")) |
209 | 10.3k | { |
210 | 10.3k | return SUCCESS; |
211 | 10.3k | } |
212 | 1.00k | Py_ssize_t stack_size = PyList_GET_SIZE(c->c_stack); |
213 | 1.02k | for (Py_ssize_t i = stack_size - 1; i >= 0; i--) { |
214 | 1.01k | PyObject *capsule = PyList_GET_ITEM(c->c_stack, i); |
215 | 1.01k | struct compiler_unit *u = (struct compiler_unit *)PyCapsule_GetPointer( |
216 | 1.01k | capsule, CAPSULE_NAME); |
217 | 1.01k | assert(u); |
218 | 1.01k | if (u->u_scope_type == COMPILE_SCOPE_CLASS) { |
219 | 993 | assert(u->u_static_attributes); |
220 | 993 | RETURN_IF_ERROR(PySet_Add(u->u_static_attributes, e->v.Attribute.attr)); |
221 | 993 | break; |
222 | 993 | } |
223 | 1.01k | } |
224 | 1.00k | return SUCCESS; |
225 | 1.00k | } |
226 | | |
227 | | static int |
228 | | compiler_set_qualname(compiler *c) |
229 | 5.47k | { |
230 | 5.47k | Py_ssize_t stack_size; |
231 | 5.47k | struct compiler_unit *u = c->u; |
232 | 5.47k | PyObject *name, *base; |
233 | | |
234 | 5.47k | base = NULL; |
235 | 5.47k | stack_size = PyList_GET_SIZE(c->c_stack); |
236 | 5.47k | assert(stack_size >= 1); |
237 | 5.47k | if (stack_size > 1) { |
238 | 3.17k | int scope, force_global = 0; |
239 | 3.17k | struct compiler_unit *parent; |
240 | 3.17k | PyObject *mangled, *capsule; |
241 | | |
242 | 3.17k | capsule = PyList_GET_ITEM(c->c_stack, stack_size - 1); |
243 | 3.17k | parent = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME); |
244 | 3.17k | assert(parent); |
245 | 3.17k | if (parent->u_scope_type == COMPILE_SCOPE_ANNOTATIONS) { |
246 | | /* The parent is an annotation scope, so we need to |
247 | | look at the grandparent. */ |
248 | 0 | if (stack_size == 2) { |
249 | | // If we're immediately within the module, we can skip |
250 | | // the rest and just set the qualname to be the same as name. |
251 | 0 | u->u_metadata.u_qualname = Py_NewRef(u->u_metadata.u_name); |
252 | 0 | return SUCCESS; |
253 | 0 | } |
254 | 0 | capsule = PyList_GET_ITEM(c->c_stack, stack_size - 2); |
255 | 0 | parent = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME); |
256 | 0 | assert(parent); |
257 | 0 | } |
258 | | |
259 | 3.17k | if (u->u_scope_type == COMPILE_SCOPE_FUNCTION |
260 | 3.17k | || u->u_scope_type == COMPILE_SCOPE_ASYNC_FUNCTION |
261 | 3.17k | || u->u_scope_type == COMPILE_SCOPE_CLASS) { |
262 | 2.92k | assert(u->u_metadata.u_name); |
263 | 2.92k | mangled = _Py_Mangle(parent->u_private, u->u_metadata.u_name); |
264 | 2.92k | if (!mangled) { |
265 | 0 | return ERROR; |
266 | 0 | } |
267 | | |
268 | 2.92k | scope = _PyST_GetScope(parent->u_ste, mangled); |
269 | 2.92k | Py_DECREF(mangled); |
270 | 2.92k | RETURN_IF_ERROR(scope); |
271 | 2.92k | assert(scope != GLOBAL_IMPLICIT); |
272 | 2.92k | if (scope == GLOBAL_EXPLICIT) |
273 | 0 | force_global = 1; |
274 | 2.92k | } |
275 | | |
276 | 3.17k | if (!force_global) { |
277 | 3.17k | if (parent->u_scope_type == COMPILE_SCOPE_FUNCTION |
278 | 3.17k | || parent->u_scope_type == COMPILE_SCOPE_ASYNC_FUNCTION |
279 | 3.17k | || parent->u_scope_type == COMPILE_SCOPE_LAMBDA) |
280 | 416 | { |
281 | 416 | _Py_DECLARE_STR(dot_locals, ".<locals>"); |
282 | 416 | base = PyUnicode_Concat(parent->u_metadata.u_qualname, |
283 | 416 | &_Py_STR(dot_locals)); |
284 | 416 | if (base == NULL) { |
285 | 0 | return ERROR; |
286 | 0 | } |
287 | 416 | } |
288 | 2.76k | else { |
289 | 2.76k | base = Py_NewRef(parent->u_metadata.u_qualname); |
290 | 2.76k | } |
291 | 3.17k | } |
292 | 3.17k | } |
293 | | |
294 | 5.47k | if (base != NULL) { |
295 | 3.17k | name = PyUnicode_Concat(base, _Py_LATIN1_CHR('.')); |
296 | 3.17k | Py_DECREF(base); |
297 | 3.17k | if (name == NULL) { |
298 | 0 | return ERROR; |
299 | 0 | } |
300 | 3.17k | PyUnicode_Append(&name, u->u_metadata.u_name); |
301 | 3.17k | if (name == NULL) { |
302 | 0 | return ERROR; |
303 | 0 | } |
304 | 3.17k | } |
305 | 2.30k | else { |
306 | 2.30k | name = Py_NewRef(u->u_metadata.u_name); |
307 | 2.30k | } |
308 | 5.47k | u->u_metadata.u_qualname = name; |
309 | | |
310 | 5.47k | return SUCCESS; |
311 | 5.47k | } |
312 | | |
313 | | /* Merge const *o* and return constant key object. |
314 | | * If recursive, insert all elements if o is a tuple or frozen set. |
315 | | */ |
316 | | static PyObject* |
317 | | const_cache_insert(PyObject *const_cache, PyObject *o, bool recursive) |
318 | 102k | { |
319 | 102k | assert(PyDict_CheckExact(const_cache)); |
320 | | // None and Ellipsis are immortal objects, and key is the singleton. |
321 | | // No need to merge object and key. |
322 | 102k | if (o == Py_None || o == Py_Ellipsis) { |
323 | 11.4k | return o; |
324 | 11.4k | } |
325 | | |
326 | 91.5k | PyObject *key = _PyCode_ConstantKey(o); |
327 | 91.5k | if (key == NULL) { |
328 | 0 | return NULL; |
329 | 0 | } |
330 | | |
331 | 91.5k | PyObject *t; |
332 | 91.5k | int res = PyDict_SetDefaultRef(const_cache, key, key, &t); |
333 | 91.5k | if (res != 0) { |
334 | | // o was not inserted into const_cache. t is either the existing value |
335 | | // or NULL (on error). |
336 | 35.0k | Py_DECREF(key); |
337 | 35.0k | return t; |
338 | 35.0k | } |
339 | 56.4k | Py_DECREF(t); |
340 | | |
341 | 56.4k | if (!recursive) { |
342 | 24.2k | return key; |
343 | 24.2k | } |
344 | | |
345 | | // We registered o in const_cache. |
346 | | // When o is a tuple or frozenset, we want to merge its |
347 | | // items too. |
348 | 32.2k | if (PyTuple_CheckExact(o)) { |
349 | 970 | Py_ssize_t len = PyTuple_GET_SIZE(o); |
350 | 3.36k | for (Py_ssize_t i = 0; i < len; i++) { |
351 | 2.39k | PyObject *item = PyTuple_GET_ITEM(o, i); |
352 | 2.39k | PyObject *u = const_cache_insert(const_cache, item, recursive); |
353 | 2.39k | if (u == NULL) { |
354 | 0 | Py_DECREF(key); |
355 | 0 | return NULL; |
356 | 0 | } |
357 | | |
358 | | // See _PyCode_ConstantKey() |
359 | 2.39k | PyObject *v; // borrowed |
360 | 2.39k | if (PyTuple_CheckExact(u)) { |
361 | 0 | v = PyTuple_GET_ITEM(u, 1); |
362 | 0 | } |
363 | 2.39k | else { |
364 | 2.39k | v = u; |
365 | 2.39k | } |
366 | 2.39k | if (v != item) { |
367 | 144 | PyTuple_SET_ITEM(o, i, Py_NewRef(v)); |
368 | 144 | Py_DECREF(item); |
369 | 144 | } |
370 | | |
371 | 2.39k | Py_DECREF(u); |
372 | 2.39k | } |
373 | 970 | } |
374 | 31.2k | else if (PyFrozenSet_CheckExact(o)) { |
375 | | // *key* is tuple. And its first item is frozenset of |
376 | | // constant keys. |
377 | | // See _PyCode_ConstantKey() for detail. |
378 | 0 | assert(PyTuple_CheckExact(key)); |
379 | 0 | assert(PyTuple_GET_SIZE(key) == 2); |
380 | |
|
381 | 0 | Py_ssize_t len = PySet_GET_SIZE(o); |
382 | 0 | if (len == 0) { // empty frozenset should not be re-created. |
383 | 0 | return key; |
384 | 0 | } |
385 | 0 | PyObject *tuple = PyTuple_New(len); |
386 | 0 | if (tuple == NULL) { |
387 | 0 | Py_DECREF(key); |
388 | 0 | return NULL; |
389 | 0 | } |
390 | 0 | Py_ssize_t i = 0, pos = 0; |
391 | 0 | PyObject *item; |
392 | 0 | Py_hash_t hash; |
393 | 0 | while (_PySet_NextEntry(o, &pos, &item, &hash)) { |
394 | 0 | PyObject *k = const_cache_insert(const_cache, item, recursive); |
395 | 0 | if (k == NULL) { |
396 | 0 | Py_DECREF(tuple); |
397 | 0 | Py_DECREF(key); |
398 | 0 | return NULL; |
399 | 0 | } |
400 | 0 | PyObject *u; |
401 | 0 | if (PyTuple_CheckExact(k)) { |
402 | 0 | u = Py_NewRef(PyTuple_GET_ITEM(k, 1)); |
403 | 0 | Py_DECREF(k); |
404 | 0 | } |
405 | 0 | else { |
406 | 0 | u = k; |
407 | 0 | } |
408 | 0 | PyTuple_SET_ITEM(tuple, i, u); // Steals reference of u. |
409 | 0 | i++; |
410 | 0 | } |
411 | | |
412 | | // Instead of rewriting o, we create new frozenset and embed in the |
413 | | // key tuple. Caller should get merged frozenset from the key tuple. |
414 | 0 | PyObject *new = PyFrozenSet_New(tuple); |
415 | 0 | Py_DECREF(tuple); |
416 | 0 | if (new == NULL) { |
417 | 0 | Py_DECREF(key); |
418 | 0 | return NULL; |
419 | 0 | } |
420 | 0 | assert(PyTuple_GET_ITEM(key, 1) == o); |
421 | 0 | Py_DECREF(o); |
422 | 0 | PyTuple_SET_ITEM(key, 1, new); |
423 | 0 | } |
424 | | |
425 | 32.2k | return key; |
426 | 32.2k | } |
427 | | |
428 | | static PyObject* |
429 | | merge_consts_recursive(PyObject *const_cache, PyObject *o) |
430 | 63.8k | { |
431 | 63.8k | return const_cache_insert(const_cache, o, true); |
432 | 63.8k | } |
433 | | |
434 | | Py_ssize_t |
435 | | _PyCompile_DictAddObj(PyObject *dict, PyObject *o) |
436 | 169k | { |
437 | 169k | PyObject *v; |
438 | 169k | Py_ssize_t arg; |
439 | | |
440 | 169k | if (PyDict_GetItemRef(dict, o, &v) < 0) { |
441 | 0 | return ERROR; |
442 | 0 | } |
443 | 169k | if (!v) { |
444 | 87.3k | arg = PyDict_GET_SIZE(dict); |
445 | 87.3k | v = PyLong_FromSsize_t(arg); |
446 | 87.3k | if (!v) { |
447 | 0 | return ERROR; |
448 | 0 | } |
449 | 87.3k | if (PyDict_SetItem(dict, o, v) < 0) { |
450 | 0 | Py_DECREF(v); |
451 | 0 | return ERROR; |
452 | 0 | } |
453 | 87.3k | } |
454 | 82.6k | else |
455 | 82.6k | arg = PyLong_AsLong(v); |
456 | 169k | Py_DECREF(v); |
457 | 169k | return arg; |
458 | 169k | } |
459 | | |
460 | | Py_ssize_t |
461 | | _PyCompile_AddConst(compiler *c, PyObject *o) |
462 | 63.8k | { |
463 | 63.8k | PyObject *key = merge_consts_recursive(c->c_const_cache, o); |
464 | 63.8k | if (key == NULL) { |
465 | 0 | return ERROR; |
466 | 0 | } |
467 | | |
468 | 63.8k | Py_ssize_t arg = _PyCompile_DictAddObj(c->u->u_metadata.u_consts, key); |
469 | 63.8k | Py_DECREF(key); |
470 | 63.8k | return arg; |
471 | 63.8k | } |
472 | | |
473 | | static PyObject * |
474 | | list2dict(PyObject *list) |
475 | 5.78k | { |
476 | 5.78k | Py_ssize_t i, n; |
477 | 5.78k | PyObject *v, *k; |
478 | 5.78k | PyObject *dict = PyDict_New(); |
479 | 5.78k | if (!dict) return NULL; |
480 | | |
481 | 5.78k | n = PyList_Size(list); |
482 | 14.5k | for (i = 0; i < n; i++) { |
483 | 8.74k | v = PyLong_FromSsize_t(i); |
484 | 8.74k | if (!v) { |
485 | 0 | Py_DECREF(dict); |
486 | 0 | return NULL; |
487 | 0 | } |
488 | 8.74k | k = PyList_GET_ITEM(list, i); |
489 | 8.74k | if (PyDict_SetItem(dict, k, v) < 0) { |
490 | 0 | Py_DECREF(v); |
491 | 0 | Py_DECREF(dict); |
492 | 0 | return NULL; |
493 | 0 | } |
494 | 8.74k | Py_DECREF(v); |
495 | 8.74k | } |
496 | 5.78k | return dict; |
497 | 5.78k | } |
498 | | |
499 | | /* Return new dict containing names from src that match scope(s). |
500 | | |
501 | | src is a symbol table dictionary. If the scope of a name matches |
502 | | either scope_type or flag is set, insert it into the new dict. The |
503 | | values are integers, starting at offset and increasing by one for |
504 | | each key. |
505 | | */ |
506 | | |
507 | | static PyObject * |
508 | | dictbytype(PyObject *src, int scope_type, int flag, Py_ssize_t offset) |
509 | 11.5k | { |
510 | 11.5k | Py_ssize_t i = offset, num_keys, key_i; |
511 | 11.5k | PyObject *k, *v, *dest = PyDict_New(); |
512 | 11.5k | PyObject *sorted_keys; |
513 | | |
514 | 11.5k | assert(offset >= 0); |
515 | 11.5k | if (dest == NULL) |
516 | 0 | return NULL; |
517 | | |
518 | | /* Sort the keys so that we have a deterministic order on the indexes |
519 | | saved in the returned dictionary. These indexes are used as indexes |
520 | | into the free and cell var storage. Therefore if they aren't |
521 | | deterministic, then the generated bytecode is not deterministic. |
522 | | */ |
523 | 11.5k | sorted_keys = PyDict_Keys(src); |
524 | 11.5k | if (sorted_keys == NULL) { |
525 | 0 | Py_DECREF(dest); |
526 | 0 | return NULL; |
527 | 0 | } |
528 | 11.5k | if (PyList_Sort(sorted_keys) != 0) { |
529 | 0 | Py_DECREF(sorted_keys); |
530 | 0 | Py_DECREF(dest); |
531 | 0 | return NULL; |
532 | 0 | } |
533 | 11.5k | num_keys = PyList_GET_SIZE(sorted_keys); |
534 | | |
535 | 80.0k | for (key_i = 0; key_i < num_keys; key_i++) { |
536 | 68.5k | k = PyList_GET_ITEM(sorted_keys, key_i); |
537 | 68.5k | v = PyDict_GetItemWithError(src, k); |
538 | 68.5k | if (!v) { |
539 | 0 | if (!PyErr_Occurred()) { |
540 | 0 | PyErr_SetObject(PyExc_KeyError, k); |
541 | 0 | } |
542 | 0 | Py_DECREF(sorted_keys); |
543 | 0 | Py_DECREF(dest); |
544 | 0 | return NULL; |
545 | 0 | } |
546 | 68.5k | long vi = PyLong_AsLong(v); |
547 | 68.5k | if (vi == -1 && PyErr_Occurred()) { |
548 | 0 | Py_DECREF(sorted_keys); |
549 | 0 | Py_DECREF(dest); |
550 | 0 | return NULL; |
551 | 0 | } |
552 | 68.5k | if (SYMBOL_TO_SCOPE(vi) == scope_type || vi & flag) { |
553 | 1.16k | PyObject *item = PyLong_FromSsize_t(i); |
554 | 1.16k | if (item == NULL) { |
555 | 0 | Py_DECREF(sorted_keys); |
556 | 0 | Py_DECREF(dest); |
557 | 0 | return NULL; |
558 | 0 | } |
559 | 1.16k | i++; |
560 | 1.16k | if (PyDict_SetItem(dest, k, item) < 0) { |
561 | 0 | Py_DECREF(sorted_keys); |
562 | 0 | Py_DECREF(item); |
563 | 0 | Py_DECREF(dest); |
564 | 0 | return NULL; |
565 | 0 | } |
566 | 1.16k | Py_DECREF(item); |
567 | 1.16k | } |
568 | 68.5k | } |
569 | 11.5k | Py_DECREF(sorted_keys); |
570 | 11.5k | return dest; |
571 | 11.5k | } |
572 | | |
573 | | int |
574 | | _PyCompile_EnterScope(compiler *c, identifier name, int scope_type, |
575 | | void *key, int lineno, PyObject *private, |
576 | | _PyCompile_CodeUnitMetadata *umd) |
577 | 5.78k | { |
578 | 5.78k | struct compiler_unit *u; |
579 | 5.78k | u = (struct compiler_unit *)PyMem_Calloc(1, sizeof(struct compiler_unit)); |
580 | 5.78k | if (!u) { |
581 | 0 | PyErr_NoMemory(); |
582 | 0 | return ERROR; |
583 | 0 | } |
584 | 5.78k | u->u_scope_type = scope_type; |
585 | 5.78k | if (umd != NULL) { |
586 | 4.45k | u->u_metadata = *umd; |
587 | 4.45k | } |
588 | 1.33k | else { |
589 | 1.33k | u->u_metadata.u_argcount = 0; |
590 | 1.33k | u->u_metadata.u_posonlyargcount = 0; |
591 | 1.33k | u->u_metadata.u_kwonlyargcount = 0; |
592 | 1.33k | } |
593 | 5.78k | u->u_ste = _PySymtable_Lookup(c->c_st, key); |
594 | 5.78k | if (!u->u_ste) { |
595 | 0 | compiler_unit_free(u); |
596 | 0 | return ERROR; |
597 | 0 | } |
598 | 5.78k | u->u_metadata.u_name = Py_NewRef(name); |
599 | 5.78k | u->u_metadata.u_varnames = list2dict(u->u_ste->ste_varnames); |
600 | 5.78k | if (!u->u_metadata.u_varnames) { |
601 | 0 | compiler_unit_free(u); |
602 | 0 | return ERROR; |
603 | 0 | } |
604 | 5.78k | u->u_metadata.u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, DEF_COMP_CELL, 0); |
605 | 5.78k | if (!u->u_metadata.u_cellvars) { |
606 | 0 | compiler_unit_free(u); |
607 | 0 | return ERROR; |
608 | 0 | } |
609 | 5.78k | if (u->u_ste->ste_needs_class_closure) { |
610 | | /* Cook up an implicit __class__ cell. */ |
611 | 66 | Py_ssize_t res; |
612 | 66 | assert(u->u_scope_type == COMPILE_SCOPE_CLASS); |
613 | 66 | res = _PyCompile_DictAddObj(u->u_metadata.u_cellvars, &_Py_ID(__class__)); |
614 | 66 | if (res < 0) { |
615 | 0 | compiler_unit_free(u); |
616 | 0 | return ERROR; |
617 | 0 | } |
618 | 66 | } |
619 | 5.78k | if (u->u_ste->ste_needs_classdict) { |
620 | | /* Cook up an implicit __classdict__ cell. */ |
621 | 595 | Py_ssize_t res; |
622 | 595 | assert(u->u_scope_type == COMPILE_SCOPE_CLASS); |
623 | 595 | res = _PyCompile_DictAddObj(u->u_metadata.u_cellvars, &_Py_ID(__classdict__)); |
624 | 595 | if (res < 0) { |
625 | 0 | compiler_unit_free(u); |
626 | 0 | return ERROR; |
627 | 0 | } |
628 | 595 | } |
629 | 5.78k | if (u->u_ste->ste_has_conditional_annotations) { |
630 | | /* Cook up an implicit __conditional__annotations__ cell */ |
631 | 0 | Py_ssize_t res; |
632 | 0 | assert(u->u_scope_type == COMPILE_SCOPE_CLASS || u->u_scope_type == COMPILE_SCOPE_MODULE); |
633 | 0 | res = _PyCompile_DictAddObj(u->u_metadata.u_cellvars, &_Py_ID(__conditional_annotations__)); |
634 | 0 | if (res < 0) { |
635 | 0 | compiler_unit_free(u); |
636 | 0 | return ERROR; |
637 | 0 | } |
638 | 0 | } |
639 | | |
640 | 5.78k | u->u_metadata.u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS, |
641 | 5.78k | PyDict_GET_SIZE(u->u_metadata.u_cellvars)); |
642 | 5.78k | if (!u->u_metadata.u_freevars) { |
643 | 0 | compiler_unit_free(u); |
644 | 0 | return ERROR; |
645 | 0 | } |
646 | | |
647 | 5.78k | u->u_metadata.u_fasthidden = PyDict_New(); |
648 | 5.78k | if (!u->u_metadata.u_fasthidden) { |
649 | 0 | compiler_unit_free(u); |
650 | 0 | return ERROR; |
651 | 0 | } |
652 | | |
653 | 5.78k | u->u_nfblocks = 0; |
654 | 5.78k | u->u_in_inlined_comp = 0; |
655 | 5.78k | u->u_metadata.u_firstlineno = lineno; |
656 | 5.78k | u->u_metadata.u_consts = PyDict_New(); |
657 | 5.78k | if (!u->u_metadata.u_consts) { |
658 | 0 | compiler_unit_free(u); |
659 | 0 | return ERROR; |
660 | 0 | } |
661 | 5.78k | u->u_metadata.u_names = PyDict_New(); |
662 | 5.78k | if (!u->u_metadata.u_names) { |
663 | 0 | compiler_unit_free(u); |
664 | 0 | return ERROR; |
665 | 0 | } |
666 | | |
667 | 5.78k | u->u_deferred_annotations = NULL; |
668 | 5.78k | u->u_conditional_annotation_indices = NULL; |
669 | 5.78k | u->u_next_conditional_annotation_index = 0; |
670 | 5.78k | if (scope_type == COMPILE_SCOPE_CLASS) { |
671 | 1.02k | u->u_static_attributes = PySet_New(0); |
672 | 1.02k | if (!u->u_static_attributes) { |
673 | 0 | compiler_unit_free(u); |
674 | 0 | return ERROR; |
675 | 0 | } |
676 | 1.02k | } |
677 | 4.76k | else { |
678 | 4.76k | u->u_static_attributes = NULL; |
679 | 4.76k | } |
680 | | |
681 | 5.78k | u->u_instr_sequence = (instr_sequence*)_PyInstructionSequence_New(); |
682 | 5.78k | if (!u->u_instr_sequence) { |
683 | 0 | compiler_unit_free(u); |
684 | 0 | return ERROR; |
685 | 0 | } |
686 | 5.78k | u->u_stashed_instr_sequence = NULL; |
687 | | |
688 | | /* Push the old compiler_unit on the stack. */ |
689 | 5.78k | if (c->u) { |
690 | 5.47k | PyObject *capsule = PyCapsule_New(c->u, CAPSULE_NAME, NULL); |
691 | 5.47k | if (!capsule || PyList_Append(c->c_stack, capsule) < 0) { |
692 | 0 | Py_XDECREF(capsule); |
693 | 0 | compiler_unit_free(u); |
694 | 0 | return ERROR; |
695 | 0 | } |
696 | 5.47k | Py_DECREF(capsule); |
697 | 5.47k | if (private == NULL) { |
698 | 4.45k | private = c->u->u_private; |
699 | 4.45k | } |
700 | 5.47k | } |
701 | | |
702 | 5.78k | u->u_private = Py_XNewRef(private); |
703 | | |
704 | 5.78k | c->u = u; |
705 | 5.78k | if (scope_type != COMPILE_SCOPE_MODULE) { |
706 | 5.47k | RETURN_IF_ERROR(compiler_set_qualname(c)); |
707 | 5.47k | } |
708 | 5.78k | return SUCCESS; |
709 | 5.78k | } |
710 | | |
711 | | void |
712 | | _PyCompile_ExitScope(compiler *c) |
713 | 5.78k | { |
714 | | // Don't call PySequence_DelItem() with an exception raised |
715 | 5.78k | PyObject *exc = PyErr_GetRaisedException(); |
716 | | |
717 | 5.78k | instr_sequence *nested_seq = NULL; |
718 | 5.78k | if (c->c_save_nested_seqs) { |
719 | 0 | nested_seq = c->u->u_instr_sequence; |
720 | 0 | Py_INCREF(nested_seq); |
721 | 0 | } |
722 | 5.78k | compiler_unit_free(c->u); |
723 | | /* Restore c->u to the parent unit. */ |
724 | 5.78k | Py_ssize_t n = PyList_GET_SIZE(c->c_stack) - 1; |
725 | 5.78k | if (n >= 0) { |
726 | 5.47k | PyObject *capsule = PyList_GET_ITEM(c->c_stack, n); |
727 | 5.47k | c->u = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME); |
728 | 5.47k | assert(c->u); |
729 | | /* we are deleting from a list so this really shouldn't fail */ |
730 | 5.47k | if (PySequence_DelItem(c->c_stack, n) < 0) { |
731 | 0 | PyErr_FormatUnraisable("Exception ignored while removing " |
732 | 0 | "the last compiler stack item"); |
733 | 0 | } |
734 | 5.47k | if (nested_seq != NULL) { |
735 | 0 | if (_PyInstructionSequence_AddNested(c->u->u_instr_sequence, nested_seq) < 0) { |
736 | 0 | PyErr_FormatUnraisable("Exception ignored while appending " |
737 | 0 | "nested instruction sequence"); |
738 | 0 | } |
739 | 0 | } |
740 | 5.47k | } |
741 | 307 | else { |
742 | 307 | c->u = NULL; |
743 | 307 | } |
744 | 5.78k | Py_XDECREF(nested_seq); |
745 | | |
746 | 5.78k | PyErr_SetRaisedException(exc); |
747 | 5.78k | } |
748 | | |
749 | | /* |
750 | | * Frame block handling functions |
751 | | */ |
752 | | |
753 | | int |
754 | | _PyCompile_PushFBlock(compiler *c, location loc, |
755 | | fblocktype t, jump_target_label block_label, |
756 | | jump_target_label exit, void *datum) |
757 | 4.31k | { |
758 | 4.31k | fblockinfo *f; |
759 | 4.31k | if (c->u->u_nfblocks >= CO_MAXBLOCKS) { |
760 | 0 | return _PyCompile_Error(c, loc, "too many statically nested blocks"); |
761 | 0 | } |
762 | 4.31k | f = &c->u->u_fblock[c->u->u_nfblocks++]; |
763 | 4.31k | f->fb_type = t; |
764 | 4.31k | f->fb_block = block_label; |
765 | 4.31k | f->fb_loc = loc; |
766 | 4.31k | f->fb_exit = exit; |
767 | 4.31k | f->fb_datum = datum; |
768 | 4.31k | return SUCCESS; |
769 | 4.31k | } |
770 | | |
771 | | void |
772 | | _PyCompile_PopFBlock(compiler *c, fblocktype t, jump_target_label block_label) |
773 | 4.31k | { |
774 | 4.31k | struct compiler_unit *u = c->u; |
775 | 4.31k | assert(u->u_nfblocks > 0); |
776 | 4.31k | u->u_nfblocks--; |
777 | 4.31k | assert(u->u_fblock[u->u_nfblocks].fb_type == t); |
778 | 4.31k | assert(SAME_JUMP_TARGET_LABEL(u->u_fblock[u->u_nfblocks].fb_block, block_label)); |
779 | 4.31k | } |
780 | | |
781 | | fblockinfo * |
782 | | _PyCompile_TopFBlock(compiler *c) |
783 | 5.76k | { |
784 | 5.76k | if (c->u->u_nfblocks == 0) { |
785 | 4.53k | return NULL; |
786 | 4.53k | } |
787 | 1.23k | return &c->u->u_fblock[c->u->u_nfblocks - 1]; |
788 | 5.76k | } |
789 | | |
790 | | void |
791 | | _PyCompile_DeferredAnnotations(compiler *c, |
792 | | PyObject **deferred_annotations, |
793 | | PyObject **conditional_annotation_indices) |
794 | 1.28k | { |
795 | 1.28k | *deferred_annotations = Py_XNewRef(c->u->u_deferred_annotations); |
796 | 1.28k | *conditional_annotation_indices = Py_XNewRef(c->u->u_conditional_annotation_indices); |
797 | 1.28k | } |
798 | | |
799 | | static location |
800 | | start_location(asdl_stmt_seq *stmts) |
801 | 264 | { |
802 | 264 | if (asdl_seq_LEN(stmts) > 0) { |
803 | | /* Set current line number to the line number of first statement. |
804 | | * This way line number for SETUP_ANNOTATIONS will always |
805 | | * coincide with the line number of first "real" statement in module. |
806 | | * If body is empty, then lineno will be set later in the assembly stage. |
807 | | */ |
808 | 261 | stmt_ty st = (stmt_ty)asdl_seq_GET(stmts, 0); |
809 | 261 | return SRC_LOCATION_FROM_AST(st); |
810 | 261 | } |
811 | 3 | return (const _Py_SourceLocation){1, 1, 0, 0}; |
812 | 264 | } |
813 | | |
814 | | static int |
815 | | compiler_codegen(compiler *c, mod_ty mod) |
816 | 307 | { |
817 | 307 | RETURN_IF_ERROR(_PyCodegen_EnterAnonymousScope(c, mod)); |
818 | 307 | assert(c->u->u_scope_type == COMPILE_SCOPE_MODULE); |
819 | 307 | switch (mod->kind) { |
820 | 264 | case Module_kind: { |
821 | 264 | asdl_stmt_seq *stmts = mod->v.Module.body; |
822 | 264 | RETURN_IF_ERROR(_PyCodegen_Module(c, start_location(stmts), stmts, false)); |
823 | 264 | break; |
824 | 264 | } |
825 | 264 | case Interactive_kind: { |
826 | 0 | c->c_interactive = 1; |
827 | 0 | asdl_stmt_seq *stmts = mod->v.Interactive.body; |
828 | 0 | RETURN_IF_ERROR(_PyCodegen_Module(c, start_location(stmts), stmts, true)); |
829 | 0 | break; |
830 | 0 | } |
831 | 43 | case Expression_kind: { |
832 | 43 | RETURN_IF_ERROR(_PyCodegen_Expression(c, mod->v.Expression.body)); |
833 | 43 | break; |
834 | 43 | } |
835 | 43 | default: { |
836 | 0 | PyErr_Format(PyExc_SystemError, |
837 | 0 | "module kind %d should not be possible", |
838 | 0 | mod->kind); |
839 | 0 | return ERROR; |
840 | 43 | }} |
841 | 307 | return SUCCESS; |
842 | 307 | } |
843 | | |
844 | | static PyCodeObject * |
845 | | compiler_mod(compiler *c, mod_ty mod) |
846 | 307 | { |
847 | 307 | PyCodeObject *co = NULL; |
848 | 307 | int addNone = mod->kind != Expression_kind; |
849 | 307 | if (compiler_codegen(c, mod) < 0) { |
850 | 0 | goto finally; |
851 | 0 | } |
852 | 307 | co = _PyCompile_OptimizeAndAssemble(c, addNone); |
853 | 307 | finally: |
854 | 307 | _PyCompile_ExitScope(c); |
855 | 307 | return co; |
856 | 307 | } |
857 | | |
858 | | int |
859 | | _PyCompile_GetRefType(compiler *c, PyObject *name) |
860 | 935 | { |
861 | 935 | if (c->u->u_scope_type == COMPILE_SCOPE_CLASS && |
862 | 935 | (_PyUnicode_EqualToASCIIString(name, "__class__") || |
863 | 175 | _PyUnicode_EqualToASCIIString(name, "__classdict__") || |
864 | 175 | _PyUnicode_EqualToASCIIString(name, "__conditional_annotations__"))) { |
865 | 160 | return CELL; |
866 | 160 | } |
867 | 775 | PySTEntryObject *ste = c->u->u_ste; |
868 | 775 | int scope = _PyST_GetScope(ste, name); |
869 | 775 | if (scope == 0) { |
870 | 0 | PyErr_Format(PyExc_SystemError, |
871 | 0 | "_PyST_GetScope(name=%R) failed: " |
872 | 0 | "unknown scope in unit %S (%R); " |
873 | 0 | "symbols: %R; locals: %R; " |
874 | 0 | "globals: %R", |
875 | 0 | name, |
876 | 0 | c->u->u_metadata.u_name, ste->ste_id, |
877 | 0 | ste->ste_symbols, c->u->u_metadata.u_varnames, |
878 | 0 | c->u->u_metadata.u_names); |
879 | 0 | return ERROR; |
880 | 0 | } |
881 | 775 | return scope; |
882 | 775 | } |
883 | | |
884 | | static int |
885 | | dict_lookup_arg(PyObject *dict, PyObject *name) |
886 | 1.44k | { |
887 | 1.44k | PyObject *v = PyDict_GetItemWithError(dict, name); |
888 | 1.44k | if (v == NULL) { |
889 | 0 | return ERROR; |
890 | 0 | } |
891 | 1.44k | return PyLong_AsLong(v); |
892 | 1.44k | } |
893 | | |
894 | | int |
895 | | _PyCompile_LookupCellvar(compiler *c, PyObject *name) |
896 | 661 | { |
897 | 661 | assert(c->u->u_metadata.u_cellvars); |
898 | 661 | return dict_lookup_arg(c->u->u_metadata.u_cellvars, name); |
899 | 661 | } |
900 | | |
901 | | int |
902 | | _PyCompile_LookupArg(compiler *c, PyCodeObject *co, PyObject *name) |
903 | 783 | { |
904 | | /* Special case: If a class contains a method with a |
905 | | * free variable that has the same name as a method, |
906 | | * the name will be considered free *and* local in the |
907 | | * class. It should be handled by the closure, as |
908 | | * well as by the normal name lookup logic. |
909 | | */ |
910 | 783 | int reftype = _PyCompile_GetRefType(c, name); |
911 | 783 | if (reftype == -1) { |
912 | 0 | return ERROR; |
913 | 0 | } |
914 | 783 | int arg; |
915 | 783 | if (reftype == CELL) { |
916 | 739 | arg = dict_lookup_arg(c->u->u_metadata.u_cellvars, name); |
917 | 739 | } |
918 | 44 | else { |
919 | 44 | arg = dict_lookup_arg(c->u->u_metadata.u_freevars, name); |
920 | 44 | } |
921 | 783 | if (arg == -1 && !PyErr_Occurred()) { |
922 | 0 | PyObject *freevars = _PyCode_GetFreevars(co); |
923 | 0 | if (freevars == NULL) { |
924 | 0 | PyErr_Clear(); |
925 | 0 | } |
926 | 0 | PyErr_Format(PyExc_SystemError, |
927 | 0 | "compiler_lookup_arg(name=%R) with reftype=%d failed in %S; " |
928 | 0 | "freevars of code %S: %R", |
929 | 0 | name, |
930 | 0 | reftype, |
931 | 0 | c->u->u_metadata.u_name, |
932 | 0 | co->co_name, |
933 | 0 | freevars); |
934 | 0 | Py_XDECREF(freevars); |
935 | 0 | return ERROR; |
936 | 0 | } |
937 | 783 | return arg; |
938 | 783 | } |
939 | | |
940 | | PyObject * |
941 | | _PyCompile_StaticAttributesAsTuple(compiler *c) |
942 | 1.02k | { |
943 | 1.02k | assert(c->u->u_static_attributes); |
944 | 1.02k | PyObject *static_attributes_unsorted = PySequence_List(c->u->u_static_attributes); |
945 | 1.02k | if (static_attributes_unsorted == NULL) { |
946 | 0 | return NULL; |
947 | 0 | } |
948 | 1.02k | if (PyList_Sort(static_attributes_unsorted) != 0) { |
949 | 0 | Py_DECREF(static_attributes_unsorted); |
950 | 0 | return NULL; |
951 | 0 | } |
952 | 1.02k | PyObject *static_attributes = PySequence_Tuple(static_attributes_unsorted); |
953 | 1.02k | Py_DECREF(static_attributes_unsorted); |
954 | 1.02k | return static_attributes; |
955 | 1.02k | } |
956 | | |
957 | | int |
958 | | _PyCompile_ResolveNameop(compiler *c, PyObject *mangled, int scope, |
959 | | _PyCompile_optype *optype, Py_ssize_t *arg) |
960 | 84.5k | { |
961 | 84.5k | PyObject *dict = c->u->u_metadata.u_names; |
962 | 84.5k | *optype = COMPILE_OP_NAME; |
963 | | |
964 | 84.5k | assert(scope >= 0); |
965 | 84.5k | switch (scope) { |
966 | 1.20k | case FREE: |
967 | 1.20k | dict = c->u->u_metadata.u_freevars; |
968 | 1.20k | *optype = COMPILE_OP_DEREF; |
969 | 1.20k | break; |
970 | 794 | case CELL: |
971 | 794 | dict = c->u->u_metadata.u_cellvars; |
972 | 794 | *optype = COMPILE_OP_DEREF; |
973 | 794 | break; |
974 | 61.0k | case LOCAL: |
975 | 61.0k | if (_PyST_IsFunctionLike(c->u->u_ste)) { |
976 | 50.4k | *optype = COMPILE_OP_FAST; |
977 | 50.4k | } |
978 | 10.5k | else { |
979 | 10.5k | PyObject *item; |
980 | 10.5k | RETURN_IF_ERROR(PyDict_GetItemRef(c->u->u_metadata.u_fasthidden, mangled, |
981 | 10.5k | &item)); |
982 | 10.5k | if (item == Py_True) { |
983 | 21 | *optype = COMPILE_OP_FAST; |
984 | 21 | } |
985 | 10.5k | Py_XDECREF(item); |
986 | 10.5k | } |
987 | 61.0k | break; |
988 | 61.0k | case GLOBAL_IMPLICIT: |
989 | 15.1k | if (_PyST_IsFunctionLike(c->u->u_ste)) { |
990 | 13.2k | *optype = COMPILE_OP_GLOBAL; |
991 | 13.2k | } |
992 | 15.1k | break; |
993 | 145 | case GLOBAL_EXPLICIT: |
994 | 145 | *optype = COMPILE_OP_GLOBAL; |
995 | 145 | break; |
996 | 6.21k | default: |
997 | | /* scope can be 0 */ |
998 | 6.21k | break; |
999 | 84.5k | } |
1000 | 84.5k | if (*optype != COMPILE_OP_FAST) { |
1001 | 34.0k | *arg = _PyCompile_DictAddObj(dict, mangled); |
1002 | 34.0k | RETURN_IF_ERROR(*arg); |
1003 | 34.0k | } |
1004 | 84.5k | return SUCCESS; |
1005 | 84.5k | } |
1006 | | |
1007 | | int |
1008 | | _PyCompile_TweakInlinedComprehensionScopes(compiler *c, location loc, |
1009 | | PySTEntryObject *entry, |
1010 | | _PyCompile_InlinedComprehensionState *state) |
1011 | 185 | { |
1012 | 185 | int in_class_block = (c->u->u_ste->ste_type == ClassBlock) && !c->u->u_in_inlined_comp; |
1013 | 185 | c->u->u_in_inlined_comp++; |
1014 | | |
1015 | 185 | PyObject *k, *v; |
1016 | 185 | Py_ssize_t pos = 0; |
1017 | 786 | while (PyDict_Next(entry->ste_symbols, &pos, &k, &v)) { |
1018 | 601 | long symbol = PyLong_AsLong(v); |
1019 | 601 | assert(symbol >= 0 || PyErr_Occurred()); |
1020 | 601 | RETURN_IF_ERROR(symbol); |
1021 | 601 | long scope = SYMBOL_TO_SCOPE(symbol); |
1022 | | |
1023 | 601 | long outsymbol = _PyST_GetSymbol(c->u->u_ste, k); |
1024 | 601 | RETURN_IF_ERROR(outsymbol); |
1025 | 601 | long outsc = SYMBOL_TO_SCOPE(outsymbol); |
1026 | | |
1027 | | // If a name has different scope inside than outside the comprehension, |
1028 | | // we need to temporarily handle it with the right scope while |
1029 | | // compiling the comprehension. If it's free in the comprehension |
1030 | | // scope, no special handling; it should be handled the same as the |
1031 | | // enclosing scope. (If it's free in outer scope and cell in inner |
1032 | | // scope, we can't treat it as both cell and free in the same function, |
1033 | | // but treating it as free throughout is fine; it's *_DEREF |
1034 | | // either way.) |
1035 | 601 | if ((scope != outsc && scope != FREE && !(scope == CELL && outsc == FREE)) |
1036 | 601 | || in_class_block) { |
1037 | 198 | if (state->temp_symbols == NULL) { |
1038 | 185 | state->temp_symbols = PyDict_New(); |
1039 | 185 | if (state->temp_symbols == NULL) { |
1040 | 0 | return ERROR; |
1041 | 0 | } |
1042 | 185 | } |
1043 | | // update the symbol to the in-comprehension version and save |
1044 | | // the outer version; we'll restore it after running the |
1045 | | // comprehension |
1046 | 198 | if (PyDict_SetItem(c->u->u_ste->ste_symbols, k, v) < 0) { |
1047 | 0 | return ERROR; |
1048 | 0 | } |
1049 | 198 | PyObject *outv = PyLong_FromLong(outsymbol); |
1050 | 198 | if (outv == NULL) { |
1051 | 0 | return ERROR; |
1052 | 0 | } |
1053 | 198 | int res = PyDict_SetItem(state->temp_symbols, k, outv); |
1054 | 198 | Py_DECREF(outv); |
1055 | 198 | RETURN_IF_ERROR(res); |
1056 | 198 | } |
1057 | | // locals handling for names bound in comprehension (DEF_LOCAL | |
1058 | | // DEF_NONLOCAL occurs in assignment expression to nonlocal) |
1059 | 601 | if ((symbol & DEF_LOCAL && !(symbol & DEF_NONLOCAL)) || in_class_block) { |
1060 | 240 | if (!_PyST_IsFunctionLike(c->u->u_ste)) { |
1061 | | // non-function scope: override this name to use fast locals |
1062 | 9 | PyObject *orig; |
1063 | 9 | if (PyDict_GetItemRef(c->u->u_metadata.u_fasthidden, k, &orig) < 0) { |
1064 | 0 | return ERROR; |
1065 | 0 | } |
1066 | 9 | assert(orig == NULL || orig == Py_True || orig == Py_False); |
1067 | 9 | if (orig != Py_True) { |
1068 | 9 | if (PyDict_SetItem(c->u->u_metadata.u_fasthidden, k, Py_True) < 0) { |
1069 | 0 | return ERROR; |
1070 | 0 | } |
1071 | 9 | if (state->fast_hidden == NULL) { |
1072 | 7 | state->fast_hidden = PySet_New(NULL); |
1073 | 7 | if (state->fast_hidden == NULL) { |
1074 | 0 | return ERROR; |
1075 | 0 | } |
1076 | 7 | } |
1077 | 9 | if (PySet_Add(state->fast_hidden, k) < 0) { |
1078 | 0 | return ERROR; |
1079 | 0 | } |
1080 | 9 | } |
1081 | 9 | } |
1082 | 240 | } |
1083 | 601 | } |
1084 | 185 | return SUCCESS; |
1085 | 185 | } |
1086 | | |
1087 | | int |
1088 | | _PyCompile_RevertInlinedComprehensionScopes(compiler *c, location loc, |
1089 | | _PyCompile_InlinedComprehensionState *state) |
1090 | 185 | { |
1091 | 185 | c->u->u_in_inlined_comp--; |
1092 | 185 | if (state->temp_symbols) { |
1093 | 185 | PyObject *k, *v; |
1094 | 185 | Py_ssize_t pos = 0; |
1095 | 383 | while (PyDict_Next(state->temp_symbols, &pos, &k, &v)) { |
1096 | 198 | if (PyDict_SetItem(c->u->u_ste->ste_symbols, k, v)) { |
1097 | 0 | return ERROR; |
1098 | 0 | } |
1099 | 198 | } |
1100 | 185 | Py_CLEAR(state->temp_symbols); |
1101 | 185 | } |
1102 | 185 | if (state->fast_hidden) { |
1103 | 16 | while (PySet_Size(state->fast_hidden) > 0) { |
1104 | 9 | PyObject *k = PySet_Pop(state->fast_hidden); |
1105 | 9 | if (k == NULL) { |
1106 | 0 | return ERROR; |
1107 | 0 | } |
1108 | | // we set to False instead of clearing, so we can track which names |
1109 | | // were temporarily fast-locals and should use CO_FAST_HIDDEN |
1110 | 9 | if (PyDict_SetItem(c->u->u_metadata.u_fasthidden, k, Py_False)) { |
1111 | 0 | Py_DECREF(k); |
1112 | 0 | return ERROR; |
1113 | 0 | } |
1114 | 9 | Py_DECREF(k); |
1115 | 9 | } |
1116 | 7 | Py_CLEAR(state->fast_hidden); |
1117 | 7 | } |
1118 | 185 | return SUCCESS; |
1119 | 185 | } |
1120 | | |
1121 | | void |
1122 | | _PyCompile_EnterConditionalBlock(struct _PyCompiler *c) |
1123 | 8.40k | { |
1124 | 8.40k | c->u->u_in_conditional_block++; |
1125 | 8.40k | } |
1126 | | |
1127 | | void |
1128 | | _PyCompile_LeaveConditionalBlock(struct _PyCompiler *c) |
1129 | 8.40k | { |
1130 | 8.40k | assert(c->u->u_in_conditional_block > 0); |
1131 | 8.40k | c->u->u_in_conditional_block--; |
1132 | 8.40k | } |
1133 | | |
1134 | | int |
1135 | | _PyCompile_AddDeferredAnnotation(compiler *c, stmt_ty s, |
1136 | | PyObject **conditional_annotation_index) |
1137 | 0 | { |
1138 | 0 | if (c->u->u_deferred_annotations == NULL) { |
1139 | 0 | c->u->u_deferred_annotations = PyList_New(0); |
1140 | 0 | if (c->u->u_deferred_annotations == NULL) { |
1141 | 0 | return ERROR; |
1142 | 0 | } |
1143 | 0 | } |
1144 | 0 | if (c->u->u_conditional_annotation_indices == NULL) { |
1145 | 0 | c->u->u_conditional_annotation_indices = PyList_New(0); |
1146 | 0 | if (c->u->u_conditional_annotation_indices == NULL) { |
1147 | 0 | return ERROR; |
1148 | 0 | } |
1149 | 0 | } |
1150 | 0 | PyObject *ptr = PyLong_FromVoidPtr((void *)s); |
1151 | 0 | if (ptr == NULL) { |
1152 | 0 | return ERROR; |
1153 | 0 | } |
1154 | 0 | if (PyList_Append(c->u->u_deferred_annotations, ptr) < 0) { |
1155 | 0 | Py_DECREF(ptr); |
1156 | 0 | return ERROR; |
1157 | 0 | } |
1158 | 0 | Py_DECREF(ptr); |
1159 | 0 | PyObject *index; |
1160 | 0 | if (c->u->u_scope_type == COMPILE_SCOPE_MODULE || c->u->u_in_conditional_block) { |
1161 | 0 | index = PyLong_FromLong(c->u->u_next_conditional_annotation_index); |
1162 | 0 | if (index == NULL) { |
1163 | 0 | return ERROR; |
1164 | 0 | } |
1165 | 0 | *conditional_annotation_index = Py_NewRef(index); |
1166 | 0 | c->u->u_next_conditional_annotation_index++; |
1167 | 0 | } |
1168 | 0 | else { |
1169 | 0 | index = PyLong_FromLong(-1); |
1170 | 0 | if (index == NULL) { |
1171 | 0 | return ERROR; |
1172 | 0 | } |
1173 | 0 | } |
1174 | 0 | int rc = PyList_Append(c->u->u_conditional_annotation_indices, index); |
1175 | 0 | Py_DECREF(index); |
1176 | 0 | RETURN_IF_ERROR(rc); |
1177 | 0 | return SUCCESS; |
1178 | 0 | } |
1179 | | |
1180 | | /* Raises a SyntaxError and returns ERROR. |
1181 | | * If something goes wrong, a different exception may be raised. |
1182 | | */ |
1183 | | int |
1184 | | _PyCompile_Error(compiler *c, location loc, const char *format, ...) |
1185 | 0 | { |
1186 | 0 | va_list vargs; |
1187 | 0 | va_start(vargs, format); |
1188 | 0 | PyObject *msg = PyUnicode_FromFormatV(format, vargs); |
1189 | 0 | va_end(vargs); |
1190 | 0 | if (msg == NULL) { |
1191 | 0 | return ERROR; |
1192 | 0 | } |
1193 | 0 | _PyErr_RaiseSyntaxError(msg, c->c_filename, loc.lineno, loc.col_offset + 1, |
1194 | 0 | loc.end_lineno, loc.end_col_offset + 1); |
1195 | 0 | Py_DECREF(msg); |
1196 | 0 | return ERROR; |
1197 | 0 | } |
1198 | | |
1199 | | /* Emits a SyntaxWarning and returns 0 on success. |
1200 | | If a SyntaxWarning raised as error, replaces it with a SyntaxError |
1201 | | and returns -1. |
1202 | | */ |
1203 | | int |
1204 | | _PyCompile_Warn(compiler *c, location loc, const char *format, ...) |
1205 | 0 | { |
1206 | 0 | va_list vargs; |
1207 | 0 | va_start(vargs, format); |
1208 | 0 | PyObject *msg = PyUnicode_FromFormatV(format, vargs); |
1209 | 0 | va_end(vargs); |
1210 | 0 | if (msg == NULL) { |
1211 | 0 | return ERROR; |
1212 | 0 | } |
1213 | 0 | int ret = _PyErr_EmitSyntaxWarning(msg, c->c_filename, loc.lineno, loc.col_offset + 1, |
1214 | 0 | loc.end_lineno, loc.end_col_offset + 1); |
1215 | 0 | Py_DECREF(msg); |
1216 | 0 | return ret; |
1217 | 0 | } |
1218 | | |
1219 | | PyObject * |
1220 | | _PyCompile_Mangle(compiler *c, PyObject *name) |
1221 | 0 | { |
1222 | 0 | return _Py_Mangle(c->u->u_private, name); |
1223 | 0 | } |
1224 | | |
1225 | | PyObject * |
1226 | | _PyCompile_MaybeMangle(compiler *c, PyObject *name) |
1227 | 105k | { |
1228 | 105k | return _Py_MaybeMangle(c->u->u_private, c->u->u_ste, name); |
1229 | 105k | } |
1230 | | |
1231 | | instr_sequence * |
1232 | | _PyCompile_InstrSequence(compiler *c) |
1233 | 385k | { |
1234 | 385k | return c->u->u_instr_sequence; |
1235 | 385k | } |
1236 | | |
1237 | | int |
1238 | | _PyCompile_StartAnnotationSetup(struct _PyCompiler *c) |
1239 | 0 | { |
1240 | 0 | instr_sequence *new_seq = (instr_sequence *)_PyInstructionSequence_New(); |
1241 | 0 | if (new_seq == NULL) { |
1242 | 0 | return ERROR; |
1243 | 0 | } |
1244 | 0 | assert(c->u->u_stashed_instr_sequence == NULL); |
1245 | 0 | c->u->u_stashed_instr_sequence = c->u->u_instr_sequence; |
1246 | 0 | c->u->u_instr_sequence = new_seq; |
1247 | 0 | return SUCCESS; |
1248 | 0 | } |
1249 | | |
1250 | | int |
1251 | | _PyCompile_EndAnnotationSetup(struct _PyCompiler *c) |
1252 | 0 | { |
1253 | 0 | assert(c->u->u_stashed_instr_sequence != NULL); |
1254 | 0 | instr_sequence *parent_seq = c->u->u_stashed_instr_sequence; |
1255 | 0 | instr_sequence *anno_seq = c->u->u_instr_sequence; |
1256 | 0 | c->u->u_stashed_instr_sequence = NULL; |
1257 | 0 | c->u->u_instr_sequence = parent_seq; |
1258 | 0 | if (_PyInstructionSequence_SetAnnotationsCode(parent_seq, anno_seq) == ERROR) { |
1259 | 0 | Py_DECREF(anno_seq); |
1260 | 0 | return ERROR; |
1261 | 0 | } |
1262 | 0 | return SUCCESS; |
1263 | 0 | } |
1264 | | |
1265 | | |
1266 | | int |
1267 | | _PyCompile_FutureFeatures(compiler *c) |
1268 | 2.58k | { |
1269 | 2.58k | return c->c_future.ff_features; |
1270 | 2.58k | } |
1271 | | |
1272 | | struct symtable * |
1273 | | _PyCompile_Symtable(compiler *c) |
1274 | 10.8k | { |
1275 | 10.8k | return c->c_st; |
1276 | 10.8k | } |
1277 | | |
1278 | | PySTEntryObject * |
1279 | | _PyCompile_SymtableEntry(compiler *c) |
1280 | 122k | { |
1281 | 122k | return c->u->u_ste; |
1282 | 122k | } |
1283 | | |
1284 | | int |
1285 | | _PyCompile_OptimizationLevel(compiler *c) |
1286 | 108 | { |
1287 | 108 | return c->c_optimize; |
1288 | 108 | } |
1289 | | |
1290 | | int |
1291 | | _PyCompile_IsInteractiveTopLevel(compiler *c) |
1292 | 3.68k | { |
1293 | 3.68k | assert(c->c_stack != NULL); |
1294 | 3.68k | assert(PyList_CheckExact(c->c_stack)); |
1295 | 3.68k | bool is_nested_scope = PyList_GET_SIZE(c->c_stack) > 0; |
1296 | 3.68k | return c->c_interactive && !is_nested_scope; |
1297 | 3.68k | } |
1298 | | |
1299 | | int |
1300 | | _PyCompile_ScopeType(compiler *c) |
1301 | 30 | { |
1302 | 30 | return c->u->u_scope_type; |
1303 | 30 | } |
1304 | | |
1305 | | int |
1306 | | _PyCompile_IsInInlinedComp(compiler *c) |
1307 | 2.29k | { |
1308 | 2.29k | return c->u->u_in_inlined_comp; |
1309 | 2.29k | } |
1310 | | |
1311 | | PyObject * |
1312 | | _PyCompile_Qualname(compiler *c) |
1313 | 1.02k | { |
1314 | 1.02k | assert(c->u->u_metadata.u_qualname); |
1315 | 1.02k | return c->u->u_metadata.u_qualname; |
1316 | 1.02k | } |
1317 | | |
1318 | | _PyCompile_CodeUnitMetadata * |
1319 | | _PyCompile_Metadata(compiler *c) |
1320 | 72.7k | { |
1321 | 72.7k | return &c->u->u_metadata; |
1322 | 72.7k | } |
1323 | | |
1324 | | // Merge *obj* with constant cache, without recursion. |
1325 | | int |
1326 | | _PyCompile_ConstCacheMergeOne(PyObject *const_cache, PyObject **obj) |
1327 | 36.7k | { |
1328 | 36.7k | PyObject *key = const_cache_insert(const_cache, *obj, false); |
1329 | 36.7k | if (key == NULL) { |
1330 | 0 | return ERROR; |
1331 | 0 | } |
1332 | 36.7k | if (PyTuple_CheckExact(key)) { |
1333 | 36.2k | PyObject *item = PyTuple_GET_ITEM(key, 1); |
1334 | 36.2k | Py_SETREF(*obj, Py_NewRef(item)); |
1335 | 36.2k | Py_DECREF(key); |
1336 | 36.2k | } |
1337 | 485 | else { |
1338 | 485 | Py_SETREF(*obj, key); |
1339 | 485 | } |
1340 | 36.7k | return SUCCESS; |
1341 | 36.7k | } |
1342 | | |
1343 | | static PyObject * |
1344 | | consts_dict_keys_inorder(PyObject *dict) |
1345 | 5.78k | { |
1346 | 5.78k | PyObject *consts, *k, *v; |
1347 | 5.78k | Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict); |
1348 | | |
1349 | 5.78k | consts = PyList_New(size); /* PyCode_Optimize() requires a list */ |
1350 | 5.78k | if (consts == NULL) |
1351 | 0 | return NULL; |
1352 | 48.7k | while (PyDict_Next(dict, &pos, &k, &v)) { |
1353 | 42.9k | assert(PyLong_CheckExact(v)); |
1354 | 42.9k | i = PyLong_AsLong(v); |
1355 | | /* The keys of the dictionary can be tuples wrapping a constant. |
1356 | | * (see _PyCompile_DictAddObj and _PyCode_ConstantKey). In that case |
1357 | | * the object we want is always second. */ |
1358 | 42.9k | if (PyTuple_CheckExact(k)) { |
1359 | 3.29k | k = PyTuple_GET_ITEM(k, 1); |
1360 | 3.29k | } |
1361 | 42.9k | assert(i < size); |
1362 | 42.9k | assert(i >= 0); |
1363 | 42.9k | PyList_SET_ITEM(consts, i, Py_NewRef(k)); |
1364 | 42.9k | } |
1365 | 5.78k | return consts; |
1366 | 5.78k | } |
1367 | | |
1368 | | static int |
1369 | | compute_code_flags(compiler *c) |
1370 | 5.78k | { |
1371 | 5.78k | PySTEntryObject *ste = c->u->u_ste; |
1372 | 5.78k | int flags = 0; |
1373 | 5.78k | if (_PyST_IsFunctionLike(ste)) { |
1374 | 4.45k | flags |= CO_NEWLOCALS | CO_OPTIMIZED; |
1375 | 4.45k | if (ste->ste_nested) |
1376 | 430 | flags |= CO_NESTED; |
1377 | 4.45k | if (ste->ste_generator && !ste->ste_coroutine) |
1378 | 291 | flags |= CO_GENERATOR; |
1379 | 4.45k | if (ste->ste_generator && ste->ste_coroutine) |
1380 | 1 | flags |= CO_ASYNC_GENERATOR; |
1381 | 4.45k | if (ste->ste_varargs) |
1382 | 168 | flags |= CO_VARARGS; |
1383 | 4.45k | if (ste->ste_varkeywords) |
1384 | 131 | flags |= CO_VARKEYWORDS; |
1385 | 4.45k | if (ste->ste_has_docstring) |
1386 | 1.44k | flags |= CO_HAS_DOCSTRING; |
1387 | 4.45k | if (ste->ste_method) |
1388 | 2.75k | flags |= CO_METHOD; |
1389 | 4.45k | } |
1390 | | |
1391 | 5.78k | if (ste->ste_coroutine && !ste->ste_generator) { |
1392 | 15 | flags |= CO_COROUTINE; |
1393 | 15 | } |
1394 | | |
1395 | | /* (Only) inherit compilerflags in PyCF_MASK */ |
1396 | 5.78k | flags |= (c->c_flags.cf_flags & PyCF_MASK); |
1397 | | |
1398 | 5.78k | return flags; |
1399 | 5.78k | } |
1400 | | |
1401 | | static PyCodeObject * |
1402 | | optimize_and_assemble_code_unit(struct compiler_unit *u, PyObject *const_cache, |
1403 | | int code_flags, PyObject *filename) |
1404 | 5.78k | { |
1405 | 5.78k | cfg_builder *g = NULL; |
1406 | 5.78k | instr_sequence optimized_instrs; |
1407 | 5.78k | memset(&optimized_instrs, 0, sizeof(instr_sequence)); |
1408 | | |
1409 | 5.78k | PyCodeObject *co = NULL; |
1410 | 5.78k | PyObject *consts = consts_dict_keys_inorder(u->u_metadata.u_consts); |
1411 | 5.78k | if (consts == NULL) { |
1412 | 0 | goto error; |
1413 | 0 | } |
1414 | 5.78k | g = _PyCfg_FromInstructionSequence(u->u_instr_sequence); |
1415 | 5.78k | if (g == NULL) { |
1416 | 0 | goto error; |
1417 | 0 | } |
1418 | 5.78k | int nlocals = (int)PyDict_GET_SIZE(u->u_metadata.u_varnames); |
1419 | 5.78k | int nparams = (int)PyList_GET_SIZE(u->u_ste->ste_varnames); |
1420 | 5.78k | assert(u->u_metadata.u_firstlineno); |
1421 | | |
1422 | 5.78k | if (_PyCfg_OptimizeCodeUnit(g, consts, const_cache, nlocals, |
1423 | 5.78k | nparams, u->u_metadata.u_firstlineno) < 0) { |
1424 | 0 | goto error; |
1425 | 0 | } |
1426 | | |
1427 | 5.78k | int stackdepth; |
1428 | 5.78k | int nlocalsplus; |
1429 | 5.78k | if (_PyCfg_OptimizedCfgToInstructionSequence(g, &u->u_metadata, code_flags, |
1430 | 5.78k | &stackdepth, &nlocalsplus, |
1431 | 5.78k | &optimized_instrs) < 0) { |
1432 | 0 | goto error; |
1433 | 0 | } |
1434 | | |
1435 | | /** Assembly **/ |
1436 | 5.78k | co = _PyAssemble_MakeCodeObject(&u->u_metadata, const_cache, consts, |
1437 | 5.78k | stackdepth, &optimized_instrs, nlocalsplus, |
1438 | 5.78k | code_flags, filename); |
1439 | | |
1440 | 5.78k | error: |
1441 | 5.78k | Py_XDECREF(consts); |
1442 | 5.78k | PyInstructionSequence_Fini(&optimized_instrs); |
1443 | 5.78k | _PyCfgBuilder_Free(g); |
1444 | 5.78k | return co; |
1445 | 5.78k | } |
1446 | | |
1447 | | |
1448 | | PyCodeObject * |
1449 | | _PyCompile_OptimizeAndAssemble(compiler *c, int addNone) |
1450 | 5.78k | { |
1451 | 5.78k | struct compiler_unit *u = c->u; |
1452 | 5.78k | PyObject *const_cache = c->c_const_cache; |
1453 | 5.78k | PyObject *filename = c->c_filename; |
1454 | | |
1455 | 5.78k | int code_flags = compute_code_flags(c); |
1456 | 5.78k | if (code_flags < 0) { |
1457 | 0 | return NULL; |
1458 | 0 | } |
1459 | | |
1460 | 5.78k | if (_PyCodegen_AddReturnAtEnd(c, addNone) < 0) { |
1461 | 0 | return NULL; |
1462 | 0 | } |
1463 | | |
1464 | 5.78k | return optimize_and_assemble_code_unit(u, const_cache, code_flags, filename); |
1465 | 5.78k | } |
1466 | | |
1467 | | PyCodeObject * |
1468 | | _PyAST_Compile(mod_ty mod, PyObject *filename, PyCompilerFlags *pflags, |
1469 | | int optimize, PyArena *arena) |
1470 | 307 | { |
1471 | 307 | assert(!PyErr_Occurred()); |
1472 | 307 | compiler *c = new_compiler(mod, filename, pflags, optimize, arena); |
1473 | 307 | if (c == NULL) { |
1474 | 0 | return NULL; |
1475 | 0 | } |
1476 | | |
1477 | 307 | PyCodeObject *co = compiler_mod(c, mod); |
1478 | 307 | compiler_free(c); |
1479 | 307 | assert(co || PyErr_Occurred()); |
1480 | 307 | return co; |
1481 | 307 | } |
1482 | | |
1483 | | int |
1484 | | _PyCompile_AstPreprocess(mod_ty mod, PyObject *filename, PyCompilerFlags *cf, |
1485 | | int optimize, PyArena *arena, int no_const_folding) |
1486 | 7.59k | { |
1487 | 7.59k | _PyFutureFeatures future; |
1488 | 7.59k | if (!_PyFuture_FromAST(mod, filename, &future)) { |
1489 | 23 | return -1; |
1490 | 23 | } |
1491 | 7.57k | int flags = future.ff_features | cf->cf_flags; |
1492 | 7.57k | if (optimize == -1) { |
1493 | 7.57k | optimize = _Py_GetConfig()->optimization_level; |
1494 | 7.57k | } |
1495 | 7.57k | if (!_PyAST_Preprocess(mod, arena, filename, optimize, flags, no_const_folding)) { |
1496 | 0 | return -1; |
1497 | 0 | } |
1498 | 7.57k | return 0; |
1499 | 7.57k | } |
1500 | | |
1501 | | // C implementation of inspect.cleandoc() |
1502 | | // |
1503 | | // Difference from inspect.cleandoc(): |
1504 | | // - Do not remove leading and trailing blank lines to keep lineno. |
1505 | | PyObject * |
1506 | | _PyCompile_CleanDoc(PyObject *doc) |
1507 | 1.88k | { |
1508 | 1.88k | doc = PyObject_CallMethod(doc, "expandtabs", NULL); |
1509 | 1.88k | if (doc == NULL) { |
1510 | 0 | return NULL; |
1511 | 0 | } |
1512 | | |
1513 | 1.88k | Py_ssize_t doc_size; |
1514 | 1.88k | const char *doc_utf8 = PyUnicode_AsUTF8AndSize(doc, &doc_size); |
1515 | 1.88k | if (doc_utf8 == NULL) { |
1516 | 0 | Py_DECREF(doc); |
1517 | 0 | return NULL; |
1518 | 0 | } |
1519 | 1.88k | const char *p = doc_utf8; |
1520 | 1.88k | const char *pend = p + doc_size; |
1521 | | |
1522 | | // First pass: find minimum indentation of any non-blank lines |
1523 | | // after first line. |
1524 | 85.1k | while (p < pend && *p++ != '\n') { |
1525 | 83.2k | } |
1526 | | |
1527 | 1.88k | Py_ssize_t margin = PY_SSIZE_T_MAX; |
1528 | 12.9k | while (p < pend) { |
1529 | 11.1k | const char *s = p; |
1530 | 66.6k | while (*p == ' ') p++; |
1531 | 11.1k | if (p < pend && *p != '\n') { |
1532 | 7.37k | margin = Py_MIN(margin, p - s); |
1533 | 7.37k | } |
1534 | 364k | while (p < pend && *p++ != '\n') { |
1535 | 353k | } |
1536 | 11.1k | } |
1537 | 1.88k | if (margin == PY_SSIZE_T_MAX) { |
1538 | 651 | margin = 0; |
1539 | 651 | } |
1540 | | |
1541 | | // Second pass: write cleandoc into buff. |
1542 | | |
1543 | | // copy first line without leading spaces. |
1544 | 1.88k | p = doc_utf8; |
1545 | 2.00k | while (*p == ' ') { |
1546 | 127 | p++; |
1547 | 127 | } |
1548 | 1.88k | if (p == doc_utf8 && margin == 0 ) { |
1549 | | // doc is already clean. |
1550 | 632 | return doc; |
1551 | 632 | } |
1552 | | |
1553 | 1.25k | char *buff = PyMem_Malloc(doc_size); |
1554 | 1.25k | if (buff == NULL){ |
1555 | 0 | Py_DECREF(doc); |
1556 | 0 | PyErr_NoMemory(); |
1557 | 0 | return NULL; |
1558 | 0 | } |
1559 | | |
1560 | 1.25k | char *w = buff; |
1561 | | |
1562 | 55.6k | while (p < pend) { |
1563 | 55.6k | int ch = *w++ = *p++; |
1564 | 55.6k | if (ch == '\n') { |
1565 | 1.25k | break; |
1566 | 1.25k | } |
1567 | 55.6k | } |
1568 | | |
1569 | | // copy subsequent lines without margin. |
1570 | 11.1k | while (p < pend) { |
1571 | 54.1k | for (Py_ssize_t i = 0; i < margin; i++, p++) { |
1572 | 46.5k | if (*p != ' ') { |
1573 | 2.24k | assert(*p == '\n' || *p == '\0'); |
1574 | 2.24k | break; |
1575 | 2.24k | } |
1576 | 46.5k | } |
1577 | 328k | while (p < pend) { |
1578 | 327k | int ch = *w++ = *p++; |
1579 | 327k | if (ch == '\n') { |
1580 | 8.75k | break; |
1581 | 8.75k | } |
1582 | 327k | } |
1583 | 9.92k | } |
1584 | | |
1585 | 1.25k | Py_DECREF(doc); |
1586 | 1.25k | PyObject *res = PyUnicode_FromStringAndSize(buff, w - buff); |
1587 | 1.25k | PyMem_Free(buff); |
1588 | 1.25k | return res; |
1589 | 1.25k | } |
1590 | | |
1591 | | /* Access to compiler optimizations for unit tests. |
1592 | | * |
1593 | | * _PyCompile_CodeGen takes an AST, applies code-gen and |
1594 | | * returns the unoptimized CFG as an instruction list. |
1595 | | * |
1596 | | */ |
1597 | | PyObject * |
1598 | | _PyCompile_CodeGen(PyObject *ast, PyObject *filename, PyCompilerFlags *pflags, |
1599 | | int optimize, int compile_mode) |
1600 | 0 | { |
1601 | 0 | PyObject *res = NULL; |
1602 | 0 | PyObject *metadata = NULL; |
1603 | |
|
1604 | 0 | if (!PyAST_Check(ast)) { |
1605 | 0 | PyErr_SetString(PyExc_TypeError, "expected an AST"); |
1606 | 0 | return NULL; |
1607 | 0 | } |
1608 | | |
1609 | 0 | PyArena *arena = _PyArena_New(); |
1610 | 0 | if (arena == NULL) { |
1611 | 0 | return NULL; |
1612 | 0 | } |
1613 | | |
1614 | 0 | mod_ty mod = PyAST_obj2mod(ast, arena, compile_mode); |
1615 | 0 | if (mod == NULL || !_PyAST_Validate(mod)) { |
1616 | 0 | _PyArena_Free(arena); |
1617 | 0 | return NULL; |
1618 | 0 | } |
1619 | | |
1620 | 0 | compiler *c = new_compiler(mod, filename, pflags, optimize, arena); |
1621 | 0 | if (c == NULL) { |
1622 | 0 | _PyArena_Free(arena); |
1623 | 0 | return NULL; |
1624 | 0 | } |
1625 | 0 | c->c_save_nested_seqs = true; |
1626 | |
|
1627 | 0 | metadata = PyDict_New(); |
1628 | 0 | if (metadata == NULL) { |
1629 | 0 | return NULL; |
1630 | 0 | } |
1631 | | |
1632 | 0 | if (compiler_codegen(c, mod) < 0) { |
1633 | 0 | goto finally; |
1634 | 0 | } |
1635 | | |
1636 | 0 | _PyCompile_CodeUnitMetadata *umd = &c->u->u_metadata; |
1637 | |
|
1638 | 0 | #define SET_METADATA_INT(key, value) do { \ |
1639 | 0 | PyObject *v = PyLong_FromLong((long)value); \ |
1640 | 0 | if (v == NULL) goto finally; \ |
1641 | 0 | int res = PyDict_SetItemString(metadata, key, v); \ |
1642 | 0 | Py_XDECREF(v); \ |
1643 | 0 | if (res < 0) goto finally; \ |
1644 | 0 | } while (0); |
1645 | |
|
1646 | 0 | SET_METADATA_INT("argcount", umd->u_argcount); |
1647 | 0 | SET_METADATA_INT("posonlyargcount", umd->u_posonlyargcount); |
1648 | 0 | SET_METADATA_INT("kwonlyargcount", umd->u_kwonlyargcount); |
1649 | 0 | #undef SET_METADATA_INT |
1650 | |
|
1651 | 0 | int addNone = mod->kind != Expression_kind; |
1652 | 0 | if (_PyCodegen_AddReturnAtEnd(c, addNone) < 0) { |
1653 | 0 | goto finally; |
1654 | 0 | } |
1655 | | |
1656 | 0 | if (_PyInstructionSequence_ApplyLabelMap(_PyCompile_InstrSequence(c)) < 0) { |
1657 | 0 | return NULL; |
1658 | 0 | } |
1659 | | /* Allocate a copy of the instruction sequence on the heap */ |
1660 | 0 | res = PyTuple_Pack(2, _PyCompile_InstrSequence(c), metadata); |
1661 | |
|
1662 | 0 | finally: |
1663 | 0 | Py_XDECREF(metadata); |
1664 | 0 | _PyCompile_ExitScope(c); |
1665 | 0 | compiler_free(c); |
1666 | 0 | _PyArena_Free(arena); |
1667 | 0 | return res; |
1668 | 0 | } |
1669 | | |
1670 | | int _PyCfg_JumpLabelsToTargets(cfg_builder *g); |
1671 | | |
1672 | | PyCodeObject * |
1673 | | _PyCompile_Assemble(_PyCompile_CodeUnitMetadata *umd, PyObject *filename, |
1674 | | PyObject *seq) |
1675 | 0 | { |
1676 | 0 | if (!_PyInstructionSequence_Check(seq)) { |
1677 | 0 | PyErr_SetString(PyExc_TypeError, "expected an instruction sequence"); |
1678 | 0 | return NULL; |
1679 | 0 | } |
1680 | 0 | cfg_builder *g = NULL; |
1681 | 0 | PyCodeObject *co = NULL; |
1682 | 0 | instr_sequence optimized_instrs; |
1683 | 0 | memset(&optimized_instrs, 0, sizeof(instr_sequence)); |
1684 | |
|
1685 | 0 | PyObject *const_cache = PyDict_New(); |
1686 | 0 | if (const_cache == NULL) { |
1687 | 0 | return NULL; |
1688 | 0 | } |
1689 | | |
1690 | 0 | g = _PyCfg_FromInstructionSequence((instr_sequence*)seq); |
1691 | 0 | if (g == NULL) { |
1692 | 0 | goto error; |
1693 | 0 | } |
1694 | | |
1695 | 0 | if (_PyCfg_JumpLabelsToTargets(g) < 0) { |
1696 | 0 | goto error; |
1697 | 0 | } |
1698 | | |
1699 | 0 | int code_flags = 0; |
1700 | 0 | int stackdepth, nlocalsplus; |
1701 | 0 | if (_PyCfg_OptimizedCfgToInstructionSequence(g, umd, code_flags, |
1702 | 0 | &stackdepth, &nlocalsplus, |
1703 | 0 | &optimized_instrs) < 0) { |
1704 | 0 | goto error; |
1705 | 0 | } |
1706 | | |
1707 | 0 | PyObject *consts = consts_dict_keys_inorder(umd->u_consts); |
1708 | 0 | if (consts == NULL) { |
1709 | 0 | goto error; |
1710 | 0 | } |
1711 | 0 | co = _PyAssemble_MakeCodeObject(umd, const_cache, |
1712 | 0 | consts, stackdepth, &optimized_instrs, |
1713 | 0 | nlocalsplus, code_flags, filename); |
1714 | 0 | Py_DECREF(consts); |
1715 | |
|
1716 | 0 | error: |
1717 | 0 | Py_DECREF(const_cache); |
1718 | 0 | _PyCfgBuilder_Free(g); |
1719 | 0 | PyInstructionSequence_Fini(&optimized_instrs); |
1720 | 0 | return co; |
1721 | 0 | } |
1722 | | |
1723 | | /* Retained for API compatibility. |
1724 | | * Optimization is now done in _PyCfg_OptimizeCodeUnit */ |
1725 | | |
1726 | | PyObject * |
1727 | | PyCode_Optimize(PyObject *code, PyObject* Py_UNUSED(consts), |
1728 | | PyObject *Py_UNUSED(names), PyObject *Py_UNUSED(lnotab_obj)) |
1729 | 0 | { |
1730 | 0 | return Py_NewRef(code); |
1731 | 0 | } |