Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(155241)

Delta Between Two Patch Sets: Python/compile.c

Issue 23722: During metaclass.__init__, super() of the constructed class does not work
Left Patch Set: Created 4 years, 8 months ago
Right Patch Set: Created 3 years ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« Python/bltinmodule.c ('K') | « Python/bltinmodule.c ('k') | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 /* 1 /*
2 * This file compiles an abstract syntax tree (AST) into Python bytecode. 2 * This file compiles an abstract syntax tree (AST) into Python bytecode.
3 * 3 *
4 * The primary entry point is PyAST_Compile(), which returns a 4 * The primary entry point is PyAST_Compile(), which returns a
5 * PyCodeObject. The compiler makes several passes to build the code 5 * PyCodeObject. The compiler makes several passes to build the code
6 * object: 6 * object:
7 * 1. Checks for future statements. See future.c 7 * 1. Checks for future statements. See future.c
8 * 2. Builds a symbol table. See symtable.c. 8 * 2. Builds a symbol table. See symtable.c.
9 * 3. Generate code for basic blocks. See compiler_mod() in this file. 9 * 3. Generate code for basic blocks. See compiler_mod() in this file.
10 * 4. Assemble the basic blocks into final code. See assemble() in 10 * 4. Assemble the basic blocks into final code. See assemble() in
(...skipping 11 matching lines...) Expand all
22 */ 22 */
23 23
24 #include "Python.h" 24 #include "Python.h"
25 25
26 #include "Python-ast.h" 26 #include "Python-ast.h"
27 #include "node.h" 27 #include "node.h"
28 #include "ast.h" 28 #include "ast.h"
29 #include "code.h" 29 #include "code.h"
30 #include "symtable.h" 30 #include "symtable.h"
31 #include "opcode.h" 31 #include "opcode.h"
32 #include "wordcode_helpers.h"
32 33
33 #define DEFAULT_BLOCK_SIZE 16 34 #define DEFAULT_BLOCK_SIZE 16
34 #define DEFAULT_BLOCKS 8 35 #define DEFAULT_BLOCKS 8
35 #define DEFAULT_CODE_SIZE 128 36 #define DEFAULT_CODE_SIZE 128
36 #define DEFAULT_LNOTAB_SIZE 16 37 #define DEFAULT_LNOTAB_SIZE 16
37 38
38 #define COMP_GENEXP 0 39 #define COMP_GENEXP 0
39 #define COMP_LISTCOMP 1 40 #define COMP_LISTCOMP 1
40 #define COMP_SETCOMP 2 41 #define COMP_SETCOMP 2
41 #define COMP_DICTCOMP 3 42 #define COMP_DICTCOMP 3
42 43
43 struct instr { 44 struct instr {
44 unsigned i_jabs : 1; 45 unsigned i_jabs : 1;
45 unsigned i_jrel : 1; 46 unsigned i_jrel : 1;
46 unsigned i_hasarg : 1;
47 unsigned char i_opcode; 47 unsigned char i_opcode;
48 int i_oparg; 48 int i_oparg;
49 struct basicblock_ *i_target; /* target block (if jump instruction) */ 49 struct basicblock_ *i_target; /* target block (if jump instruction) */
50 int i_lineno; 50 int i_lineno;
51 }; 51 };
52 52
53 typedef struct basicblock_ { 53 typedef struct basicblock_ {
54 /* Each basicblock in a compilation unit is linked via b_list in the 54 /* Each basicblock in a compilation unit is linked via b_list in the
55 reverse order that the block are allocated. b_list points to the next 55 reverse order that the block are allocated. b_list points to the next
56 block, not to be confused with b_next, which is next by control flow. */ 56 block, not to be confused with b_next, which is next by control flow. */
(...skipping 28 matching lines...) Expand all
85 85
86 struct fblockinfo { 86 struct fblockinfo {
87 enum fblocktype fb_type; 87 enum fblocktype fb_type;
88 basicblock *fb_block; 88 basicblock *fb_block;
89 }; 89 };
90 90
91 enum { 91 enum {
92 COMPILER_SCOPE_MODULE, 92 COMPILER_SCOPE_MODULE,
93 COMPILER_SCOPE_CLASS, 93 COMPILER_SCOPE_CLASS,
94 COMPILER_SCOPE_FUNCTION, 94 COMPILER_SCOPE_FUNCTION,
95 COMPILER_SCOPE_ASYNC_FUNCTION,
95 COMPILER_SCOPE_LAMBDA, 96 COMPILER_SCOPE_LAMBDA,
96 COMPILER_SCOPE_COMPREHENSION, 97 COMPILER_SCOPE_COMPREHENSION,
97 }; 98 };
98 99
99 /* The following items change on entry and exit of code blocks. 100 /* The following items change on entry and exit of code blocks.
100 They must be saved and restored when returning to a block. 101 They must be saved and restored when returning to a block.
101 */ 102 */
102 struct compiler_unit { 103 struct compiler_unit {
103 PySTEntryObject *u_ste; 104 PySTEntryObject *u_ste;
104 105
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
163 }; 164 };
164 165
165 static int compiler_enter_scope(struct compiler *, identifier, int, void *, int) ; 166 static int compiler_enter_scope(struct compiler *, identifier, int, void *, int) ;
166 static void compiler_free(struct compiler *); 167 static void compiler_free(struct compiler *);
167 static basicblock *compiler_new_block(struct compiler *); 168 static basicblock *compiler_new_block(struct compiler *);
168 static int compiler_next_instr(struct compiler *, basicblock *); 169 static int compiler_next_instr(struct compiler *, basicblock *);
169 static int compiler_addop(struct compiler *, int); 170 static int compiler_addop(struct compiler *, int);
170 static int compiler_addop_o(struct compiler *, int, PyObject *, PyObject *); 171 static int compiler_addop_o(struct compiler *, int, PyObject *, PyObject *);
171 static int compiler_addop_i(struct compiler *, int, Py_ssize_t); 172 static int compiler_addop_i(struct compiler *, int, Py_ssize_t);
172 static int compiler_addop_j(struct compiler *, int, basicblock *, int); 173 static int compiler_addop_j(struct compiler *, int, basicblock *, int);
173 static basicblock *compiler_use_new_block(struct compiler *);
174 static int compiler_error(struct compiler *, const char *); 174 static int compiler_error(struct compiler *, const char *);
175 static int compiler_nameop(struct compiler *, identifier, expr_context_ty); 175 static int compiler_nameop(struct compiler *, identifier, expr_context_ty);
176 176
177 static PyCodeObject *compiler_mod(struct compiler *, mod_ty); 177 static PyCodeObject *compiler_mod(struct compiler *, mod_ty);
178 static int compiler_visit_stmt(struct compiler *, stmt_ty); 178 static int compiler_visit_stmt(struct compiler *, stmt_ty);
179 static int compiler_visit_keyword(struct compiler *, keyword_ty); 179 static int compiler_visit_keyword(struct compiler *, keyword_ty);
180 static int compiler_visit_expr(struct compiler *, expr_ty); 180 static int compiler_visit_expr(struct compiler *, expr_ty);
181 static int compiler_augassign(struct compiler *, stmt_ty); 181 static int compiler_augassign(struct compiler *, stmt_ty);
182 static int compiler_annassign(struct compiler *, stmt_ty);
182 static int compiler_visit_slice(struct compiler *, slice_ty, 183 static int compiler_visit_slice(struct compiler *, slice_ty,
183 expr_context_ty); 184 expr_context_ty);
184 185
185 static int compiler_push_fblock(struct compiler *, enum fblocktype, 186 static int compiler_push_fblock(struct compiler *, enum fblocktype,
186 basicblock *); 187 basicblock *);
187 static void compiler_pop_fblock(struct compiler *, enum fblocktype, 188 static void compiler_pop_fblock(struct compiler *, enum fblocktype,
188 basicblock *); 189 basicblock *);
189 /* Returns true if there is a loop on the fblock stack. */ 190 /* Returns true if there is a loop on the fblock stack. */
190 static int compiler_in_loop(struct compiler *); 191 static int compiler_in_loop(struct compiler *);
191 192
192 static int inplace_binop(struct compiler *, operator_ty); 193 static int inplace_binop(struct compiler *, operator_ty);
193 static int expr_constant(struct compiler *, expr_ty); 194 static int expr_constant(struct compiler *, expr_ty);
194 195
195 static int compiler_with(struct compiler *, stmt_ty, int); 196 static int compiler_with(struct compiler *, stmt_ty, int);
196 static int compiler_call_helper(struct compiler *c, Py_ssize_t n, 197 static int compiler_async_with(struct compiler *, stmt_ty, int);
198 static int compiler_async_for(struct compiler *, stmt_ty);
199 static int compiler_call_helper(struct compiler *c, int n,
197 asdl_seq *args, 200 asdl_seq *args,
198 asdl_seq *keywords, 201 asdl_seq *keywords);
199 expr_ty starargs,
200 expr_ty kwargs);
201 static int compiler_try_except(struct compiler *, stmt_ty); 202 static int compiler_try_except(struct compiler *, stmt_ty);
202 static int compiler_set_qualname(struct compiler *); 203 static int compiler_set_qualname(struct compiler *);
203 204
205 static int compiler_sync_comprehension_generator(
206 struct compiler *c,
207 asdl_seq *generators, int gen_index,
208 expr_ty elt, expr_ty val, int type);
209
210 static int compiler_async_comprehension_generator(
211 struct compiler *c,
212 asdl_seq *generators, int gen_index,
213 expr_ty elt, expr_ty val, int type);
214
204 static PyCodeObject *assemble(struct compiler *, int addNone); 215 static PyCodeObject *assemble(struct compiler *, int addNone);
205 static PyObject *__doc__; 216 static PyObject *__doc__;
206 217
207 #define COMPILER_CAPSULE_NAME_COMPILER_UNIT "compile.c compiler unit" 218 #define CAPSULE_NAME "compile.c compiler unit"
208 219
209 PyObject * 220 PyObject *
210 _Py_Mangle(PyObject *privateobj, PyObject *ident) 221 _Py_Mangle(PyObject *privateobj, PyObject *ident)
211 { 222 {
212 /* Name mangling: __private becomes _classname__private. 223 /* Name mangling: __private becomes _classname__private.
213 This is independent from how the name is used. */ 224 This is independent from how the name is used. */
214 PyObject *result; 225 PyObject *result;
215 size_t nlen, plen, ipriv; 226 size_t nlen, plen, ipriv;
216 Py_UCS4 maxchar; 227 Py_UCS4 maxchar;
217 if (privateobj == NULL || !PyUnicode_Check(privateobj) || 228 if (privateobj == NULL || !PyUnicode_Check(privateobj) ||
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after
385 if (!dict) return NULL; 396 if (!dict) return NULL;
386 397
387 n = PyList_Size(list); 398 n = PyList_Size(list);
388 for (i = 0; i < n; i++) { 399 for (i = 0; i < n; i++) {
389 v = PyLong_FromSsize_t(i); 400 v = PyLong_FromSsize_t(i);
390 if (!v) { 401 if (!v) {
391 Py_DECREF(dict); 402 Py_DECREF(dict);
392 return NULL; 403 return NULL;
393 } 404 }
394 k = PyList_GET_ITEM(list, i); 405 k = PyList_GET_ITEM(list, i);
395 k = PyTuple_Pack(2, k, k->ob_type); 406 k = _PyCode_ConstantKey(k);
396 if (k == NULL || PyDict_SetItem(dict, k, v) < 0) { 407 if (k == NULL || PyDict_SetItem(dict, k, v) < 0) {
397 Py_XDECREF(k); 408 Py_XDECREF(k);
398 Py_DECREF(v); 409 Py_DECREF(v);
399 Py_DECREF(dict); 410 Py_DECREF(dict);
400 return NULL; 411 return NULL;
401 } 412 }
402 Py_DECREF(k); 413 Py_DECREF(k);
403 Py_DECREF(v); 414 Py_DECREF(v);
404 } 415 }
405 return dict; 416 return dict;
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
448 scope = (vi >> SCOPE_OFFSET) & SCOPE_MASK; 459 scope = (vi >> SCOPE_OFFSET) & SCOPE_MASK;
449 460
450 if (scope == scope_type || vi & flag) { 461 if (scope == scope_type || vi & flag) {
451 PyObject *tuple, *item = PyLong_FromSsize_t(i); 462 PyObject *tuple, *item = PyLong_FromSsize_t(i);
452 if (item == NULL) { 463 if (item == NULL) {
453 Py_DECREF(sorted_keys); 464 Py_DECREF(sorted_keys);
454 Py_DECREF(dest); 465 Py_DECREF(dest);
455 return NULL; 466 return NULL;
456 } 467 }
457 i++; 468 i++;
458 tuple = PyTuple_Pack(2, k, k->ob_type); 469 tuple = _PyCode_ConstantKey(k);
459 if (!tuple || PyDict_SetItem(dest, tuple, item) < 0) { 470 if (!tuple || PyDict_SetItem(dest, tuple, item) < 0) {
460 Py_DECREF(sorted_keys); 471 Py_DECREF(sorted_keys);
461 Py_DECREF(item); 472 Py_DECREF(item);
462 Py_DECREF(dest); 473 Py_DECREF(dest);
463 Py_XDECREF(tuple); 474 Py_XDECREF(tuple);
464 return NULL; 475 return NULL;
465 } 476 }
466 Py_DECREF(item); 477 Py_DECREF(item);
467 Py_DECREF(tuple); 478 Py_DECREF(tuple);
468 } 479 }
469 } 480 }
470 Py_DECREF(sorted_keys); 481 Py_DECREF(sorted_keys);
471 return dest; 482 return dest;
472 } 483 }
473 484
474 static void 485 static void
475 compiler_unit_check(struct compiler_unit *u) 486 compiler_unit_check(struct compiler_unit *u)
476 { 487 {
477 basicblock *block; 488 basicblock *block;
478 for (block = u->u_blocks; block != NULL; block = block->b_list) { 489 for (block = u->u_blocks; block != NULL; block = block->b_list) {
479 assert((void *)block != (void *)0xcbcbcbcb); 490 assert((uintptr_t)block != 0xcbcbcbcbU);
480 assert((void *)block != (void *)0xfbfbfbfb); 491 assert((uintptr_t)block != 0xfbfbfbfbU);
481 assert((void *)block != (void *)0xdbdbdbdb); 492 assert((uintptr_t)block != 0xdbdbdbdbU);
482 if (block->b_instr != NULL) { 493 if (block->b_instr != NULL) {
483 assert(block->b_ialloc > 0); 494 assert(block->b_ialloc > 0);
484 assert(block->b_iused > 0); 495 assert(block->b_iused > 0);
485 assert(block->b_ialloc >= block->b_iused); 496 assert(block->b_ialloc >= block->b_iused);
486 } 497 }
487 else { 498 else {
488 assert (block->b_iused == 0); 499 assert (block->b_iused == 0);
489 assert (block->b_ialloc == 0); 500 assert (block->b_ialloc == 0);
490 } 501 }
491 } 502 }
(...skipping 23 matching lines...) Expand all
515 Py_CLEAR(u->u_cellvars); 526 Py_CLEAR(u->u_cellvars);
516 Py_CLEAR(u->u_private); 527 Py_CLEAR(u->u_private);
517 PyObject_Free(u); 528 PyObject_Free(u);
518 } 529 }
519 530
520 static int 531 static int
521 compiler_enter_scope(struct compiler *c, identifier name, 532 compiler_enter_scope(struct compiler *c, identifier name,
522 int scope_type, void *key, int lineno) 533 int scope_type, void *key, int lineno)
523 { 534 {
524 struct compiler_unit *u; 535 struct compiler_unit *u;
536 basicblock *block;
525 537
526 u = (struct compiler_unit *)PyObject_Malloc(sizeof( 538 u = (struct compiler_unit *)PyObject_Malloc(sizeof(
527 struct compiler_unit)); 539 struct compiler_unit));
528 if (!u) { 540 if (!u) {
529 PyErr_NoMemory(); 541 PyErr_NoMemory();
530 return 0; 542 return 0;
531 } 543 }
532 memset(u, 0, sizeof(struct compiler_unit)); 544 memset(u, 0, sizeof(struct compiler_unit));
533 u->u_scope_type = scope_type; 545 u->u_scope_type = scope_type;
534 u->u_argcount = 0; 546 u->u_argcount = 0;
535 u->u_kwonlyargcount = 0; 547 u->u_kwonlyargcount = 0;
536 u->u_ste = PySymtable_Lookup(c->c_st, key); 548 u->u_ste = PySymtable_Lookup(c->c_st, key);
537 if (!u->u_ste) { 549 if (!u->u_ste) {
538 compiler_unit_free(u); 550 compiler_unit_free(u);
539 return 0; 551 return 0;
540 } 552 }
541 Py_INCREF(name); 553 Py_INCREF(name);
542 u->u_name = name; 554 u->u_name = name;
543 u->u_varnames = list2dict(u->u_ste->ste_varnames); 555 u->u_varnames = list2dict(u->u_ste->ste_varnames);
544 u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0); 556 u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0);
545 if (!u->u_varnames || !u->u_cellvars) { 557 if (!u->u_varnames || !u->u_cellvars) {
546 compiler_unit_free(u); 558 compiler_unit_free(u);
547 return 0; 559 return 0;
548 } 560 }
549 if (u->u_ste->ste_needs_class_closure) { 561 if (u->u_ste->ste_needs_class_closure) {
550 /* Cook up a implicit __class__ cell. */ 562 /* Cook up an implicit __class__ cell. */
551 _Py_IDENTIFIER(__class__); 563 _Py_IDENTIFIER(__class__);
552 PyObject *tuple, *name, *zero; 564 PyObject *tuple, *name, *zero;
553 int res; 565 int res;
554 assert(u->u_scope_type == COMPILER_SCOPE_CLASS); 566 assert(u->u_scope_type == COMPILER_SCOPE_CLASS);
555 assert(PyDict_Size(u->u_cellvars) == 0); 567 assert(PyDict_Size(u->u_cellvars) == 0);
556 name = _PyUnicode_FromId(&PyId___class__); 568 name = _PyUnicode_FromId(&PyId___class__);
557 if (!name) { 569 if (!name) {
558 compiler_unit_free(u); 570 compiler_unit_free(u);
559 return 0; 571 return 0;
560 } 572 }
561 tuple = PyTuple_Pack(2, name, Py_TYPE(name)); 573 tuple = _PyCode_ConstantKey(name);
562 if (!tuple) { 574 if (!tuple) {
563 compiler_unit_free(u); 575 compiler_unit_free(u);
564 return 0; 576 return 0;
565 } 577 }
566 zero = PyLong_FromLong(0); 578 zero = PyLong_FromLong(0);
567 if (!zero) { 579 if (!zero) {
568 Py_DECREF(tuple); 580 Py_DECREF(tuple);
569 compiler_unit_free(u); 581 compiler_unit_free(u);
570 return 0; 582 return 0;
571 } 583 }
(...skipping 27 matching lines...) Expand all
599 u->u_names = PyDict_New(); 611 u->u_names = PyDict_New();
600 if (!u->u_names) { 612 if (!u->u_names) {
601 compiler_unit_free(u); 613 compiler_unit_free(u);
602 return 0; 614 return 0;
603 } 615 }
604 616
605 u->u_private = NULL; 617 u->u_private = NULL;
606 618
607 /* Push the old compiler_unit on the stack. */ 619 /* Push the old compiler_unit on the stack. */
608 if (c->u) { 620 if (c->u) {
609 PyObject *capsule = PyCapsule_New(c->u, COMPILER_CAPSULE_NAME_COMPILER_U NIT, NULL); 621 PyObject *capsule = PyCapsule_New(c->u, CAPSULE_NAME, NULL);
610 if (!capsule || PyList_Append(c->c_stack, capsule) < 0) { 622 if (!capsule || PyList_Append(c->c_stack, capsule) < 0) {
611 Py_XDECREF(capsule); 623 Py_XDECREF(capsule);
612 compiler_unit_free(u); 624 compiler_unit_free(u);
613 return 0; 625 return 0;
614 } 626 }
615 Py_DECREF(capsule); 627 Py_DECREF(capsule);
616 u->u_private = c->u->u_private; 628 u->u_private = c->u->u_private;
617 Py_XINCREF(u->u_private); 629 Py_XINCREF(u->u_private);
618 } 630 }
619 c->u = u; 631 c->u = u;
620 632
621 c->c_nestlevel++; 633 c->c_nestlevel++;
622 if (compiler_use_new_block(c) == NULL) 634
623 return 0; 635 block = compiler_new_block(c);
636 if (block == NULL)
637 return 0;
638 c->u->u_curblock = block;
624 639
625 if (u->u_scope_type != COMPILER_SCOPE_MODULE) { 640 if (u->u_scope_type != COMPILER_SCOPE_MODULE) {
626 if (!compiler_set_qualname(c)) 641 if (!compiler_set_qualname(c))
627 return 0; 642 return 0;
628 } 643 }
629 644
630 return 1; 645 return 1;
631 } 646 }
632 647
633 static void 648 static void
634 compiler_exit_scope(struct compiler *c) 649 compiler_exit_scope(struct compiler *c)
635 { 650 {
636 Py_ssize_t n; 651 Py_ssize_t n;
637 PyObject *capsule; 652 PyObject *capsule;
638 653
639 c->c_nestlevel--; 654 c->c_nestlevel--;
640 compiler_unit_free(c->u); 655 compiler_unit_free(c->u);
641 /* Restore c->u to the parent unit. */ 656 /* Restore c->u to the parent unit. */
642 n = PyList_GET_SIZE(c->c_stack) - 1; 657 n = PyList_GET_SIZE(c->c_stack) - 1;
643 if (n >= 0) { 658 if (n >= 0) {
644 capsule = PyList_GET_ITEM(c->c_stack, n); 659 capsule = PyList_GET_ITEM(c->c_stack, n);
645 c->u = (struct compiler_unit *)PyCapsule_GetPointer(capsule, COMPILER_CA PSULE_NAME_COMPILER_UNIT); 660 c->u = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAM E);
646 assert(c->u); 661 assert(c->u);
647 /* we are deleting from a list so this really shouldn't fail */ 662 /* we are deleting from a list so this really shouldn't fail */
648 if (PySequence_DelItem(c->c_stack, n) < 0) 663 if (PySequence_DelItem(c->c_stack, n) < 0)
649 Py_FatalError("compiler_exit_scope()"); 664 Py_FatalError("compiler_exit_scope()");
650 compiler_unit_check(c->u); 665 compiler_unit_check(c->u);
651 } 666 }
652 else 667 else
653 c->u = NULL; 668 c->u = NULL;
654 669
655 } 670 }
656 671
657 static int 672 static int
658 compiler_set_qualname(struct compiler *c) 673 compiler_set_qualname(struct compiler *c)
659 { 674 {
660 _Py_static_string(dot, "."); 675 _Py_static_string(dot, ".");
661 _Py_static_string(dot_locals, ".<locals>"); 676 _Py_static_string(dot_locals, ".<locals>");
662 Py_ssize_t stack_size; 677 Py_ssize_t stack_size;
663 struct compiler_unit *u = c->u; 678 struct compiler_unit *u = c->u;
664 PyObject *name, *base, *dot_str, *dot_locals_str; 679 PyObject *name, *base, *dot_str, *dot_locals_str;
665 680
666 base = NULL; 681 base = NULL;
667 stack_size = PyList_GET_SIZE(c->c_stack); 682 stack_size = PyList_GET_SIZE(c->c_stack);
668 assert(stack_size >= 1); 683 assert(stack_size >= 1);
669 if (stack_size > 1) { 684 if (stack_size > 1) {
670 int scope, force_global = 0; 685 int scope, force_global = 0;
671 struct compiler_unit *parent; 686 struct compiler_unit *parent;
672 PyObject *mangled, *capsule; 687 PyObject *mangled, *capsule;
673 688
674 capsule = PyList_GET_ITEM(c->c_stack, stack_size - 1); 689 capsule = PyList_GET_ITEM(c->c_stack, stack_size - 1);
675 parent = (struct compiler_unit *)PyCapsule_GetPointer(capsule, COMPILER_ CAPSULE_NAME_COMPILER_UNIT); 690 parent = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_N AME);
676 assert(parent); 691 assert(parent);
677 692
678 if (u->u_scope_type == COMPILER_SCOPE_FUNCTION || u->u_scope_type == COM PILER_SCOPE_CLASS) { 693 if (u->u_scope_type == COMPILER_SCOPE_FUNCTION
694 || u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION
695 || u->u_scope_type == COMPILER_SCOPE_CLASS) {
679 assert(u->u_name); 696 assert(u->u_name);
680 mangled = _Py_Mangle(parent->u_private, u->u_name); 697 mangled = _Py_Mangle(parent->u_private, u->u_name);
681 if (!mangled) 698 if (!mangled)
682 return 0; 699 return 0;
683 scope = PyST_GetScope(parent->u_ste, mangled); 700 scope = PyST_GetScope(parent->u_ste, mangled);
684 Py_DECREF(mangled); 701 Py_DECREF(mangled);
685 assert(scope != GLOBAL_IMPLICIT); 702 assert(scope != GLOBAL_IMPLICIT);
686 if (scope == GLOBAL_EXPLICIT) 703 if (scope == GLOBAL_EXPLICIT)
687 force_global = 1; 704 force_global = 1;
688 } 705 }
689 706
690 if (!force_global) { 707 if (!force_global) {
691 if (parent->u_scope_type == COMPILER_SCOPE_FUNCTION 708 if (parent->u_scope_type == COMPILER_SCOPE_FUNCTION
709 || parent->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION
692 || parent->u_scope_type == COMPILER_SCOPE_LAMBDA) { 710 || parent->u_scope_type == COMPILER_SCOPE_LAMBDA) {
693 dot_locals_str = _PyUnicode_FromId(&dot_locals); 711 dot_locals_str = _PyUnicode_FromId(&dot_locals);
694 if (dot_locals_str == NULL) 712 if (dot_locals_str == NULL)
695 return 0; 713 return 0;
696 base = PyUnicode_Concat(parent->u_qualname, dot_locals_str); 714 base = PyUnicode_Concat(parent->u_qualname, dot_locals_str);
697 if (base == NULL) 715 if (base == NULL)
698 return 0; 716 return 0;
699 } 717 }
700 else { 718 else {
701 Py_INCREF(parent->u_qualname); 719 Py_INCREF(parent->u_qualname);
(...skipping 18 matching lines...) Expand all
720 } 738 }
721 else { 739 else {
722 Py_INCREF(u->u_name); 740 Py_INCREF(u->u_name);
723 name = u->u_name; 741 name = u->u_name;
724 } 742 }
725 u->u_qualname = name; 743 u->u_qualname = name;
726 744
727 return 1; 745 return 1;
728 } 746 }
729 747
748
730 /* Allocate a new block and return a pointer to it. 749 /* Allocate a new block and return a pointer to it.
731 Returns NULL on error. 750 Returns NULL on error.
732 */ 751 */
733 752
734 static basicblock * 753 static basicblock *
735 compiler_new_block(struct compiler *c) 754 compiler_new_block(struct compiler *c)
736 { 755 {
737 basicblock *b; 756 basicblock *b;
738 struct compiler_unit *u; 757 struct compiler_unit *u;
739 758
740 u = c->u; 759 u = c->u;
741 b = (basicblock *)PyObject_Malloc(sizeof(basicblock)); 760 b = (basicblock *)PyObject_Malloc(sizeof(basicblock));
742 if (b == NULL) { 761 if (b == NULL) {
743 PyErr_NoMemory(); 762 PyErr_NoMemory();
744 return NULL; 763 return NULL;
745 } 764 }
746 memset((void *)b, 0, sizeof(basicblock)); 765 memset((void *)b, 0, sizeof(basicblock));
747 /* Extend the singly linked list of blocks with new block. */ 766 /* Extend the singly linked list of blocks with new block. */
748 b->b_list = u->u_blocks; 767 b->b_list = u->u_blocks;
749 u->u_blocks = b; 768 u->u_blocks = b;
750 return b; 769 return b;
751 }
752
753 static basicblock *
754 compiler_use_new_block(struct compiler *c)
755 {
756 basicblock *block = compiler_new_block(c);
757 if (block == NULL)
758 return NULL;
759 c->u->u_curblock = block;
760 return block;
761 } 770 }
762 771
763 static basicblock * 772 static basicblock *
764 compiler_next_block(struct compiler *c) 773 compiler_next_block(struct compiler *c)
765 { 774 {
766 basicblock *block = compiler_new_block(c); 775 basicblock *block = compiler_new_block(c);
767 if (block == NULL) 776 if (block == NULL)
768 return NULL; 777 return NULL;
769 c->u->u_curblock->b_next = block; 778 c->u->u_curblock->b_next = block;
770 c->u->u_curblock = block; 779 c->u->u_curblock = block;
(...skipping 28 matching lines...) Expand all
799 b->b_ialloc = DEFAULT_BLOCK_SIZE; 808 b->b_ialloc = DEFAULT_BLOCK_SIZE;
800 memset((char *)b->b_instr, 0, 809 memset((char *)b->b_instr, 0,
801 sizeof(struct instr) * DEFAULT_BLOCK_SIZE); 810 sizeof(struct instr) * DEFAULT_BLOCK_SIZE);
802 } 811 }
803 else if (b->b_iused == b->b_ialloc) { 812 else if (b->b_iused == b->b_ialloc) {
804 struct instr *tmp; 813 struct instr *tmp;
805 size_t oldsize, newsize; 814 size_t oldsize, newsize;
806 oldsize = b->b_ialloc * sizeof(struct instr); 815 oldsize = b->b_ialloc * sizeof(struct instr);
807 newsize = oldsize << 1; 816 newsize = oldsize << 1;
808 817
809 if (oldsize > (PY_SIZE_MAX >> 1)) { 818 if (oldsize > (SIZE_MAX >> 1)) {
810 PyErr_NoMemory(); 819 PyErr_NoMemory();
811 return -1; 820 return -1;
812 } 821 }
813 822
814 if (newsize == 0) { 823 if (newsize == 0) {
815 PyErr_NoMemory(); 824 PyErr_NoMemory();
816 return -1; 825 return -1;
817 } 826 }
818 b->b_ialloc <<= 1; 827 b->b_ialloc <<= 1;
819 tmp = (struct instr *)PyObject_Realloc( 828 tmp = (struct instr *)PyObject_Realloc(
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
892 return -1; 901 return -1;
893 902
894 case INPLACE_ADD: 903 case INPLACE_ADD:
895 case INPLACE_SUBTRACT: 904 case INPLACE_SUBTRACT:
896 case INPLACE_MULTIPLY: 905 case INPLACE_MULTIPLY:
897 case INPLACE_MATRIX_MULTIPLY: 906 case INPLACE_MATRIX_MULTIPLY:
898 case INPLACE_MODULO: 907 case INPLACE_MODULO:
899 return -1; 908 return -1;
900 case STORE_SUBSCR: 909 case STORE_SUBSCR:
901 return -3; 910 return -3;
902 case STORE_MAP:
903 return -2;
904 case DELETE_SUBSCR: 911 case DELETE_SUBSCR:
905 return -2; 912 return -2;
906 913
907 case BINARY_LSHIFT: 914 case BINARY_LSHIFT:
908 case BINARY_RSHIFT: 915 case BINARY_RSHIFT:
909 case BINARY_AND: 916 case BINARY_AND:
910 case BINARY_XOR: 917 case BINARY_XOR:
911 case BINARY_OR: 918 case BINARY_OR:
912 return -1; 919 return -1;
913 case INPLACE_POWER: 920 case INPLACE_POWER:
914 return -1; 921 return -1;
915 case GET_ITER: 922 case GET_ITER:
916 return 0; 923 return 0;
917 924
918 case PRINT_EXPR: 925 case PRINT_EXPR:
919 return -1; 926 return -1;
920 case LOAD_BUILD_CLASS: 927 case LOAD_BUILD_CLASS:
921 return 1; 928 return 1;
922 case INPLACE_LSHIFT: 929 case INPLACE_LSHIFT:
923 case INPLACE_RSHIFT: 930 case INPLACE_RSHIFT:
924 case INPLACE_AND: 931 case INPLACE_AND:
925 case INPLACE_XOR: 932 case INPLACE_XOR:
926 case INPLACE_OR: 933 case INPLACE_OR:
927 return -1; 934 return -1;
928 case BREAK_LOOP: 935 case BREAK_LOOP:
929 return 0; 936 return 0;
930 case SETUP_WITH: 937 case SETUP_WITH:
931 return 7; 938 return 7;
932 case WITH_CLEANUP: 939 case WITH_CLEANUP_START:
940 return 1;
941 case WITH_CLEANUP_FINISH:
933 return -1; /* XXX Sometimes more */ 942 return -1; /* XXX Sometimes more */
934 case RETURN_VALUE: 943 case RETURN_VALUE:
935 return -1; 944 return -1;
936 case IMPORT_STAR: 945 case IMPORT_STAR:
937 return -1; 946 return -1;
947 case SETUP_ANNOTATIONS:
948 return 0;
938 case YIELD_VALUE: 949 case YIELD_VALUE:
939 return 0; 950 return 0;
940 case YIELD_FROM: 951 case YIELD_FROM:
941 return -1; 952 return -1;
942 case POP_BLOCK: 953 case POP_BLOCK:
943 return 0; 954 return 0;
944 case POP_EXCEPT: 955 case POP_EXCEPT:
945 return 0; /* -3 except if bad bytecode */ 956 return 0; /* -3 except if bad bytecode */
946 case END_FINALLY: 957 case END_FINALLY:
947 return -1; /* or -2 or -3 if exception occurred */ 958 return -1; /* or -2 or -3 if exception occurred */
(...skipping 17 matching lines...) Expand all
965 return -1; 976 return -1;
966 case DELETE_GLOBAL: 977 case DELETE_GLOBAL:
967 return 0; 978 return 0;
968 case LOAD_CONST: 979 case LOAD_CONST:
969 return 1; 980 return 1;
970 case LOAD_NAME: 981 case LOAD_NAME:
971 return 1; 982 return 1;
972 case BUILD_TUPLE: 983 case BUILD_TUPLE:
973 case BUILD_LIST: 984 case BUILD_LIST:
974 case BUILD_SET: 985 case BUILD_SET:
986 case BUILD_STRING:
975 return 1-oparg; 987 return 1-oparg;
988 case BUILD_LIST_UNPACK:
989 case BUILD_TUPLE_UNPACK:
990 case BUILD_TUPLE_UNPACK_WITH_CALL:
991 case BUILD_SET_UNPACK:
992 case BUILD_MAP_UNPACK:
993 case BUILD_MAP_UNPACK_WITH_CALL:
994 return 1 - oparg;
976 case BUILD_MAP: 995 case BUILD_MAP:
977 return 1; 996 return 1 - 2*oparg;
997 case BUILD_CONST_KEY_MAP:
998 return -oparg;
978 case LOAD_ATTR: 999 case LOAD_ATTR:
979 return 0; 1000 return 0;
980 case COMPARE_OP: 1001 case COMPARE_OP:
981 return -1; 1002 return -1;
982 case IMPORT_NAME: 1003 case IMPORT_NAME:
983 return -1; 1004 return -1;
984 case IMPORT_FROM: 1005 case IMPORT_FROM:
985 return 1; 1006 return 1;
986 1007
987 case JUMP_FORWARD: 1008 case JUMP_FORWARD:
(...skipping 17 matching lines...) Expand all
1005 case SETUP_FINALLY: 1026 case SETUP_FINALLY:
1006 return 6; /* can push 3 values for the new exception 1027 return 6; /* can push 3 values for the new exception
1007 + 3 others for the previous exception state */ 1028 + 3 others for the previous exception state */
1008 1029
1009 case LOAD_FAST: 1030 case LOAD_FAST:
1010 return 1; 1031 return 1;
1011 case STORE_FAST: 1032 case STORE_FAST:
1012 return -1; 1033 return -1;
1013 case DELETE_FAST: 1034 case DELETE_FAST:
1014 return 0; 1035 return 0;
1036 case STORE_ANNOTATION:
1037 return -1;
1015 1038
1016 case RAISE_VARARGS: 1039 case RAISE_VARARGS:
1017 return -oparg; 1040 return -oparg;
1018 #define NARGS(o) (((o) % 256) + 2*(((o) / 256) % 256))
1019 case CALL_FUNCTION: 1041 case CALL_FUNCTION:
1020 return -NARGS(oparg); 1042 return -oparg;
1021 case CALL_FUNCTION_VAR:
1022 case CALL_FUNCTION_KW: 1043 case CALL_FUNCTION_KW:
1023 return -NARGS(oparg)-1; 1044 return -oparg-1;
1024 case CALL_FUNCTION_VAR_KW: 1045 case CALL_FUNCTION_EX:
1025 return -NARGS(oparg)-2; 1046 return - ((oparg & 0x01) != 0) - ((oparg & 0x02) != 0);
1026 case MAKE_FUNCTION: 1047 case MAKE_FUNCTION:
1027 return -1 -NARGS(oparg) - ((oparg >> 16) & 0xffff); 1048 return -1 - ((oparg & 0x01) != 0) - ((oparg & 0x02) != 0) -
1028 case MAKE_CLOSURE: 1049 ((oparg & 0x04) != 0) - ((oparg & 0x08) != 0);
1029 return -2 - NARGS(oparg) - ((oparg >> 16) & 0xffff);
1030 #undef NARGS
1031 case BUILD_SLICE: 1050 case BUILD_SLICE:
1032 if (oparg == 3) 1051 if (oparg == 3)
1033 return -2; 1052 return -2;
1034 else 1053 else
1035 return -1; 1054 return -1;
1036 1055
1037 case LOAD_CLOSURE: 1056 case LOAD_CLOSURE:
1038 return 1; 1057 return 1;
1039 case LOAD_DEREF: 1058 case LOAD_DEREF:
1040 case LOAD_CLASSDEREF: 1059 case LOAD_CLASSDEREF:
1041 return 1; 1060 return 1;
1042 case STORE_DEREF: 1061 case STORE_DEREF:
1043 return -1; 1062 return -1;
1044 case DELETE_DEREF: 1063 case DELETE_DEREF:
1045 return 0; 1064 return 0;
1065 case GET_AWAITABLE:
1066 return 0;
1067 case SETUP_ASYNC_WITH:
1068 return 6;
1069 case BEFORE_ASYNC_WITH:
1070 return 1;
1071 case GET_AITER:
1072 return 0;
1073 case GET_ANEXT:
1074 return 1;
1075 case GET_YIELD_FROM_ITER:
1076 return 0;
1077 case FORMAT_VALUE:
1078 /* If there's a fmt_spec on the stack, we go from 2->1,
1079 else 1->1. */
1080 return (oparg & FVS_MASK) == FVS_HAVE_SPEC ? -1 : 0;
1046 default: 1081 default:
1047 return PY_INVALID_STACK_EFFECT; 1082 return PY_INVALID_STACK_EFFECT;
1048 } 1083 }
1049 return PY_INVALID_STACK_EFFECT; /* not reachable */ 1084 return PY_INVALID_STACK_EFFECT; /* not reachable */
1050 } 1085 }
1051 1086
1052 /* Add an opcode with no argument. 1087 /* Add an opcode with no argument.
1053 Returns 0 on failure, 1 on success. 1088 Returns 0 on failure, 1 on success.
1054 */ 1089 */
1055 1090
1056 static int 1091 static int
1057 compiler_addop(struct compiler *c, int opcode) 1092 compiler_addop(struct compiler *c, int opcode)
1058 { 1093 {
1059 basicblock *b; 1094 basicblock *b;
1060 struct instr *i; 1095 struct instr *i;
1061 int off; 1096 int off;
1097 assert(!HAS_ARG(opcode));
1062 off = compiler_next_instr(c, c->u->u_curblock); 1098 off = compiler_next_instr(c, c->u->u_curblock);
1063 if (off < 0) 1099 if (off < 0)
1064 return 0; 1100 return 0;
1065 b = c->u->u_curblock; 1101 b = c->u->u_curblock;
1066 i = &b->b_instr[off]; 1102 i = &b->b_instr[off];
1067 i->i_opcode = opcode; 1103 i->i_opcode = opcode;
1068 i->i_hasarg = 0; 1104 i->i_oparg = 0;
1069 if (opcode == RETURN_VALUE) 1105 if (opcode == RETURN_VALUE)
1070 b->b_return = 1; 1106 b->b_return = 1;
1071 compiler_set_lineno(c, off); 1107 compiler_set_lineno(c, off);
1072 return 1; 1108 return 1;
1073 } 1109 }
1074 1110
1075 static Py_ssize_t 1111 static Py_ssize_t
1076 compiler_add_o(struct compiler *c, PyObject *dict, PyObject *o) 1112 compiler_add_o(struct compiler *c, PyObject *dict, PyObject *o)
1077 { 1113 {
1078 PyObject *t, *v; 1114 PyObject *t, *v;
1079 Py_ssize_t arg; 1115 Py_ssize_t arg;
1080 double d; 1116
1081 1117 t = _PyCode_ConstantKey(o);
1082 /* necessary to make sure types aren't coerced (e.g., float and complex) */
1083 /* _and_ to distinguish 0.0 from -0.0 e.g. on IEEE platforms */
1084 if (PyFloat_Check(o)) {
1085 d = PyFloat_AS_DOUBLE(o);
1086 /* all we need is to make the tuple different in either the 0.0
1087 * or -0.0 case from all others, just to avoid the "coercion".
1088 */
1089 if (d == 0.0 && copysign(1.0, d) < 0.0)
1090 t = PyTuple_Pack(3, o, o->ob_type, Py_None);
1091 else
1092 t = PyTuple_Pack(2, o, o->ob_type);
1093 }
1094 else if (PyComplex_Check(o)) {
1095 Py_complex z;
1096 int real_negzero, imag_negzero;
1097 /* For the complex case we must make complex(x, 0.)
1098 different from complex(x, -0.) and complex(0., y)
1099 different from complex(-0., y), for any x and y.
1100 All four complex zeros must be distinguished.*/
1101 z = PyComplex_AsCComplex(o);
1102 real_negzero = z.real == 0.0 && copysign(1.0, z.real) < 0.0;
1103 imag_negzero = z.imag == 0.0 && copysign(1.0, z.imag) < 0.0;
1104 if (real_negzero && imag_negzero) {
1105 t = PyTuple_Pack(5, o, o->ob_type,
1106 Py_None, Py_None, Py_None);
1107 }
1108 else if (imag_negzero) {
1109 t = PyTuple_Pack(4, o, o->ob_type, Py_None, Py_None);
1110 }
1111 else if (real_negzero) {
1112 t = PyTuple_Pack(3, o, o->ob_type, Py_None);
1113 }
1114 else {
1115 t = PyTuple_Pack(2, o, o->ob_type);
1116 }
1117 }
1118 else {
1119 t = PyTuple_Pack(2, o, o->ob_type);
1120 }
1121 if (t == NULL) 1118 if (t == NULL)
1122 return -1; 1119 return -1;
1123 1120
1124 v = PyDict_GetItem(dict, t); 1121 v = PyDict_GetItem(dict, t);
1125 if (!v) { 1122 if (!v) {
1126 if (PyErr_Occurred()) 1123 if (PyErr_Occurred()) {
1124 Py_DECREF(t);
1127 return -1; 1125 return -1;
1126 }
1128 arg = PyDict_Size(dict); 1127 arg = PyDict_Size(dict);
1129 v = PyLong_FromSsize_t(arg); 1128 v = PyLong_FromSsize_t(arg);
1130 if (!v) { 1129 if (!v) {
1131 Py_DECREF(t); 1130 Py_DECREF(t);
1132 return -1; 1131 return -1;
1133 } 1132 }
1134 if (PyDict_SetItem(dict, t, v) < 0) { 1133 if (PyDict_SetItem(dict, t, v) < 0) {
1135 Py_DECREF(t); 1134 Py_DECREF(t);
1136 Py_DECREF(v); 1135 Py_DECREF(v);
1137 return -1; 1136 return -1;
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
1172 /* Add an opcode with an integer argument. 1171 /* Add an opcode with an integer argument.
1173 Returns 0 on failure, 1 on success. 1172 Returns 0 on failure, 1 on success.
1174 */ 1173 */
1175 1174
1176 static int 1175 static int
1177 compiler_addop_i(struct compiler *c, int opcode, Py_ssize_t oparg) 1176 compiler_addop_i(struct compiler *c, int opcode, Py_ssize_t oparg)
1178 { 1177 {
1179 struct instr *i; 1178 struct instr *i;
1180 int off; 1179 int off;
1181 1180
1182 /* Integer arguments are limit to 16-bit. There is an extension for 32-bit 1181 /* oparg value is unsigned, but a signed C int is usually used to store
1183 integer arguments. */ 1182 it in the C code (like Python/ceval.c).
1184 assert((-2147483647-1) <= oparg); 1183
1185 assert(oparg <= 2147483647); 1184 Limit to 32-bit signed C int (rather than INT_MAX) for portability.
1185
1186 The argument of a concrete bytecode instruction is limited to 8-bit.
1187 EXTENDED_ARG is used for 16, 24, and 32-bit arguments. */
1188 assert(HAS_ARG(opcode));
1189 assert(0 <= oparg && oparg <= 2147483647);
1186 1190
1187 off = compiler_next_instr(c, c->u->u_curblock); 1191 off = compiler_next_instr(c, c->u->u_curblock);
1188 if (off < 0) 1192 if (off < 0)
1189 return 0; 1193 return 0;
1190 i = &c->u->u_curblock->b_instr[off]; 1194 i = &c->u->u_curblock->b_instr[off];
1191 i->i_opcode = opcode; 1195 i->i_opcode = opcode;
1192 i->i_oparg = Py_SAFE_DOWNCAST(oparg, Py_ssize_t, int); 1196 i->i_oparg = Py_SAFE_DOWNCAST(oparg, Py_ssize_t, int);
1193 i->i_hasarg = 1;
1194 compiler_set_lineno(c, off); 1197 compiler_set_lineno(c, off);
1195 return 1; 1198 return 1;
1196 } 1199 }
1197 1200
1198 static int 1201 static int
1199 compiler_addop_j(struct compiler *c, int opcode, basicblock *b, int absolute) 1202 compiler_addop_j(struct compiler *c, int opcode, basicblock *b, int absolute)
1200 { 1203 {
1201 struct instr *i; 1204 struct instr *i;
1202 int off; 1205 int off;
1203 1206
1207 assert(HAS_ARG(opcode));
1204 assert(b != NULL); 1208 assert(b != NULL);
1205 off = compiler_next_instr(c, c->u->u_curblock); 1209 off = compiler_next_instr(c, c->u->u_curblock);
1206 if (off < 0) 1210 if (off < 0)
1207 return 0; 1211 return 0;
1208 i = &c->u->u_curblock->b_instr[off]; 1212 i = &c->u->u_curblock->b_instr[off];
1209 i->i_opcode = opcode; 1213 i->i_opcode = opcode;
1210 i->i_target = b; 1214 i->i_target = b;
1211 i->i_hasarg = 1;
1212 if (absolute) 1215 if (absolute)
1213 i->i_jabs = 1; 1216 i->i_jabs = 1;
1214 else 1217 else
1215 i->i_jrel = 1; 1218 i->i_jrel = 1;
1216 compiler_set_lineno(c, off); 1219 compiler_set_lineno(c, off);
1217 return 1; 1220 return 1;
1218 } 1221 }
1219 1222
1220 /* The distinction between NEW_BLOCK and NEXT_BLOCK is subtle. (I'd 1223 /* NEXT_BLOCK() creates an implicit jump from the current block
1221 like to find better names.) NEW_BLOCK() creates a new block and sets 1224 to the new block.
1222 it as the current block. NEXT_BLOCK() also creates an implicit jump 1225
1223 from the current block to the new block. 1226 The returns inside this macro make it impossible to decref objects
1227 created in the local function. Local objects should use the arena.
1224 */ 1228 */
1225
1226 /* The returns inside these macros make it impossible to decref objects
1227 created in the local function. Local objects should use the arena.
1228 */
1229
1230
1231 #define NEW_BLOCK(C) { \
1232 if (compiler_use_new_block((C)) == NULL) \
1233 return 0; \
1234 }
1235
1236 #define NEXT_BLOCK(C) { \ 1229 #define NEXT_BLOCK(C) { \
1237 if (compiler_next_block((C)) == NULL) \ 1230 if (compiler_next_block((C)) == NULL) \
1238 return 0; \ 1231 return 0; \
1239 } 1232 }
1240 1233
1241 #define ADDOP(C, OP) { \ 1234 #define ADDOP(C, OP) { \
1242 if (!compiler_addop((C), (OP))) \ 1235 if (!compiler_addop((C), (OP))) \
1243 return 0; \ 1236 return 0; \
1244 } 1237 }
1245 1238
1246 #define ADDOP_IN_SCOPE(C, OP) { \ 1239 #define ADDOP_IN_SCOPE(C, OP) { \
1247 if (!compiler_addop((C), (OP))) { \ 1240 if (!compiler_addop((C), (OP))) { \
1248 compiler_exit_scope(c); \ 1241 compiler_exit_scope(c); \
1249 return 0; \ 1242 return 0; \
1250 } \ 1243 } \
1251 } 1244 }
1252 1245
1253 #define ADDOP_O(C, OP, O, TYPE) { \ 1246 #define ADDOP_O(C, OP, O, TYPE) { \
1254 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \ 1247 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1255 return 0; \ 1248 return 0; \
1249 }
1250
1251 /* Same as ADDOP_O, but steals a reference. */
1252 #define ADDOP_N(C, OP, O, TYPE) { \
1253 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) { \
1254 Py_DECREF((O)); \
1255 return 0; \
1256 } \
1257 Py_DECREF((O)); \
1256 } 1258 }
1257 1259
1258 #define ADDOP_NAME(C, OP, O, TYPE) { \ 1260 #define ADDOP_NAME(C, OP, O, TYPE) { \
1259 if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \ 1261 if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1260 return 0; \ 1262 return 0; \
1261 } 1263 }
1262 1264
1263 #define ADDOP_I(C, OP, O) { \ 1265 #define ADDOP_I(C, OP, O) { \
1264 if (!compiler_addop_i((C), (OP), (O))) \ 1266 if (!compiler_addop_i((C), (OP), (O))) \
1265 return 0; \ 1267 return 0; \
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
1316 return 0; \ 1318 return 0; \
1317 } \ 1319 } \
1318 } \ 1320 } \
1319 } 1321 }
1320 1322
1321 static int 1323 static int
1322 compiler_isdocstring(stmt_ty s) 1324 compiler_isdocstring(stmt_ty s)
1323 { 1325 {
1324 if (s->kind != Expr_kind) 1326 if (s->kind != Expr_kind)
1325 return 0; 1327 return 0;
1326 return s->v.Expr.value->kind == Str_kind; 1328 if (s->v.Expr.value->kind == Str_kind)
1327 } 1329 return 1;
1328 1330 if (s->v.Expr.value->kind == Constant_kind)
1329 /* Compile a sequence of statements, checking for a docstring. */ 1331 return PyUnicode_CheckExact(s->v.Expr.value->v.Constant.value);
1332 return 0;
1333 }
1334
1335 static int
1336 is_const(expr_ty e)
1337 {
1338 switch (e->kind) {
1339 case Constant_kind:
1340 case Num_kind:
1341 case Str_kind:
1342 case Bytes_kind:
1343 case Ellipsis_kind:
1344 case NameConstant_kind:
1345 return 1;
1346 default:
1347 return 0;
1348 }
1349 }
1350
1351 static PyObject *
1352 get_const_value(expr_ty e)
1353 {
1354 switch (e->kind) {
1355 case Constant_kind:
1356 return e->v.Constant.value;
1357 case Num_kind:
1358 return e->v.Num.n;
1359 case Str_kind:
1360 return e->v.Str.s;
1361 case Bytes_kind:
1362 return e->v.Bytes.s;
1363 case Ellipsis_kind:
1364 return Py_Ellipsis;
1365 case NameConstant_kind:
1366 return e->v.NameConstant.value;
1367 default:
1368 assert(!is_const(e));
1369 return NULL;
1370 }
1371 }
1372
1373 /* Search if variable annotations are present statically in a block. */
1374
1375 static int
1376 find_ann(asdl_seq *stmts)
1377 {
1378 int i, j, res = 0;
1379 stmt_ty st;
1380
1381 for (i = 0; i < asdl_seq_LEN(stmts); i++) {
1382 st = (stmt_ty)asdl_seq_GET(stmts, i);
1383 switch (st->kind) {
1384 case AnnAssign_kind:
1385 return 1;
1386 case For_kind:
1387 res = find_ann(st->v.For.body) ||
1388 find_ann(st->v.For.orelse);
1389 break;
1390 case AsyncFor_kind:
1391 res = find_ann(st->v.AsyncFor.body) ||
1392 find_ann(st->v.AsyncFor.orelse);
1393 break;
1394 case While_kind:
1395 res = find_ann(st->v.While.body) ||
1396 find_ann(st->v.While.orelse);
1397 break;
1398 case If_kind:
1399 res = find_ann(st->v.If.body) ||
1400 find_ann(st->v.If.orelse);
1401 break;
1402 case With_kind:
1403 res = find_ann(st->v.With.body);
1404 break;
1405 case AsyncWith_kind:
1406 res = find_ann(st->v.AsyncWith.body);
1407 break;
1408 case Try_kind:
1409 for (j = 0; j < asdl_seq_LEN(st->v.Try.handlers); j++) {
1410 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
1411 st->v.Try.handlers, j);
1412 if (find_ann(handler->v.ExceptHandler.body)) {
1413 return 1;
1414 }
1415 }
1416 res = find_ann(st->v.Try.body) ||
1417 find_ann(st->v.Try.finalbody) ||
1418 find_ann(st->v.Try.orelse);
1419 break;
1420 default:
1421 res = 0;
1422 }
1423 if (res) {
1424 break;
1425 }
1426 }
1427 return res;
1428 }
1429
1430 /* Compile a sequence of statements, checking for a docstring
1431 and for annotations. */
1330 1432
1331 static int 1433 static int
1332 compiler_body(struct compiler *c, asdl_seq *stmts) 1434 compiler_body(struct compiler *c, asdl_seq *stmts)
1333 { 1435 {
1334 int i = 0; 1436 int i = 0;
1335 stmt_ty st; 1437 stmt_ty st;
1336 1438
1439 /* Set current line number to the line number of first statement.
1440 This way line number for SETUP_ANNOTATIONS will always
1441 coincide with the line number of first "real" statement in module.
1442 If body is empy, then lineno will be set later in assemble. */
1443 if (c->u->u_scope_type == COMPILER_SCOPE_MODULE &&
1444 !c->u->u_lineno && asdl_seq_LEN(stmts)) {
1445 st = (stmt_ty)asdl_seq_GET(stmts, 0);
1446 c->u->u_lineno = st->lineno;
1447 }
1448 /* Every annotated class and module should have __annotations__. */
1449 if (find_ann(stmts)) {
1450 ADDOP(c, SETUP_ANNOTATIONS);
1451 }
1337 if (!asdl_seq_LEN(stmts)) 1452 if (!asdl_seq_LEN(stmts))
1338 return 1; 1453 return 1;
1339 st = (stmt_ty)asdl_seq_GET(stmts, 0); 1454 st = (stmt_ty)asdl_seq_GET(stmts, 0);
1340 if (compiler_isdocstring(st) && c->c_optimize < 2) { 1455 if (compiler_isdocstring(st) && c->c_optimize < 2) {
1341 /* don't generate docstrings if -OO */ 1456 /* don't generate docstrings if -OO */
1342 i = 1; 1457 i = 1;
1343 VISIT(c, expr, st->v.Expr.value); 1458 VISIT(c, expr, st->v.Expr.value);
1344 if (!compiler_nameop(c, __doc__, Store)) 1459 if (!compiler_nameop(c, __doc__, Store))
1345 return 0; 1460 return 0;
1346 } 1461 }
(...skipping 17 matching lines...) Expand all
1364 if (!compiler_enter_scope(c, module, COMPILER_SCOPE_MODULE, mod, 0)) 1479 if (!compiler_enter_scope(c, module, COMPILER_SCOPE_MODULE, mod, 0))
1365 return NULL; 1480 return NULL;
1366 switch (mod->kind) { 1481 switch (mod->kind) {
1367 case Module_kind: 1482 case Module_kind:
1368 if (!compiler_body(c, mod->v.Module.body)) { 1483 if (!compiler_body(c, mod->v.Module.body)) {
1369 compiler_exit_scope(c); 1484 compiler_exit_scope(c);
1370 return 0; 1485 return 0;
1371 } 1486 }
1372 break; 1487 break;
1373 case Interactive_kind: 1488 case Interactive_kind:
1489 if (find_ann(mod->v.Interactive.body)) {
1490 ADDOP(c, SETUP_ANNOTATIONS);
1491 }
1374 c->c_interactive = 1; 1492 c->c_interactive = 1;
1375 VISIT_SEQ_IN_SCOPE(c, stmt, 1493 VISIT_SEQ_IN_SCOPE(c, stmt,
1376 mod->v.Interactive.body); 1494 mod->v.Interactive.body);
1377 break; 1495 break;
1378 case Expression_kind: 1496 case Expression_kind:
1379 VISIT_IN_SCOPE(c, expr, mod->v.Expression.body); 1497 VISIT_IN_SCOPE(c, expr, mod->v.Expression.body);
1380 addNone = 0; 1498 addNone = 0;
1381 break; 1499 break;
1382 case Suite_kind: 1500 case Suite_kind:
1383 PyErr_SetString(PyExc_SystemError, 1501 PyErr_SetString(PyExc_SystemError,
(...skipping 13 matching lines...) Expand all
1397 /* The test for LOCAL must come before the test for FREE in order to 1515 /* The test for LOCAL must come before the test for FREE in order to
1398 handle classes where name is both local and free. The local var is 1516 handle classes where name is both local and free. The local var is
1399 a method and the free var is a free var referenced within a method. 1517 a method and the free var is a free var referenced within a method.
1400 */ 1518 */
1401 1519
1402 static int 1520 static int
1403 get_ref_type(struct compiler *c, PyObject *name) 1521 get_ref_type(struct compiler *c, PyObject *name)
1404 { 1522 {
1405 int scope; 1523 int scope;
1406 if (c->u->u_scope_type == COMPILER_SCOPE_CLASS && 1524 if (c->u->u_scope_type == COMPILER_SCOPE_CLASS &&
1407 !PyUnicode_CompareWithASCIIString(name, "__class__")) 1525 _PyUnicode_EqualToASCIIString(name, "__class__"))
1408 return CELL; 1526 return CELL;
1409 scope = PyST_GetScope(c->u->u_ste, name); 1527 scope = PyST_GetScope(c->u->u_ste, name);
1410 if (scope == 0) { 1528 if (scope == 0) {
1411 char buf[350]; 1529 char buf[350];
1412 PyOS_snprintf(buf, sizeof(buf), 1530 PyOS_snprintf(buf, sizeof(buf),
1413 "unknown scope for %.100s in %.100s(%s)\n" 1531 "unknown scope for %.100s in %.100s(%s)\n"
1414 "symbols: %s\nlocals: %s\nglobals: %s", 1532 "symbols: %s\nlocals: %s\nglobals: %s",
1415 PyUnicode_AsUTF8(name), 1533 PyUnicode_AsUTF8(name),
1416 PyUnicode_AsUTF8(c->u->u_name), 1534 PyUnicode_AsUTF8(c->u->u_name),
1417 PyUnicode_AsUTF8(PyObject_Repr(c->u->u_ste->ste_id)), 1535 PyUnicode_AsUTF8(PyObject_Repr(c->u->u_ste->ste_id)),
1418 PyUnicode_AsUTF8(PyObject_Repr(c->u->u_ste->ste_symbols)), 1536 PyUnicode_AsUTF8(PyObject_Repr(c->u->u_ste->ste_symbols)),
1419 PyUnicode_AsUTF8(PyObject_Repr(c->u->u_varnames)), 1537 PyUnicode_AsUTF8(PyObject_Repr(c->u->u_varnames)),
1420 PyUnicode_AsUTF8(PyObject_Repr(c->u->u_names)) 1538 PyUnicode_AsUTF8(PyObject_Repr(c->u->u_names))
1421 ); 1539 );
1422 Py_FatalError(buf); 1540 Py_FatalError(buf);
1423 } 1541 }
1424 1542
1425 return scope; 1543 return scope;
1426 } 1544 }
1427 1545
1428 static int 1546 static int
1429 compiler_lookup_arg(PyObject *dict, PyObject *name) 1547 compiler_lookup_arg(PyObject *dict, PyObject *name)
1430 { 1548 {
1431 PyObject *k, *v; 1549 PyObject *k, *v;
1432 k = PyTuple_Pack(2, name, name->ob_type); 1550 k = _PyCode_ConstantKey(name);
1433 if (k == NULL) 1551 if (k == NULL)
1434 return -1; 1552 return -1;
1435 v = PyDict_GetItem(dict, k); 1553 v = PyDict_GetItem(dict, k);
1436 Py_DECREF(k); 1554 Py_DECREF(k);
1437 if (v == NULL) 1555 if (v == NULL)
1438 return -1; 1556 return -1;
1439 return PyLong_AS_LONG(v); 1557 return PyLong_AS_LONG(v);
1440 } 1558 }
1441 1559
1442 static int 1560 static int
1443 compiler_make_closure(struct compiler *c, PyCodeObject *co, Py_ssize_t args, PyO bject *qualname) 1561 compiler_make_closure(struct compiler *c, PyCodeObject *co, Py_ssize_t flags, Py Object *qualname)
1444 { 1562 {
1445 Py_ssize_t i, free = PyCode_GetNumFree(co); 1563 Py_ssize_t i, free = PyCode_GetNumFree(co);
1446 if (qualname == NULL) 1564 if (qualname == NULL)
1447 qualname = co->co_name; 1565 qualname = co->co_name;
1448 1566
1449 if (free == 0) { 1567 if (free) {
1450 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts); 1568 for (i = 0; i < free; ++i) {
1451 ADDOP_O(c, LOAD_CONST, qualname, consts); 1569 /* Bypass com_addop_varname because it will generate
1452 ADDOP_I(c, MAKE_FUNCTION, args); 1570 LOAD_DEREF but LOAD_CLOSURE is needed.
1453 return 1; 1571 */
1454 } 1572 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
1455 for (i = 0; i < free; ++i) { 1573 int arg, reftype;
1456 /* Bypass com_addop_varname because it will generate 1574
1457 LOAD_DEREF but LOAD_CLOSURE is needed. 1575 /* Special case: If a class contains a method with a
1458 */ 1576 free variable that has the same name as a method,
1459 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i); 1577 the name will be considered free *and* local in the
1460 int arg, reftype; 1578 class. It should be handled by the closure, as
1461 1579 well as by the normal name loookup logic.
1462 /* Special case: If a class contains a method with a 1580 */
1463 free variable that has the same name as a method, 1581 reftype = get_ref_type(c, name);
1464 the name will be considered free *and* local in the 1582 if (reftype == CELL)
1465 class. It should be handled by the closure, as 1583 arg = compiler_lookup_arg(c->u->u_cellvars, name);
1466 well as by the normal name loookup logic. 1584 else /* (reftype == FREE) */
1467 */ 1585 arg = compiler_lookup_arg(c->u->u_freevars, name);
1468 reftype = get_ref_type(c, name); 1586 if (arg == -1) {
1469 if (reftype == CELL) 1587 fprintf(stderr,
1470 arg = compiler_lookup_arg(c->u->u_cellvars, name); 1588 "lookup %s in %s %d %d\n"
1471 else /* (reftype == FREE) */ 1589 "freevars of %s: %s\n",
1472 arg = compiler_lookup_arg(c->u->u_freevars, name); 1590 PyUnicode_AsUTF8(PyObject_Repr(name)),
1473 if (arg == -1) { 1591 PyUnicode_AsUTF8(c->u->u_name),
1474 fprintf(stderr, 1592 reftype, arg,
1475 "lookup %s in %s %d %d\n" 1593 PyUnicode_AsUTF8(co->co_name),
1476 "freevars of %s: %s\n", 1594 PyUnicode_AsUTF8(PyObject_Repr(co->co_freevars)));
1477 PyUnicode_AsUTF8(PyObject_Repr(name)), 1595 Py_FatalError("compiler_make_closure()");
1478 PyUnicode_AsUTF8(c->u->u_name), 1596 }
1479 reftype, arg, 1597 ADDOP_I(c, LOAD_CLOSURE, arg);
1480 PyUnicode_AsUTF8(co->co_name), 1598 }
1481 PyUnicode_AsUTF8(PyObject_Repr(co->co_freevars))); 1599 flags |= 0x08;
1482 Py_FatalError("compiler_make_closure()"); 1600 ADDOP_I(c, BUILD_TUPLE, free);
1483 } 1601 }
1484 ADDOP_I(c, LOAD_CLOSURE, arg);
1485 }
1486 ADDOP_I(c, BUILD_TUPLE, free);
1487 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts); 1602 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
1488 ADDOP_O(c, LOAD_CONST, qualname, consts); 1603 ADDOP_O(c, LOAD_CONST, qualname, consts);
1489 ADDOP_I(c, MAKE_CLOSURE, args); 1604 ADDOP_I(c, MAKE_FUNCTION, flags);
1490 return 1; 1605 return 1;
1491 } 1606 }
1492 1607
1493 static int 1608 static int
1494 compiler_decorators(struct compiler *c, asdl_seq* decos) 1609 compiler_decorators(struct compiler *c, asdl_seq* decos)
1495 { 1610 {
1496 int i; 1611 int i;
1497 1612
1498 if (!decos) 1613 if (!decos)
1499 return 1; 1614 return 1;
1500 1615
1501 for (i = 0; i < asdl_seq_LEN(decos); i++) { 1616 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1502 VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i)); 1617 VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i));
1503 } 1618 }
1504 return 1; 1619 return 1;
1505 } 1620 }
1506 1621
1507 static int 1622 static int
1508 compiler_visit_kwonlydefaults(struct compiler *c, asdl_seq *kwonlyargs, 1623 compiler_visit_kwonlydefaults(struct compiler *c, asdl_seq *kwonlyargs,
1509 asdl_seq *kw_defaults) 1624 asdl_seq *kw_defaults)
1510 { 1625 {
1511 int i, default_count = 0; 1626 /* Push a dict of keyword-only default values.
1627
1628 Return 0 on error, -1 if no dict pushed, 1 if a dict is pushed.
1629 */
1630 int i;
1631 PyObject *keys = NULL;
1632
1512 for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) { 1633 for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) {
1513 arg_ty arg = asdl_seq_GET(kwonlyargs, i); 1634 arg_ty arg = asdl_seq_GET(kwonlyargs, i);
1514 expr_ty default_ = asdl_seq_GET(kw_defaults, i); 1635 expr_ty default_ = asdl_seq_GET(kw_defaults, i);
1515 if (default_) { 1636 if (default_) {
1516 PyObject *mangled = _Py_Mangle(c->u->u_private, arg->arg); 1637 PyObject *mangled = _Py_Mangle(c->u->u_private, arg->arg);
1517 if (!mangled) 1638 if (!mangled) {
1518 return -1; 1639 goto error;
1519 ADDOP_O(c, LOAD_CONST, mangled, consts); 1640 }
1520 Py_DECREF(mangled); 1641 if (keys == NULL) {
1642 keys = PyList_New(1);
1643 if (keys == NULL) {
1644 Py_DECREF(mangled);
1645 return 0;
1646 }
1647 PyList_SET_ITEM(keys, 0, mangled);
1648 }
1649 else {
1650 int res = PyList_Append(keys, mangled);
1651 Py_DECREF(mangled);
1652 if (res == -1) {
1653 goto error;
1654 }
1655 }
1521 if (!compiler_visit_expr(c, default_)) { 1656 if (!compiler_visit_expr(c, default_)) {
1522 return -1; 1657 goto error;
1523 } 1658 }
1524 default_count++; 1659 }
1525 } 1660 }
1526 } 1661 if (keys != NULL) {
1527 return default_count; 1662 Py_ssize_t default_count = PyList_GET_SIZE(keys);
1663 PyObject *keys_tuple = PyList_AsTuple(keys);
1664 Py_DECREF(keys);
1665 if (keys_tuple == NULL) {
1666 return 0;
1667 }
1668 ADDOP_N(c, LOAD_CONST, keys_tuple, consts);
1669 ADDOP_I(c, BUILD_CONST_KEY_MAP, default_count);
1670 assert(default_count > 0);
1671 return 1;
1672 }
1673 else {
1674 return -1;
1675 }
1676
1677 error:
1678 Py_XDECREF(keys);
1679 return 0;
1528 } 1680 }
1529 1681
1530 static int 1682 static int
1531 compiler_visit_argannotation(struct compiler *c, identifier id, 1683 compiler_visit_argannotation(struct compiler *c, identifier id,
1532 expr_ty annotation, PyObject *names) 1684 expr_ty annotation, PyObject *names)
1533 { 1685 {
1534 if (annotation) { 1686 if (annotation) {
1535 PyObject *mangled; 1687 PyObject *mangled;
1536 VISIT(c, expr, annotation); 1688 VISIT(c, expr, annotation);
1537 mangled = _Py_Mangle(c->u->u_private, id); 1689 mangled = _Py_Mangle(c->u->u_private, id);
1538 if (!mangled) 1690 if (!mangled)
1539 return -1; 1691 return 0;
1540 if (PyList_Append(names, mangled) < 0) { 1692 if (PyList_Append(names, mangled) < 0) {
1541 Py_DECREF(mangled); 1693 Py_DECREF(mangled);
1542 return -1; 1694 return 0;
1543 } 1695 }
1544 Py_DECREF(mangled); 1696 Py_DECREF(mangled);
1545 } 1697 }
1546 return 0; 1698 return 1;
1547 } 1699 }
1548 1700
1549 static int 1701 static int
1550 compiler_visit_argannotations(struct compiler *c, asdl_seq* args, 1702 compiler_visit_argannotations(struct compiler *c, asdl_seq* args,
1551 PyObject *names) 1703 PyObject *names)
1552 { 1704 {
1553 int i, error; 1705 int i;
1554 for (i = 0; i < asdl_seq_LEN(args); i++) { 1706 for (i = 0; i < asdl_seq_LEN(args); i++) {
1555 arg_ty arg = (arg_ty)asdl_seq_GET(args, i); 1707 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1556 error = compiler_visit_argannotation( 1708 if (!compiler_visit_argannotation(
1557 c, 1709 c,
1558 arg->arg, 1710 arg->arg,
1559 arg->annotation, 1711 arg->annotation,
1560 names); 1712 names))
1561 if (error) 1713 return 0;
1562 return error; 1714 }
1563 } 1715 return 1;
1564 return 0;
1565 } 1716 }
1566 1717
1567 static int 1718 static int
1568 compiler_visit_annotations(struct compiler *c, arguments_ty args, 1719 compiler_visit_annotations(struct compiler *c, arguments_ty args,
1569 expr_ty returns) 1720 expr_ty returns)
1570 { 1721 {
1571 /* Push arg annotations and a list of the argument names. Return the # 1722 /* Push arg annotation dict.
1572 of items pushed. The expressions are evaluated out-of-order wrt the 1723 The expressions are evaluated out-of-order wrt the source code.
1573 source code. 1724
1574 1725 Return 0 on error, -1 if no dict pushed, 1 if a dict is pushed.
1575 More than 2^16-1 annotations is a SyntaxError. Returns -1 on error.
1576 */ 1726 */
1577 static identifier return_str; 1727 static identifier return_str;
1578 PyObject *names; 1728 PyObject *names;
1579 Py_ssize_t len; 1729 Py_ssize_t len;
1580 names = PyList_New(0); 1730 names = PyList_New(0);
1581 if (!names) 1731 if (!names)
1582 return -1; 1732 return 0;
1583 1733
1584 if (compiler_visit_argannotations(c, args->args, names)) 1734 if (!compiler_visit_argannotations(c, args->args, names))
1585 goto error; 1735 goto error;
1586 if (args->vararg && args->vararg->annotation && 1736 if (args->vararg && args->vararg->annotation &&
1587 compiler_visit_argannotation(c, args->vararg->arg, 1737 !compiler_visit_argannotation(c, args->vararg->arg,
1588 args->vararg->annotation, names)) 1738 args->vararg->annotation, names))
1589 goto error; 1739 goto error;
1590 if (compiler_visit_argannotations(c, args->kwonlyargs, names)) 1740 if (!compiler_visit_argannotations(c, args->kwonlyargs, names))
1591 goto error; 1741 goto error;
1592 if (args->kwarg && args->kwarg->annotation && 1742 if (args->kwarg && args->kwarg->annotation &&
1593 compiler_visit_argannotation(c, args->kwarg->arg, 1743 !compiler_visit_argannotation(c, args->kwarg->arg,
1594 args->kwarg->annotation, names)) 1744 args->kwarg->annotation, names))
1595 goto error; 1745 goto error;
1596 1746
1597 if (!return_str) { 1747 if (!return_str) {
1598 return_str = PyUnicode_InternFromString("return"); 1748 return_str = PyUnicode_InternFromString("return");
1599 if (!return_str) 1749 if (!return_str)
1600 goto error; 1750 goto error;
1601 } 1751 }
1602 if (compiler_visit_argannotation(c, return_str, returns, names)) { 1752 if (!compiler_visit_argannotation(c, return_str, returns, names)) {
1603 goto error; 1753 goto error;
1604 } 1754 }
1605 1755
1606 len = PyList_GET_SIZE(names); 1756 len = PyList_GET_SIZE(names);
1607 if (len > 65534) {
1608 /* len must fit in 16 bits, and len is incremented below */
1609 PyErr_SetString(PyExc_SyntaxError,
1610 "too many annotations");
1611 goto error;
1612 }
1613 if (len) { 1757 if (len) {
1614 /* convert names to a tuple and place on stack */ 1758 PyObject *keytuple = PyList_AsTuple(names);
1615 PyObject *elt; 1759 Py_DECREF(names);
1616 Py_ssize_t i; 1760 if (keytuple == NULL) {
1617 PyObject *s = PyTuple_New(len); 1761 return 0;
1618 if (!s) 1762 }
1619 goto error; 1763 ADDOP_N(c, LOAD_CONST, keytuple, consts);
1620 for (i = 0; i < len; i++) { 1764 ADDOP_I(c, BUILD_CONST_KEY_MAP, len);
1621 elt = PyList_GET_ITEM(names, i); 1765 return 1;
1622 Py_INCREF(elt); 1766 }
1623 PyTuple_SET_ITEM(s, i, elt); 1767 else {
1624 } 1768 Py_DECREF(names);
1625 ADDOP_O(c, LOAD_CONST, s, consts); 1769 return -1;
1626 Py_DECREF(s); 1770 }
1627 len++; /* include the just-pushed tuple */
1628 }
1629 Py_DECREF(names);
1630
1631 /* We just checked that len <= 65535, see above */
1632 return Py_SAFE_DOWNCAST(len, Py_ssize_t, int);
1633 1771
1634 error: 1772 error:
1635 Py_DECREF(names); 1773 Py_DECREF(names);
1636 return -1; 1774 return 0;
1637 } 1775 }
1638 1776
1639 static int 1777 static int
1640 compiler_function(struct compiler *c, stmt_ty s) 1778 compiler_visit_defaults(struct compiler *c, arguments_ty args)
1641 { 1779 {
1642 PyCodeObject *co; 1780 VISIT_SEQ(c, expr, args->defaults);
1643 PyObject *qualname, *first_const = Py_None; 1781 ADDOP_I(c, BUILD_TUPLE, asdl_seq_LEN(args->defaults));
1644 arguments_ty args = s->v.FunctionDef.args; 1782 return 1;
1645 expr_ty returns = s->v.FunctionDef.returns; 1783 }
1646 asdl_seq* decos = s->v.FunctionDef.decorator_list; 1784
1647 stmt_ty st; 1785 static Py_ssize_t
1648 Py_ssize_t i, n, arglength; 1786 compiler_default_arguments(struct compiler *c, arguments_ty args)
1649 int docstring, kw_default_count = 0; 1787 {
1650 int num_annotations; 1788 Py_ssize_t funcflags = 0;
1651 1789 if (args->defaults && asdl_seq_LEN(args->defaults) > 0) {
1652 assert(s->kind == FunctionDef_kind); 1790 if (!compiler_visit_defaults(c, args))
1653 1791 return -1;
1654 if (!compiler_decorators(c, decos)) 1792 funcflags |= 0x01;
1655 return 0; 1793 }
1656 if (args->defaults)
1657 VISIT_SEQ(c, expr, args->defaults);
1658 if (args->kwonlyargs) { 1794 if (args->kwonlyargs) {
1659 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs, 1795 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
1660 args->kw_defaults); 1796 args->kw_defaults);
1661 if (res < 0) 1797 if (res == 0) {
1662 return 0; 1798 return -1;
1663 kw_default_count = res; 1799 }
1664 } 1800 else if (res > 0) {
1665 num_annotations = compiler_visit_annotations(c, args, returns); 1801 funcflags |= 0x02;
1666 if (num_annotations < 0) 1802 }
1667 return 0; 1803 }
1668 assert((num_annotations & 0xFFFF) == num_annotations); 1804 return funcflags;
1669 1805 }
1670 if (!compiler_enter_scope(c, s->v.FunctionDef.name, 1806
1671 COMPILER_SCOPE_FUNCTION, (void *)s, 1807 static int
1672 s->lineno)) 1808 compiler_function(struct compiler *c, stmt_ty s, int is_async)
1673 return 0; 1809 {
1674 1810 PyCodeObject *co;
1675 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, 0); 1811 PyObject *qualname, *first_const = Py_None;
1812 arguments_ty args;
1813 expr_ty returns;
1814 identifier name;
1815 asdl_seq* decos;
1816 asdl_seq *body;
1817 stmt_ty st;
1818 Py_ssize_t i, n, funcflags;
1819 int docstring;
1820 int annotations;
1821 int scope_type;
1822
1823 if (is_async) {
1824 assert(s->kind == AsyncFunctionDef_kind);
1825
1826 args = s->v.AsyncFunctionDef.args;
1827 returns = s->v.AsyncFunctionDef.returns;
1828 decos = s->v.AsyncFunctionDef.decorator_list;
1829 name = s->v.AsyncFunctionDef.name;
1830 body = s->v.AsyncFunctionDef.body;
1831
1832 scope_type = COMPILER_SCOPE_ASYNC_FUNCTION;
1833 } else {
1834 assert(s->kind == FunctionDef_kind);
1835
1836 args = s->v.FunctionDef.args;
1837 returns = s->v.FunctionDef.returns;
1838 decos = s->v.FunctionDef.decorator_list;
1839 name = s->v.FunctionDef.name;
1840 body = s->v.FunctionDef.body;
1841
1842 scope_type = COMPILER_SCOPE_FUNCTION;
1843 }
1844
1845 if (!compiler_decorators(c, decos))
1846 return 0;
1847
1848 funcflags = compiler_default_arguments(c, args);
1849 if (funcflags == -1) {
1850 return 0;
1851 }
1852
1853 annotations = compiler_visit_annotations(c, args, returns);
1854 if (annotations == 0) {
1855 return 0;
1856 }
1857 else if (annotations > 0) {
1858 funcflags |= 0x04;
1859 }
1860
1861 if (!compiler_enter_scope(c, name, scope_type, (void *)s, s->lineno)) {
1862 return 0;
1863 }
1864
1865 st = (stmt_ty)asdl_seq_GET(body, 0);
1676 docstring = compiler_isdocstring(st); 1866 docstring = compiler_isdocstring(st);
1677 if (docstring && c->c_optimize < 2) 1867 if (docstring && c->c_optimize < 2) {
1678 first_const = st->v.Expr.value->v.Str.s; 1868 if (st->v.Expr.value->kind == Constant_kind)
1869 first_const = st->v.Expr.value->v.Constant.value;
1870 else
1871 first_const = st->v.Expr.value->v.Str.s;
1872 }
1679 if (compiler_add_o(c, c->u->u_consts, first_const) < 0) { 1873 if (compiler_add_o(c, c->u->u_consts, first_const) < 0) {
1680 compiler_exit_scope(c); 1874 compiler_exit_scope(c);
1681 return 0; 1875 return 0;
1682 } 1876 }
1683 1877
1684 c->u->u_argcount = asdl_seq_LEN(args->args); 1878 c->u->u_argcount = asdl_seq_LEN(args->args);
1685 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs); 1879 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
1686 n = asdl_seq_LEN(s->v.FunctionDef.body); 1880 n = asdl_seq_LEN(body);
1687 /* if there was a docstring, we need to skip the first statement */ 1881 /* if there was a docstring, we need to skip the first statement */
1688 for (i = docstring; i < n; i++) { 1882 for (i = docstring; i < n; i++) {
1689 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, i); 1883 st = (stmt_ty)asdl_seq_GET(body, i);
1690 VISIT_IN_SCOPE(c, stmt, st); 1884 VISIT_IN_SCOPE(c, stmt, st);
1691 } 1885 }
1692 co = assemble(c, 1); 1886 co = assemble(c, 1);
1693 qualname = c->u->u_qualname; 1887 qualname = c->u->u_qualname;
1694 Py_INCREF(qualname); 1888 Py_INCREF(qualname);
1695 compiler_exit_scope(c); 1889 compiler_exit_scope(c);
1696 if (co == NULL) { 1890 if (co == NULL) {
1697 Py_XDECREF(qualname); 1891 Py_XDECREF(qualname);
1698 Py_XDECREF(co); 1892 Py_XDECREF(co);
1699 return 0; 1893 return 0;
1700 } 1894 }
1701 1895
1702 arglength = asdl_seq_LEN(args->defaults); 1896 compiler_make_closure(c, co, funcflags, qualname);
1703 arglength |= kw_default_count << 8;
1704 arglength |= num_annotations << 16;
1705 compiler_make_closure(c, co, arglength, qualname);
1706 Py_DECREF(qualname); 1897 Py_DECREF(qualname);
1707 Py_DECREF(co); 1898 Py_DECREF(co);
1708 1899
1709 /* decorators */ 1900 /* decorators */
1710 for (i = 0; i < asdl_seq_LEN(decos); i++) { 1901 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1711 ADDOP_I(c, CALL_FUNCTION, 1); 1902 ADDOP_I(c, CALL_FUNCTION, 1);
1712 } 1903 }
1713 1904
1714 return compiler_nameop(c, s->v.FunctionDef.name, Store); 1905 return compiler_nameop(c, name, Store);
1715 } 1906 }
1716 1907
1717 static int 1908 static int
1718 compiler_class(struct compiler *c, stmt_ty s) 1909 compiler_class(struct compiler *c, stmt_ty s)
1719 { 1910 {
1720 PyCodeObject *co; 1911 PyCodeObject *co;
1721 PyObject *str; 1912 PyObject *str;
1722 int i; 1913 int i;
1723 asdl_seq* decos = s->v.ClassDef.decorator_list; 1914 asdl_seq* decos = s->v.ClassDef.decorator_list;
1724 1915
(...skipping 13 matching lines...) Expand all
1738 */ 1929 */
1739 1930
1740 /* 1. compile the class body into a code object */ 1931 /* 1. compile the class body into a code object */
1741 if (!compiler_enter_scope(c, s->v.ClassDef.name, 1932 if (!compiler_enter_scope(c, s->v.ClassDef.name,
1742 COMPILER_SCOPE_CLASS, (void *)s, s->lineno)) 1933 COMPILER_SCOPE_CLASS, (void *)s, s->lineno))
1743 return 0; 1934 return 0;
1744 /* this block represents what we do in the new scope */ 1935 /* this block represents what we do in the new scope */
1745 { 1936 {
1746 /* use the class name for name mangling */ 1937 /* use the class name for name mangling */
1747 Py_INCREF(s->v.ClassDef.name); 1938 Py_INCREF(s->v.ClassDef.name);
1748 Py_XDECREF(c->u->u_private); 1939 Py_XSETREF(c->u->u_private, s->v.ClassDef.name);
1749 c->u->u_private = s->v.ClassDef.name;
1750 /* load (global) __name__ ... */ 1940 /* load (global) __name__ ... */
1751 str = PyUnicode_InternFromString("__name__"); 1941 str = PyUnicode_InternFromString("__name__");
1752 if (!str || !compiler_nameop(c, str, Load)) { 1942 if (!str || !compiler_nameop(c, str, Load)) {
1753 Py_XDECREF(str); 1943 Py_XDECREF(str);
1754 compiler_exit_scope(c); 1944 compiler_exit_scope(c);
1755 return 0; 1945 return 0;
1756 } 1946 }
1757 Py_DECREF(str); 1947 Py_DECREF(str);
1758 /* ... and store it as __module__ */ 1948 /* ... and store it as __module__ */
1759 str = PyUnicode_InternFromString("__module__"); 1949 str = PyUnicode_InternFromString("__module__");
(...skipping 10 matching lines...) Expand all
1770 Py_XDECREF(str); 1960 Py_XDECREF(str);
1771 compiler_exit_scope(c); 1961 compiler_exit_scope(c);
1772 return 0; 1962 return 0;
1773 } 1963 }
1774 Py_DECREF(str); 1964 Py_DECREF(str);
1775 /* compile the body proper */ 1965 /* compile the body proper */
1776 if (!compiler_body(c, s->v.ClassDef.body)) { 1966 if (!compiler_body(c, s->v.ClassDef.body)) {
1777 compiler_exit_scope(c); 1967 compiler_exit_scope(c);
1778 return 0; 1968 return 0;
1779 } 1969 }
1970 /* Return __classcell__ if it is referenced, otherwise return None */
1780 if (c->u->u_ste->ste_needs_class_closure) { 1971 if (c->u->u_ste->ste_needs_class_closure) {
1781 /* return the (empty) __class__ cell */ 1972 /* Store __classcell__ into class namespace & return it */
1782 str = PyUnicode_InternFromString("__class__"); 1973 str = PyUnicode_InternFromString("__class__");
1783 if (str == NULL) { 1974 if (str == NULL) {
1784 compiler_exit_scope(c); 1975 compiler_exit_scope(c);
1785 return 0; 1976 return 0;
1786 } 1977 }
1787 i = compiler_lookup_arg(c->u->u_cellvars, str); 1978 i = compiler_lookup_arg(c->u->u_cellvars, str);
1788 Py_DECREF(str); 1979 Py_DECREF(str);
1789 if (i < 0) { 1980 if (i < 0) {
1790 compiler_exit_scope(c); 1981 compiler_exit_scope(c);
1791 return 0; 1982 return 0;
1792 } 1983 }
1793 assert(i == 0); 1984 assert(i == 0);
1794 1985
1795 /* store __cell__ into class namespace */
1796 ADDOP_I(c, LOAD_CLOSURE, i); 1986 ADDOP_I(c, LOAD_CLOSURE, i);
1797 str = PyUnicode_InternFromString("__cell__"); 1987 ADDOP(c, DUP_TOP);
1988 str = PyUnicode_InternFromString("__classcell__");
1798 if (!str || !compiler_nameop(c, str, Store)) { 1989 if (!str || !compiler_nameop(c, str, Store)) {
1799 Py_XDECREF(str); 1990 Py_XDECREF(str);
1800 compiler_exit_scope(c); 1991 compiler_exit_scope(c);
1801 return 0; 1992 return 0;
1802 } 1993 }
1803 Py_DECREF(str); 1994 Py_DECREF(str);
1804 } 1995 }
1805 else { 1996 else {
1997 /* No methods referenced __class__, so just return None */
1806 assert(PyDict_Size(c->u->u_cellvars) == 0); 1998 assert(PyDict_Size(c->u->u_cellvars) == 0);
1807 /* This happens when nobody references the cell. Return None. */ 1999 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1808 } 2000 }
1809 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1810 ADDOP_IN_SCOPE(c, RETURN_VALUE); 2001 ADDOP_IN_SCOPE(c, RETURN_VALUE);
1811 /* create the code object */ 2002 /* create the code object */
1812 co = assemble(c, 1); 2003 co = assemble(c, 1);
1813 } 2004 }
1814 /* leave the new scope */ 2005 /* leave the new scope */
1815 compiler_exit_scope(c); 2006 compiler_exit_scope(c);
1816 if (co == NULL) 2007 if (co == NULL)
1817 return 0; 2008 return 0;
1818 2009
1819 /* 2. load the 'build_class' function */ 2010 /* 2. load the 'build_class' function */
1820 ADDOP(c, LOAD_BUILD_CLASS); 2011 ADDOP(c, LOAD_BUILD_CLASS);
1821 2012
1822 /* 3. load a function (or closure) made from the code object */ 2013 /* 3. load a function (or closure) made from the code object */
1823 compiler_make_closure(c, co, 0, NULL); 2014 compiler_make_closure(c, co, 0, NULL);
1824 Py_DECREF(co); 2015 Py_DECREF(co);
1825 2016
1826 /* 4. load class name */ 2017 /* 4. load class name */
1827 ADDOP_O(c, LOAD_CONST, s->v.ClassDef.name, consts); 2018 ADDOP_O(c, LOAD_CONST, s->v.ClassDef.name, consts);
1828 2019
1829 /* 5. generate the rest of the code for the call */ 2020 /* 5. generate the rest of the code for the call */
1830 if (!compiler_call_helper(c, 2, 2021 if (!compiler_call_helper(c, 2,
1831 s->v.ClassDef.bases, 2022 s->v.ClassDef.bases,
1832 s->v.ClassDef.keywords, 2023 s->v.ClassDef.keywords))
1833 s->v.ClassDef.starargs,
1834 s->v.ClassDef.kwargs))
1835 return 0; 2024 return 0;
1836 2025
1837 /* 6. apply decorators */ 2026 /* 6. apply decorators */
1838 for (i = 0; i < asdl_seq_LEN(decos); i++) { 2027 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1839 ADDOP_I(c, CALL_FUNCTION, 1); 2028 ADDOP_I(c, CALL_FUNCTION, 1);
1840 } 2029 }
1841 2030
1842 /* 7. store into <name> */ 2031 /* 7. store into <name> */
1843 if (!compiler_nameop(c, s->v.ClassDef.name, Store)) 2032 if (!compiler_nameop(c, s->v.ClassDef.name, Store))
1844 return 0; 2033 return 0;
(...skipping 21 matching lines...) Expand all
1866 compiler_use_next_block(c, end); 2055 compiler_use_next_block(c, end);
1867 return 1; 2056 return 1;
1868 } 2057 }
1869 2058
1870 static int 2059 static int
1871 compiler_lambda(struct compiler *c, expr_ty e) 2060 compiler_lambda(struct compiler *c, expr_ty e)
1872 { 2061 {
1873 PyCodeObject *co; 2062 PyCodeObject *co;
1874 PyObject *qualname; 2063 PyObject *qualname;
1875 static identifier name; 2064 static identifier name;
1876 int kw_default_count = 0; 2065 Py_ssize_t funcflags;
1877 Py_ssize_t arglength;
1878 arguments_ty args = e->v.Lambda.args; 2066 arguments_ty args = e->v.Lambda.args;
1879 assert(e->kind == Lambda_kind); 2067 assert(e->kind == Lambda_kind);
1880 2068
1881 if (!name) { 2069 if (!name) {
1882 name = PyUnicode_InternFromString("<lambda>"); 2070 name = PyUnicode_InternFromString("<lambda>");
1883 if (!name) 2071 if (!name)
1884 return 0; 2072 return 0;
1885 } 2073 }
1886 2074
1887 if (args->defaults) 2075 funcflags = compiler_default_arguments(c, args);
1888 VISIT_SEQ(c, expr, args->defaults); 2076 if (funcflags == -1) {
1889 if (args->kwonlyargs) { 2077 return 0;
1890 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs, 2078 }
1891 args->kw_defaults); 2079
1892 if (res < 0) return 0;
1893 kw_default_count = res;
1894 }
1895 if (!compiler_enter_scope(c, name, COMPILER_SCOPE_LAMBDA, 2080 if (!compiler_enter_scope(c, name, COMPILER_SCOPE_LAMBDA,
1896 (void *)e, e->lineno)) 2081 (void *)e, e->lineno))
1897 return 0; 2082 return 0;
1898 2083
1899 /* Make None the first constant, so the lambda can't have a 2084 /* Make None the first constant, so the lambda can't have a
1900 docstring. */ 2085 docstring. */
1901 if (compiler_add_o(c, c->u->u_consts, Py_None) < 0) 2086 if (compiler_add_o(c, c->u->u_consts, Py_None) < 0)
1902 return 0; 2087 return 0;
1903 2088
1904 c->u->u_argcount = asdl_seq_LEN(args->args); 2089 c->u->u_argcount = asdl_seq_LEN(args->args);
1905 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs); 2090 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
1906 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body); 2091 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
1907 if (c->u->u_ste->ste_generator) { 2092 if (c->u->u_ste->ste_generator) {
1908 co = assemble(c, 0); 2093 co = assemble(c, 0);
1909 } 2094 }
1910 else { 2095 else {
1911 ADDOP_IN_SCOPE(c, RETURN_VALUE); 2096 ADDOP_IN_SCOPE(c, RETURN_VALUE);
1912 co = assemble(c, 1); 2097 co = assemble(c, 1);
1913 } 2098 }
1914 qualname = c->u->u_qualname; 2099 qualname = c->u->u_qualname;
1915 Py_INCREF(qualname); 2100 Py_INCREF(qualname);
1916 compiler_exit_scope(c); 2101 compiler_exit_scope(c);
1917 if (co == NULL) 2102 if (co == NULL)
1918 return 0; 2103 return 0;
1919 2104
1920 arglength = asdl_seq_LEN(args->defaults); 2105 compiler_make_closure(c, co, funcflags, qualname);
1921 arglength |= kw_default_count << 8;
1922 compiler_make_closure(c, co, arglength, qualname);
1923 Py_DECREF(qualname); 2106 Py_DECREF(qualname);
1924 Py_DECREF(co); 2107 Py_DECREF(co);
1925 2108
1926 return 1; 2109 return 1;
1927 } 2110 }
1928 2111
1929 static int 2112 static int
1930 compiler_if(struct compiler *c, stmt_ty s) 2113 compiler_if(struct compiler *c, stmt_ty s)
1931 { 2114 {
1932 basicblock *end, *next; 2115 basicblock *end, *next;
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
1984 compiler_use_next_block(c, start); 2167 compiler_use_next_block(c, start);
1985 ADDOP_JREL(c, FOR_ITER, cleanup); 2168 ADDOP_JREL(c, FOR_ITER, cleanup);
1986 VISIT(c, expr, s->v.For.target); 2169 VISIT(c, expr, s->v.For.target);
1987 VISIT_SEQ(c, stmt, s->v.For.body); 2170 VISIT_SEQ(c, stmt, s->v.For.body);
1988 ADDOP_JABS(c, JUMP_ABSOLUTE, start); 2171 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
1989 compiler_use_next_block(c, cleanup); 2172 compiler_use_next_block(c, cleanup);
1990 ADDOP(c, POP_BLOCK); 2173 ADDOP(c, POP_BLOCK);
1991 compiler_pop_fblock(c, LOOP, start); 2174 compiler_pop_fblock(c, LOOP, start);
1992 VISIT_SEQ(c, stmt, s->v.For.orelse); 2175 VISIT_SEQ(c, stmt, s->v.For.orelse);
1993 compiler_use_next_block(c, end); 2176 compiler_use_next_block(c, end);
2177 return 1;
2178 }
2179
2180
2181 static int
2182 compiler_async_for(struct compiler *c, stmt_ty s)
2183 {
2184 _Py_IDENTIFIER(StopAsyncIteration);
2185
2186 basicblock *try, *except, *end, *after_try, *try_cleanup,
2187 *after_loop, *after_loop_else;
2188
2189 PyObject *stop_aiter_error = _PyUnicode_FromId(&PyId_StopAsyncIteration);
2190 if (stop_aiter_error == NULL) {
2191 return 0;
2192 }
2193
2194 try = compiler_new_block(c);
2195 except = compiler_new_block(c);
2196 end = compiler_new_block(c);
2197 after_try = compiler_new_block(c);
2198 try_cleanup = compiler_new_block(c);
2199 after_loop = compiler_new_block(c);
2200 after_loop_else = compiler_new_block(c);
2201
2202 if (try == NULL || except == NULL || end == NULL
2203 || after_try == NULL || try_cleanup == NULL)
2204 return 0;
2205
2206 ADDOP_JREL(c, SETUP_LOOP, after_loop);
2207 if (!compiler_push_fblock(c, LOOP, try))
2208 return 0;
2209
2210 VISIT(c, expr, s->v.AsyncFor.iter);
2211 ADDOP(c, GET_AITER);
2212 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2213 ADDOP(c, YIELD_FROM);
2214
2215 compiler_use_next_block(c, try);
2216
2217
2218 ADDOP_JREL(c, SETUP_EXCEPT, except);
2219 if (!compiler_push_fblock(c, EXCEPT, try))
2220 return 0;
2221
2222 ADDOP(c, GET_ANEXT);
2223 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2224 ADDOP(c, YIELD_FROM);
2225 VISIT(c, expr, s->v.AsyncFor.target);
2226 ADDOP(c, POP_BLOCK);
2227 compiler_pop_fblock(c, EXCEPT, try);
2228 ADDOP_JREL(c, JUMP_FORWARD, after_try);
2229
2230
2231 compiler_use_next_block(c, except);
2232 ADDOP(c, DUP_TOP);
2233 ADDOP_O(c, LOAD_GLOBAL, stop_aiter_error, names);
2234 ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH);
2235 ADDOP_JABS(c, POP_JUMP_IF_FALSE, try_cleanup);
2236
2237 ADDOP(c, POP_TOP);
2238 ADDOP(c, POP_TOP);
2239 ADDOP(c, POP_TOP);
2240 ADDOP(c, POP_EXCEPT); /* for SETUP_EXCEPT */
2241 ADDOP(c, POP_BLOCK); /* for SETUP_LOOP */
2242 ADDOP_JABS(c, JUMP_ABSOLUTE, after_loop_else);
2243
2244
2245 compiler_use_next_block(c, try_cleanup);
2246 ADDOP(c, END_FINALLY);
2247
2248 compiler_use_next_block(c, after_try);
2249 VISIT_SEQ(c, stmt, s->v.AsyncFor.body);
2250 ADDOP_JABS(c, JUMP_ABSOLUTE, try);
2251
2252 ADDOP(c, POP_BLOCK); /* for SETUP_LOOP */
2253 compiler_pop_fblock(c, LOOP, try);
2254
2255 compiler_use_next_block(c, after_loop);
2256 ADDOP_JABS(c, JUMP_ABSOLUTE, end);
2257
2258 compiler_use_next_block(c, after_loop_else);
2259 VISIT_SEQ(c, stmt, s->v.For.orelse);
2260
2261 compiler_use_next_block(c, end);
2262
1994 return 1; 2263 return 1;
1995 } 2264 }
1996 2265
1997 static int 2266 static int
1998 compiler_while(struct compiler *c, stmt_ty s) 2267 compiler_while(struct compiler *c, stmt_ty s)
1999 { 2268 {
2000 basicblock *loop, *orelse, *end, *anchor = NULL; 2269 basicblock *loop, *orelse, *end, *anchor = NULL;
2001 int constant = expr_constant(c, s->v.While.test); 2270 int constant = expr_constant(c, s->v.While.test);
2002 2271
2003 if (constant == 0) { 2272 if (constant == 0) {
(...skipping 306 matching lines...) Expand 10 before | Expand all | Expand 10 after
2310 { 2579 {
2311 /* The IMPORT_NAME opcode was already generated. This function 2580 /* The IMPORT_NAME opcode was already generated. This function
2312 merely needs to bind the result to a name. 2581 merely needs to bind the result to a name.
2313 2582
2314 If there is a dot in name, we need to split it and emit a 2583 If there is a dot in name, we need to split it and emit a
2315 LOAD_ATTR for each name. 2584 LOAD_ATTR for each name.
2316 */ 2585 */
2317 Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0, 2586 Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0,
2318 PyUnicode_GET_LENGTH(name), 1); 2587 PyUnicode_GET_LENGTH(name), 1);
2319 if (dot == -2) 2588 if (dot == -2)
2320 return -1; 2589 return 0;
2321 if (dot != -1) { 2590 if (dot != -1) {
2322 /* Consume the base module name to get the first attribute */ 2591 /* Consume the base module name to get the first attribute */
2323 Py_ssize_t pos = dot + 1; 2592 Py_ssize_t pos = dot + 1;
2324 while (dot != -1) { 2593 while (dot != -1) {
2325 PyObject *attr; 2594 PyObject *attr;
2326 dot = PyUnicode_FindChar(name, '.', pos, 2595 dot = PyUnicode_FindChar(name, '.', pos,
2327 PyUnicode_GET_LENGTH(name), 1); 2596 PyUnicode_GET_LENGTH(name), 1);
2328 if (dot == -2) 2597 if (dot == -2)
2329 return -1; 2598 return 0;
2330 attr = PyUnicode_Substring(name, pos, 2599 attr = PyUnicode_Substring(name, pos,
2331 (dot != -1) ? dot : 2600 (dot != -1) ? dot :
2332 PyUnicode_GET_LENGTH(name)); 2601 PyUnicode_GET_LENGTH(name));
2333 if (!attr) 2602 if (!attr)
2334 return -1; 2603 return 0;
2335 ADDOP_O(c, LOAD_ATTR, attr, names); 2604 ADDOP_O(c, LOAD_ATTR, attr, names);
2336 Py_DECREF(attr); 2605 Py_DECREF(attr);
2337 pos = dot + 1; 2606 pos = dot + 1;
2338 } 2607 }
2339 } 2608 }
2340 return compiler_nameop(c, asname, Store); 2609 return compiler_nameop(c, asname, Store);
2341 } 2610 }
2342 2611
2343 static int 2612 static int
2344 compiler_import(struct compiler *c, stmt_ty s) 2613 compiler_import(struct compiler *c, stmt_ty s)
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
2416 } 2685 }
2417 2686
2418 /* build up the names */ 2687 /* build up the names */
2419 for (i = 0; i < n; i++) { 2688 for (i = 0; i < n; i++) {
2420 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i); 2689 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
2421 Py_INCREF(alias->name); 2690 Py_INCREF(alias->name);
2422 PyTuple_SET_ITEM(names, i, alias->name); 2691 PyTuple_SET_ITEM(names, i, alias->name);
2423 } 2692 }
2424 2693
2425 if (s->lineno > c->c_future->ff_lineno && s->v.ImportFrom.module && 2694 if (s->lineno > c->c_future->ff_lineno && s->v.ImportFrom.module &&
2426 !PyUnicode_CompareWithASCIIString(s->v.ImportFrom.module, "__future__")) { 2695 _PyUnicode_EqualToASCIIString(s->v.ImportFrom.module, "__future__")) {
2427 Py_DECREF(level); 2696 Py_DECREF(level);
2428 Py_DECREF(names); 2697 Py_DECREF(names);
2429 return compiler_error(c, "from __future__ imports must occur " 2698 return compiler_error(c, "from __future__ imports must occur "
2430 "at the beginning of the file"); 2699 "at the beginning of the file");
2431 } 2700 }
2432 2701
2433 ADDOP_O(c, LOAD_CONST, level, consts); 2702 ADDOP_O(c, LOAD_CONST, level, consts);
2434 Py_DECREF(level); 2703 Py_DECREF(level);
2435 ADDOP_O(c, LOAD_CONST, names, consts); 2704 ADDOP_O(c, LOAD_CONST, names, consts);
2436 Py_DECREF(names); 2705 Py_DECREF(names);
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
2502 if (s->v.Assert.msg) { 2771 if (s->v.Assert.msg) {
2503 VISIT(c, expr, s->v.Assert.msg); 2772 VISIT(c, expr, s->v.Assert.msg);
2504 ADDOP_I(c, CALL_FUNCTION, 1); 2773 ADDOP_I(c, CALL_FUNCTION, 1);
2505 } 2774 }
2506 ADDOP_I(c, RAISE_VARARGS, 1); 2775 ADDOP_I(c, RAISE_VARARGS, 1);
2507 compiler_use_next_block(c, end); 2776 compiler_use_next_block(c, end);
2508 return 1; 2777 return 1;
2509 } 2778 }
2510 2779
2511 static int 2780 static int
2781 compiler_visit_stmt_expr(struct compiler *c, expr_ty value)
2782 {
2783 if (c->c_interactive && c->c_nestlevel <= 1) {
2784 VISIT(c, expr, value);
2785 ADDOP(c, PRINT_EXPR);
2786 return 1;
2787 }
2788
2789 if (is_const(value)) {
2790 /* ignore constant statement */
2791 return 1;
2792 }
2793
2794 VISIT(c, expr, value);
2795 ADDOP(c, POP_TOP);
2796 return 1;
2797 }
2798
2799 static int
2512 compiler_visit_stmt(struct compiler *c, stmt_ty s) 2800 compiler_visit_stmt(struct compiler *c, stmt_ty s)
2513 { 2801 {
2514 Py_ssize_t i, n; 2802 Py_ssize_t i, n;
2515 2803
2516 /* Always assign a lineno to the next instruction for a stmt. */ 2804 /* Always assign a lineno to the next instruction for a stmt. */
2517 c->u->u_lineno = s->lineno; 2805 c->u->u_lineno = s->lineno;
2518 c->u->u_col_offset = s->col_offset; 2806 c->u->u_col_offset = s->col_offset;
2519 c->u->u_lineno_set = 0; 2807 c->u->u_lineno_set = 0;
2520 2808
2521 switch (s->kind) { 2809 switch (s->kind) {
2522 case FunctionDef_kind: 2810 case FunctionDef_kind:
2523 return compiler_function(c, s); 2811 return compiler_function(c, s, 0);
2524 case ClassDef_kind: 2812 case ClassDef_kind:
2525 return compiler_class(c, s); 2813 return compiler_class(c, s);
2526 case Return_kind: 2814 case Return_kind:
2527 if (c->u->u_ste->ste_type != FunctionBlock) 2815 if (c->u->u_ste->ste_type != FunctionBlock)
2528 return compiler_error(c, "'return' outside function"); 2816 return compiler_error(c, "'return' outside function");
2529 if (s->v.Return.value) { 2817 if (s->v.Return.value) {
2818 if (c->u->u_ste->ste_coroutine && c->u->u_ste->ste_generator)
2819 return compiler_error(
2820 c, "'return' with value in async generator");
2530 VISIT(c, expr, s->v.Return.value); 2821 VISIT(c, expr, s->v.Return.value);
2531 } 2822 }
2532 else 2823 else
2533 ADDOP_O(c, LOAD_CONST, Py_None, consts); 2824 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2534 ADDOP(c, RETURN_VALUE); 2825 ADDOP(c, RETURN_VALUE);
2535 break; 2826 break;
2536 case Delete_kind: 2827 case Delete_kind:
2537 VISIT_SEQ(c, expr, s->v.Delete.targets) 2828 VISIT_SEQ(c, expr, s->v.Delete.targets)
2538 break; 2829 break;
2539 case Assign_kind: 2830 case Assign_kind:
2540 n = asdl_seq_LEN(s->v.Assign.targets); 2831 n = asdl_seq_LEN(s->v.Assign.targets);
2541 VISIT(c, expr, s->v.Assign.value); 2832 VISIT(c, expr, s->v.Assign.value);
2542 for (i = 0; i < n; i++) { 2833 for (i = 0; i < n; i++) {
2543 if (i < n - 1) 2834 if (i < n - 1)
2544 ADDOP(c, DUP_TOP); 2835 ADDOP(c, DUP_TOP);
2545 VISIT(c, expr, 2836 VISIT(c, expr,
2546 (expr_ty)asdl_seq_GET(s->v.Assign.targets, i)); 2837 (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
2547 } 2838 }
2548 break; 2839 break;
2549 case AugAssign_kind: 2840 case AugAssign_kind:
2550 return compiler_augassign(c, s); 2841 return compiler_augassign(c, s);
2842 case AnnAssign_kind:
2843 return compiler_annassign(c, s);
2551 case For_kind: 2844 case For_kind:
2552 return compiler_for(c, s); 2845 return compiler_for(c, s);
2553 case While_kind: 2846 case While_kind:
2554 return compiler_while(c, s); 2847 return compiler_while(c, s);
2555 case If_kind: 2848 case If_kind:
2556 return compiler_if(c, s); 2849 return compiler_if(c, s);
2557 case Raise_kind: 2850 case Raise_kind:
2558 n = 0; 2851 n = 0;
2559 if (s->v.Raise.exc) { 2852 if (s->v.Raise.exc) {
2560 VISIT(c, expr, s->v.Raise.exc); 2853 VISIT(c, expr, s->v.Raise.exc);
(...skipping 10 matching lines...) Expand all
2571 case Assert_kind: 2864 case Assert_kind:
2572 return compiler_assert(c, s); 2865 return compiler_assert(c, s);
2573 case Import_kind: 2866 case Import_kind:
2574 return compiler_import(c, s); 2867 return compiler_import(c, s);
2575 case ImportFrom_kind: 2868 case ImportFrom_kind:
2576 return compiler_from_import(c, s); 2869 return compiler_from_import(c, s);
2577 case Global_kind: 2870 case Global_kind:
2578 case Nonlocal_kind: 2871 case Nonlocal_kind:
2579 break; 2872 break;
2580 case Expr_kind: 2873 case Expr_kind:
2581 if (c->c_interactive && c->c_nestlevel <= 1) { 2874 return compiler_visit_stmt_expr(c, s->v.Expr.value);
2582 VISIT(c, expr, s->v.Expr.value);
2583 ADDOP(c, PRINT_EXPR);
2584 }
2585 else if (s->v.Expr.value->kind != Str_kind &&
2586 s->v.Expr.value->kind != Num_kind) {
2587 VISIT(c, expr, s->v.Expr.value);
2588 ADDOP(c, POP_TOP);
2589 }
2590 break;
2591 case Pass_kind: 2875 case Pass_kind:
2592 break; 2876 break;
2593 case Break_kind: 2877 case Break_kind:
2594 if (!compiler_in_loop(c)) 2878 if (!compiler_in_loop(c))
2595 return compiler_error(c, "'break' outside loop"); 2879 return compiler_error(c, "'break' outside loop");
2596 ADDOP(c, BREAK_LOOP); 2880 ADDOP(c, BREAK_LOOP);
2597 break; 2881 break;
2598 case Continue_kind: 2882 case Continue_kind:
2599 return compiler_continue(c); 2883 return compiler_continue(c);
2600 case With_kind: 2884 case With_kind:
2601 return compiler_with(c, s, 0); 2885 return compiler_with(c, s, 0);
2602 } 2886 case AsyncFunctionDef_kind:
2887 return compiler_function(c, s, 1);
2888 case AsyncWith_kind:
2889 return compiler_async_with(c, s, 0);
2890 case AsyncFor_kind:
2891 return compiler_async_for(c, s);
2892 }
2893
2603 return 1; 2894 return 1;
2604 } 2895 }
2605 2896
2606 static int 2897 static int
2607 unaryop(unaryop_ty op) 2898 unaryop(unaryop_ty op)
2608 { 2899 {
2609 switch (op) { 2900 switch (op) {
2610 case Invert: 2901 case Invert:
2611 return UNARY_INVERT; 2902 return UNARY_INVERT;
2612 case Not: 2903 case Not:
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after
2733 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype; 3024 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
2734 3025
2735 PyObject *dict = c->u->u_names; 3026 PyObject *dict = c->u->u_names;
2736 PyObject *mangled; 3027 PyObject *mangled;
2737 /* XXX AugStore isn't used anywhere! */ 3028 /* XXX AugStore isn't used anywhere! */
2738 3029
2739 mangled = _Py_Mangle(c->u->u_private, name); 3030 mangled = _Py_Mangle(c->u->u_private, name);
2740 if (!mangled) 3031 if (!mangled)
2741 return 0; 3032 return 0;
2742 3033
2743 assert(PyUnicode_CompareWithASCIIString(name, "None") && 3034 assert(!_PyUnicode_EqualToASCIIString(name, "None") &&
2744 PyUnicode_CompareWithASCIIString(name, "True") && 3035 !_PyUnicode_EqualToASCIIString(name, "True") &&
2745 PyUnicode_CompareWithASCIIString(name, "False")); 3036 !_PyUnicode_EqualToASCIIString(name, "False"));
2746 3037
2747 op = 0; 3038 op = 0;
2748 optype = OP_NAME; 3039 optype = OP_NAME;
2749 scope = PyST_GetScope(c->u->u_ste, mangled); 3040 scope = PyST_GetScope(c->u->u_ste, mangled);
2750 switch (scope) { 3041 switch (scope) {
2751 case FREE: 3042 case FREE:
2752 dict = c->u->u_freevars; 3043 dict = c->u->u_freevars;
2753 optype = OP_DEREF; 3044 optype = OP_DEREF;
2754 break; 3045 break;
2755 case CELL: 3046 case CELL:
2756 dict = c->u->u_cellvars; 3047 dict = c->u->u_cellvars;
2757 optype = OP_DEREF; 3048 optype = OP_DEREF;
2758 break; 3049 break;
2759 case LOCAL: 3050 case LOCAL:
2760 if (c->u->u_ste->ste_type == FunctionBlock) 3051 if (c->u->u_ste->ste_type == FunctionBlock)
2761 optype = OP_FAST; 3052 optype = OP_FAST;
2762 break; 3053 break;
2763 case GLOBAL_IMPLICIT: 3054 case GLOBAL_IMPLICIT:
2764 if (c->u->u_ste->ste_type == FunctionBlock && 3055 if (c->u->u_ste->ste_type == FunctionBlock)
2765 !c->u->u_ste->ste_unoptimized)
2766 optype = OP_GLOBAL; 3056 optype = OP_GLOBAL;
2767 break; 3057 break;
2768 case GLOBAL_EXPLICIT: 3058 case GLOBAL_EXPLICIT:
2769 optype = OP_GLOBAL; 3059 optype = OP_GLOBAL;
2770 break; 3060 break;
2771 default: 3061 default:
2772 /* scope can be 0 */ 3062 /* scope can be 0 */
2773 break; 3063 break;
2774 } 3064 }
2775 3065
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
2873 for (i = 0; i < n; ++i) { 3163 for (i = 0; i < n; ++i) {
2874 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i)); 3164 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i));
2875 ADDOP_JABS(c, jumpi, end); 3165 ADDOP_JABS(c, jumpi, end);
2876 } 3166 }
2877 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n)); 3167 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n));
2878 compiler_use_next_block(c, end); 3168 compiler_use_next_block(c, end);
2879 return 1; 3169 return 1;
2880 } 3170 }
2881 3171
2882 static int 3172 static int
3173 starunpack_helper(struct compiler *c, asdl_seq *elts,
3174 int single_op, int inner_op, int outer_op)
3175 {
3176 Py_ssize_t n = asdl_seq_LEN(elts);
3177 Py_ssize_t i, nsubitems = 0, nseen = 0;
3178 for (i = 0; i < n; i++) {
3179 expr_ty elt = asdl_seq_GET(elts, i);
3180 if (elt->kind == Starred_kind) {
3181 if (nseen) {
3182 ADDOP_I(c, inner_op, nseen);
3183 nseen = 0;
3184 nsubitems++;
3185 }
3186 VISIT(c, expr, elt->v.Starred.value);
3187 nsubitems++;
3188 }
3189 else {
3190 VISIT(c, expr, elt);
3191 nseen++;
3192 }
3193 }
3194 if (nsubitems) {
3195 if (nseen) {
3196 ADDOP_I(c, inner_op, nseen);
3197 nsubitems++;
3198 }
3199 ADDOP_I(c, outer_op, nsubitems);
3200 }
3201 else
3202 ADDOP_I(c, single_op, nseen);
3203 return 1;
3204 }
3205
3206 static int
3207 assignment_helper(struct compiler *c, asdl_seq *elts)
3208 {
3209 Py_ssize_t n = asdl_seq_LEN(elts);
3210 Py_ssize_t i;
3211 int seen_star = 0;
3212 for (i = 0; i < n; i++) {
3213 expr_ty elt = asdl_seq_GET(elts, i);
3214 if (elt->kind == Starred_kind && !seen_star) {
3215 if ((i >= (1 << 8)) ||
3216 (n-i-1 >= (INT_MAX >> 8)))
3217 return compiler_error(c,
3218 "too many expressions in "
3219 "star-unpacking assignment");
3220 ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
3221 seen_star = 1;
3222 asdl_seq_SET(elts, i, elt->v.Starred.value);
3223 }
3224 else if (elt->kind == Starred_kind) {
3225 return compiler_error(c,
3226 "two starred expressions in assignment");
3227 }
3228 }
3229 if (!seen_star) {
3230 ADDOP_I(c, UNPACK_SEQUENCE, n);
3231 }
3232 VISIT_SEQ(c, expr, elts);
3233 return 1;
3234 }
3235
3236 static int
2883 compiler_list(struct compiler *c, expr_ty e) 3237 compiler_list(struct compiler *c, expr_ty e)
2884 { 3238 {
2885 Py_ssize_t n = asdl_seq_LEN(e->v.List.elts); 3239 asdl_seq *elts = e->v.List.elts;
2886 if (e->v.List.ctx == Store) { 3240 if (e->v.List.ctx == Store) {
2887 int i, seen_star = 0; 3241 return assignment_helper(c, elts);
2888 for (i = 0; i < n; i++) { 3242 }
2889 expr_ty elt = asdl_seq_GET(e->v.List.elts, i); 3243 else if (e->v.List.ctx == Load) {
2890 if (elt->kind == Starred_kind && !seen_star) { 3244 return starunpack_helper(c, elts,
2891 if ((i >= (1 << 8)) || 3245 BUILD_LIST, BUILD_TUPLE, BUILD_LIST_UNPACK);
2892 (n-i-1 >= (INT_MAX >> 8))) 3246 }
2893 return compiler_error(c, 3247 else
2894 "too many expressions in " 3248 VISIT_SEQ(c, expr, elts);
2895 "star-unpacking assignment");
2896 ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
2897 seen_star = 1;
2898 asdl_seq_SET(e->v.List.elts, i, elt->v.Starred.value);
2899 } else if (elt->kind == Starred_kind) {
2900 return compiler_error(c,
2901 "two starred expressions in assignment");
2902 }
2903 }
2904 if (!seen_star) {
2905 ADDOP_I(c, UNPACK_SEQUENCE, n);
2906 }
2907 }
2908 VISIT_SEQ(c, expr, e->v.List.elts);
2909 if (e->v.List.ctx == Load) {
2910 ADDOP_I(c, BUILD_LIST, n);
2911 }
2912 return 1; 3249 return 1;
2913 } 3250 }
2914 3251
2915 static int 3252 static int
2916 compiler_tuple(struct compiler *c, expr_ty e) 3253 compiler_tuple(struct compiler *c, expr_ty e)
2917 { 3254 {
2918 Py_ssize_t n = asdl_seq_LEN(e->v.Tuple.elts); 3255 asdl_seq *elts = e->v.Tuple.elts;
2919 if (e->v.Tuple.ctx == Store) { 3256 if (e->v.Tuple.ctx == Store) {
2920 int i, seen_star = 0; 3257 return assignment_helper(c, elts);
2921 for (i = 0; i < n; i++) { 3258 }
2922 expr_ty elt = asdl_seq_GET(e->v.Tuple.elts, i); 3259 else if (e->v.Tuple.ctx == Load) {
2923 if (elt->kind == Starred_kind && !seen_star) { 3260 return starunpack_helper(c, elts,
2924 if ((i >= (1 << 8)) || 3261 BUILD_TUPLE, BUILD_TUPLE, BUILD_TUPLE_UNPACK);
2925 (n-i-1 >= (INT_MAX >> 8))) 3262 }
2926 return compiler_error(c, 3263 else
2927 "too many expressions in " 3264 VISIT_SEQ(c, expr, elts);
2928 "star-unpacking assignment"); 3265 return 1;
2929 ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8))); 3266 }
2930 seen_star = 1; 3267
2931 asdl_seq_SET(e->v.Tuple.elts, i, elt->v.Starred.value); 3268 static int
2932 } else if (elt->kind == Starred_kind) { 3269 compiler_set(struct compiler *c, expr_ty e)
2933 return compiler_error(c, 3270 {
2934 "two starred expressions in assignment"); 3271 return starunpack_helper(c, e->v.Set.elts, BUILD_SET,
2935 } 3272 BUILD_SET, BUILD_SET_UNPACK);
2936 } 3273 }
2937 if (!seen_star) { 3274
2938 ADDOP_I(c, UNPACK_SEQUENCE, n); 3275 static int
2939 } 3276 are_all_items_const(asdl_seq *seq, Py_ssize_t begin, Py_ssize_t end)
2940 } 3277 {
2941 VISIT_SEQ(c, expr, e->v.Tuple.elts); 3278 Py_ssize_t i;
2942 if (e->v.Tuple.ctx == Load) { 3279 for (i = begin; i < end; i++) {
2943 ADDOP_I(c, BUILD_TUPLE, n); 3280 expr_ty key = (expr_ty)asdl_seq_GET(seq, i);
3281 if (key == NULL || !is_const(key))
3282 return 0;
3283 }
3284 return 1;
3285 }
3286
3287 static int
3288 compiler_subdict(struct compiler *c, expr_ty e, Py_ssize_t begin, Py_ssize_t end )
3289 {
3290 Py_ssize_t i, n = end - begin;
3291 PyObject *keys, *key;
3292 if (n > 1 && are_all_items_const(e->v.Dict.keys, begin, end)) {
3293 for (i = begin; i < end; i++) {
3294 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
3295 }
3296 keys = PyTuple_New(n);
3297 if (keys == NULL) {
3298 return 0;
3299 }
3300 for (i = begin; i < end; i++) {
3301 key = get_const_value((expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
3302 Py_INCREF(key);
3303 PyTuple_SET_ITEM(keys, i - begin, key);
3304 }
3305 ADDOP_N(c, LOAD_CONST, keys, consts);
3306 ADDOP_I(c, BUILD_CONST_KEY_MAP, n);
3307 }
3308 else {
3309 for (i = begin; i < end; i++) {
3310 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
3311 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
3312 }
3313 ADDOP_I(c, BUILD_MAP, n);
3314 }
3315 return 1;
3316 }
3317
3318 static int
3319 compiler_dict(struct compiler *c, expr_ty e)
3320 {
3321 Py_ssize_t i, n, elements;
3322 int containers;
3323 int is_unpacking = 0;
3324 n = asdl_seq_LEN(e->v.Dict.values);
3325 containers = 0;
3326 elements = 0;
3327 for (i = 0; i < n; i++) {
3328 is_unpacking = (expr_ty)asdl_seq_GET(e->v.Dict.keys, i) == NULL;
3329 if (elements == 0xFFFF || (elements && is_unpacking)) {
3330 if (!compiler_subdict(c, e, i - elements, i))
3331 return 0;
3332 containers++;
3333 elements = 0;
3334 }
3335 if (is_unpacking) {
3336 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
3337 containers++;
3338 }
3339 else {
3340 elements++;
3341 }
3342 }
3343 if (elements || containers == 0) {
3344 if (!compiler_subdict(c, e, n - elements, n))
3345 return 0;
3346 containers++;
3347 }
3348 /* If there is more than one dict, they need to be merged into a new
3349 * dict. If there is one dict and it's an unpacking, then it needs
3350 * to be copied into a new dict." */
3351 while (containers > 1 || is_unpacking) {
3352 int oparg = containers < 255 ? containers : 255;
3353 ADDOP_I(c, BUILD_MAP_UNPACK, oparg);
3354 containers -= (oparg - 1);
3355 is_unpacking = 0;
2944 } 3356 }
2945 return 1; 3357 return 1;
2946 } 3358 }
2947 3359
2948 static int 3360 static int
2949 compiler_compare(struct compiler *c, expr_ty e) 3361 compiler_compare(struct compiler *c, expr_ty e)
2950 { 3362 {
2951 Py_ssize_t i, n; 3363 Py_ssize_t i, n;
2952 basicblock *cleanup = NULL; 3364 basicblock *cleanup = NULL;
2953 3365
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
2989 } 3401 }
2990 return 1; 3402 return 1;
2991 } 3403 }
2992 3404
2993 static int 3405 static int
2994 compiler_call(struct compiler *c, expr_ty e) 3406 compiler_call(struct compiler *c, expr_ty e)
2995 { 3407 {
2996 VISIT(c, expr, e->v.Call.func); 3408 VISIT(c, expr, e->v.Call.func);
2997 return compiler_call_helper(c, 0, 3409 return compiler_call_helper(c, 0,
2998 e->v.Call.args, 3410 e->v.Call.args,
2999 e->v.Call.keywords, 3411 e->v.Call.keywords);
3000 e->v.Call.starargs, 3412 }
3001 e->v.Call.kwargs); 3413
3414 static int
3415 compiler_joined_str(struct compiler *c, expr_ty e)
3416 {
3417 VISIT_SEQ(c, expr, e->v.JoinedStr.values);
3418 ADDOP_I(c, BUILD_STRING, asdl_seq_LEN(e->v.JoinedStr.values));
3419 return 1;
3420 }
3421
3422 /* Used to implement f-strings. Format a single value. */
3423 static int
3424 compiler_formatted_value(struct compiler *c, expr_ty e)
3425 {
3426 /* Our oparg encodes 2 pieces of information: the conversion
3427 character, and whether or not a format_spec was provided.
3428
3429 Convert the conversion char to 2 bits:
3430 None: 000 0x0 FVC_NONE
3431 !s : 001 0x1 FVC_STR
3432 !r : 010 0x2 FVC_REPR
3433 !a : 011 0x3 FVC_ASCII
3434
3435 next bit is whether or not we have a format spec:
3436 yes : 100 0x4
3437 no : 000 0x0
3438 */
3439
3440 int oparg;
3441
3442 /* Evaluate the expression to be formatted. */
3443 VISIT(c, expr, e->v.FormattedValue.value);
3444
3445 switch (e->v.FormattedValue.conversion) {
3446 case 's': oparg = FVC_STR; break;
3447 case 'r': oparg = FVC_REPR; break;
3448 case 'a': oparg = FVC_ASCII; break;
3449 case -1: oparg = FVC_NONE; break;
3450 default:
3451 PyErr_SetString(PyExc_SystemError,
3452 "Unrecognized conversion character");
3453 return 0;
3454 }
3455 if (e->v.FormattedValue.format_spec) {
3456 /* Evaluate the format spec, and update our opcode arg. */
3457 VISIT(c, expr, e->v.FormattedValue.format_spec);
3458 oparg |= FVS_HAVE_SPEC;
3459 }
3460
3461 /* And push our opcode and oparg */
3462 ADDOP_I(c, FORMAT_VALUE, oparg);
3463 return 1;
3464 }
3465
3466 static int
3467 compiler_subkwargs(struct compiler *c, asdl_seq *keywords, Py_ssize_t begin, Py_ ssize_t end)
3468 {
3469 Py_ssize_t i, n = end - begin;
3470 keyword_ty kw;
3471 PyObject *keys, *key;
3472 assert(n > 0);
3473 if (n > 1) {
3474 for (i = begin; i < end; i++) {
3475 kw = asdl_seq_GET(keywords, i);
3476 VISIT(c, expr, kw->value);
3477 }
3478 keys = PyTuple_New(n);
3479 if (keys == NULL) {
3480 return 0;
3481 }
3482 for (i = begin; i < end; i++) {
3483 key = ((keyword_ty) asdl_seq_GET(keywords, i))->arg;
3484 Py_INCREF(key);
3485 PyTuple_SET_ITEM(keys, i - begin, key);
3486 }
3487 ADDOP_N(c, LOAD_CONST, keys, consts);
3488 ADDOP_I(c, BUILD_CONST_KEY_MAP, n);
3489 }
3490 else {
3491 /* a for loop only executes once */
3492 for (i = begin; i < end; i++) {
3493 kw = asdl_seq_GET(keywords, i);
3494 ADDOP_O(c, LOAD_CONST, kw->arg, consts);
3495 VISIT(c, expr, kw->value);
3496 }
3497 ADDOP_I(c, BUILD_MAP, n);
3498 }
3499 return 1;
3002 } 3500 }
3003 3501
3004 /* shared code between compiler_call and compiler_class */ 3502 /* shared code between compiler_call and compiler_class */
3005 static int 3503 static int
3006 compiler_call_helper(struct compiler *c, 3504 compiler_call_helper(struct compiler *c,
3007 Py_ssize_t n, /* Args already pushed */ 3505 int n, /* Args already pushed */
3008 asdl_seq *args, 3506 asdl_seq *args,
3009 asdl_seq *keywords, 3507 asdl_seq *keywords)
3010 expr_ty starargs, 3508 {
3011 expr_ty kwargs) 3509 Py_ssize_t i, nseen, nelts, nkwelts;
3012 { 3510 int mustdictunpack = 0;
3013 int code = 0; 3511
3014 3512 /* the number of tuples and dictionaries on the stack */
3015 n += asdl_seq_LEN(args); 3513 Py_ssize_t nsubargs = 0, nsubkwargs = 0;
3016 VISIT_SEQ(c, expr, args); 3514
3017 if (keywords) { 3515 nelts = asdl_seq_LEN(args);
3516 nkwelts = asdl_seq_LEN(keywords);
3517
3518 for (i = 0; i < nkwelts; i++) {
3519 keyword_ty kw = asdl_seq_GET(keywords, i);
3520 if (kw->arg == NULL) {
3521 mustdictunpack = 1;
3522 break;
3523 }
3524 }
3525
3526 nseen = n; /* the number of positional arguments on the stack */
3527 for (i = 0; i < nelts; i++) {
3528 expr_ty elt = asdl_seq_GET(args, i);
3529 if (elt->kind == Starred_kind) {
3530 /* A star-arg. If we've seen positional arguments,
3531 pack the positional arguments into a tuple. */
3532 if (nseen) {
3533 ADDOP_I(c, BUILD_TUPLE, nseen);
3534 nseen = 0;
3535 nsubargs++;
3536 }
3537 VISIT(c, expr, elt->v.Starred.value);
3538 nsubargs++;
3539 }
3540 else {
3541 VISIT(c, expr, elt);
3542 nseen++;
3543 }
3544 }
3545
3546 /* Same dance again for keyword arguments */
3547 if (nsubargs || mustdictunpack) {
3548 if (nseen) {
3549 /* Pack up any trailing positional arguments. */
3550 ADDOP_I(c, BUILD_TUPLE, nseen);
3551 nsubargs++;
3552 }
3553 if (nsubargs > 1) {
3554 /* If we ended up with more than one stararg, we need
3555 to concatenate them into a single sequence. */
3556 ADDOP_I(c, BUILD_TUPLE_UNPACK_WITH_CALL, nsubargs);
3557 }
3558 else if (nsubargs == 0) {
3559 ADDOP_I(c, BUILD_TUPLE, 0);
3560 }
3561 nseen = 0; /* the number of keyword arguments on the stack following */
3562 for (i = 0; i < nkwelts; i++) {
3563 keyword_ty kw = asdl_seq_GET(keywords, i);
3564 if (kw->arg == NULL) {
3565 /* A keyword argument unpacking. */
3566 if (nseen) {
3567 if (!compiler_subkwargs(c, keywords, i - nseen, i))
3568 return 0;
3569 nsubkwargs++;
3570 nseen = 0;
3571 }
3572 VISIT(c, expr, kw->value);
3573 nsubkwargs++;
3574 }
3575 else {
3576 nseen++;
3577 }
3578 }
3579 if (nseen) {
3580 /* Pack up any trailing keyword arguments. */
3581 if (!compiler_subkwargs(c, keywords, nkwelts - nseen, nkwelts))
3582 return 0;
3583 nsubkwargs++;
3584 }
3585 if (nsubkwargs > 1) {
3586 /* Pack it all up */
3587 ADDOP_I(c, BUILD_MAP_UNPACK_WITH_CALL, nsubkwargs);
3588 }
3589 ADDOP_I(c, CALL_FUNCTION_EX, nsubkwargs > 0);
3590 return 1;
3591 }
3592 else if (nkwelts) {
3593 PyObject *names;
3018 VISIT_SEQ(c, keyword, keywords); 3594 VISIT_SEQ(c, keyword, keywords);
3019 n |= asdl_seq_LEN(keywords) << 8; 3595 names = PyTuple_New(nkwelts);
3020 } 3596 if (names == NULL) {
3021 if (starargs) { 3597 return 0;
3022 VISIT(c, expr, starargs); 3598 }
3023 code |= 1; 3599 for (i = 0; i < nkwelts; i++) {
3024 } 3600 keyword_ty kw = asdl_seq_GET(keywords, i);
3025 if (kwargs) { 3601 Py_INCREF(kw->arg);
3026 VISIT(c, expr, kwargs); 3602 PyTuple_SET_ITEM(names, i, kw->arg);
3027 code |= 2; 3603 }
3028 } 3604 ADDOP_N(c, LOAD_CONST, names, consts);
3029 switch (code) { 3605 ADDOP_I(c, CALL_FUNCTION_KW, n + nelts + nkwelts);
3030 case 0: 3606 return 1;
3031 ADDOP_I(c, CALL_FUNCTION, n); 3607 }
3032 break; 3608 else {
3033 case 1: 3609 ADDOP_I(c, CALL_FUNCTION, n + nelts);
3034 ADDOP_I(c, CALL_FUNCTION_VAR, n); 3610 return 1;
3035 break; 3611 }
3036 case 2:
3037 ADDOP_I(c, CALL_FUNCTION_KW, n);
3038 break;
3039 case 3:
3040 ADDOP_I(c, CALL_FUNCTION_VAR_KW, n);
3041 break;
3042 }
3043 return 1;
3044 } 3612 }
3045 3613
3046 3614
3047 /* List and set comprehensions and generator expressions work by creating a 3615 /* List and set comprehensions and generator expressions work by creating a
3048 nested function to perform the actual iteration. This means that the 3616 nested function to perform the actual iteration. This means that the
3049 iteration variables don't leak into the current scope. 3617 iteration variables don't leak into the current scope.
3050 The defined function is called immediately following its definition, with the 3618 The defined function is called immediately following its definition, with the
3051 result of that call being the result of the expression. 3619 result of that call being the result of the expression.
3052 The LC/SC version returns the populated container, while the GE version is 3620 The LC/SC version returns the populated container, while the GE version is
3053 flagged in symtable.c as a generator, so it returns the generator object 3621 flagged in symtable.c as a generator, so it returns the generator object
3054 when the function is called. 3622 when the function is called.
3055 This code *knows* that the loop cannot contain break, continue, or return, 3623 This code *knows* that the loop cannot contain break, continue, or return,
3056 so it cheats and skips the SETUP_LOOP/POP_BLOCK steps used in normal loops. 3624 so it cheats and skips the SETUP_LOOP/POP_BLOCK steps used in normal loops.
3057 3625
3058 Possible cleanups: 3626 Possible cleanups:
3059 - iterate over the generator sequence instead of using recursion 3627 - iterate over the generator sequence instead of using recursion
3060 */ 3628 */
3061 3629
3630
3062 static int 3631 static int
3063 compiler_comprehension_generator(struct compiler *c, 3632 compiler_comprehension_generator(struct compiler *c,
3064 asdl_seq *generators, int gen_index, 3633 asdl_seq *generators, int gen_index,
3065 expr_ty elt, expr_ty val, int type) 3634 expr_ty elt, expr_ty val, int type)
3635 {
3636 comprehension_ty gen;
3637 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
3638 if (gen->is_async) {
3639 return compiler_async_comprehension_generator(
3640 c, generators, gen_index, elt, val, type);
3641 } else {
3642 return compiler_sync_comprehension_generator(
3643 c, generators, gen_index, elt, val, type);
3644 }
3645 }
3646
3647 static int
3648 compiler_sync_comprehension_generator(struct compiler *c,
3649 asdl_seq *generators, int gen_index,
3650 expr_ty elt, expr_ty val, int type)
3066 { 3651 {
3067 /* generate code for the iterator, then each of the ifs, 3652 /* generate code for the iterator, then each of the ifs,
3068 and then write to the element */ 3653 and then write to the element */
3069 3654
3070 comprehension_ty gen; 3655 comprehension_ty gen;
3071 basicblock *start, *anchor, *skip, *if_cleanup; 3656 basicblock *start, *anchor, *skip, *if_cleanup;
3072 Py_ssize_t i, n; 3657 Py_ssize_t i, n;
3073 3658
3074 start = compiler_new_block(c); 3659 start = compiler_new_block(c);
3075 skip = compiler_new_block(c); 3660 skip = compiler_new_block(c);
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
3143 compiler_use_next_block(c, skip); 3728 compiler_use_next_block(c, skip);
3144 } 3729 }
3145 compiler_use_next_block(c, if_cleanup); 3730 compiler_use_next_block(c, if_cleanup);
3146 ADDOP_JABS(c, JUMP_ABSOLUTE, start); 3731 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
3147 compiler_use_next_block(c, anchor); 3732 compiler_use_next_block(c, anchor);
3148 3733
3149 return 1; 3734 return 1;
3150 } 3735 }
3151 3736
3152 static int 3737 static int
3153 compiler_comprehension(struct compiler *c, expr_ty e, int type, identifier name, 3738 compiler_async_comprehension_generator(struct compiler *c,
3154 asdl_seq *generators, expr_ty elt, expr_ty val) 3739 asdl_seq *generators, int gen_index,
3740 expr_ty elt, expr_ty val, int type)
3741 {
3742 _Py_IDENTIFIER(StopAsyncIteration);
3743
3744 comprehension_ty gen;
3745 basicblock *anchor, *skip, *if_cleanup, *try,
3746 *after_try, *except, *try_cleanup;
3747 Py_ssize_t i, n;
3748
3749 PyObject *stop_aiter_error = _PyUnicode_FromId(&PyId_StopAsyncIteration);
3750 if (stop_aiter_error == NULL) {
3751 return 0;
3752 }
3753
3754 try = compiler_new_block(c);
3755 after_try = compiler_new_block(c);
3756 try_cleanup = compiler_new_block(c);
3757 except = compiler_new_block(c);
3758 skip = compiler_new_block(c);
3759 if_cleanup = compiler_new_block(c);
3760 anchor = compiler_new_block(c);
3761
3762 if (skip == NULL || if_cleanup == NULL || anchor == NULL ||
3763 try == NULL || after_try == NULL ||
3764 except == NULL || after_try == NULL) {
3765 return 0;
3766 }
3767
3768 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
3769
3770 if (gen_index == 0) {
3771 /* Receive outermost iter as an implicit argument */
3772 c->u->u_argcount = 1;
3773 ADDOP_I(c, LOAD_FAST, 0);
3774 }
3775 else {
3776 /* Sub-iter - calculate on the fly */
3777 VISIT(c, expr, gen->iter);
3778 ADDOP(c, GET_AITER);
3779 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3780 ADDOP(c, YIELD_FROM);
3781 }
3782
3783 compiler_use_next_block(c, try);
3784
3785
3786 ADDOP_JREL(c, SETUP_EXCEPT, except);
3787 if (!compiler_push_fblock(c, EXCEPT, try))
3788 return 0;
3789
3790 ADDOP(c, GET_ANEXT);
3791 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3792 ADDOP(c, YIELD_FROM);
3793 VISIT(c, expr, gen->target);
3794 ADDOP(c, POP_BLOCK);
3795 compiler_pop_fblock(c, EXCEPT, try);
3796 ADDOP_JREL(c, JUMP_FORWARD, after_try);
3797
3798
3799 compiler_use_next_block(c, except);
3800 ADDOP(c, DUP_TOP);
3801 ADDOP_O(c, LOAD_GLOBAL, stop_aiter_error, names);
3802 ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH);
3803 ADDOP_JABS(c, POP_JUMP_IF_FALSE, try_cleanup);
3804
3805 ADDOP(c, POP_TOP);
3806 ADDOP(c, POP_TOP);
3807 ADDOP(c, POP_TOP);
3808 ADDOP(c, POP_EXCEPT); /* for SETUP_EXCEPT */
3809 ADDOP_JABS(c, JUMP_ABSOLUTE, anchor);
3810
3811
3812 compiler_use_next_block(c, try_cleanup);
3813 ADDOP(c, END_FINALLY);
3814
3815 compiler_use_next_block(c, after_try);
3816
3817 n = asdl_seq_LEN(gen->ifs);
3818 for (i = 0; i < n; i++) {
3819 expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
3820 VISIT(c, expr, e);
3821 ADDOP_JABS(c, POP_JUMP_IF_FALSE, if_cleanup);
3822 NEXT_BLOCK(c);
3823 }
3824
3825 if (++gen_index < asdl_seq_LEN(generators))
3826 if (!compiler_comprehension_generator(c,
3827 generators, gen_index,
3828 elt, val, type))
3829 return 0;
3830
3831 /* only append after the last for generator */
3832 if (gen_index >= asdl_seq_LEN(generators)) {
3833 /* comprehension specific code */
3834 switch (type) {
3835 case COMP_GENEXP:
3836 VISIT(c, expr, elt);
3837 ADDOP(c, YIELD_VALUE);
3838 ADDOP(c, POP_TOP);
3839 break;
3840 case COMP_LISTCOMP:
3841 VISIT(c, expr, elt);
3842 ADDOP_I(c, LIST_APPEND, gen_index + 1);
3843 break;
3844 case COMP_SETCOMP:
3845 VISIT(c, expr, elt);
3846 ADDOP_I(c, SET_ADD, gen_index + 1);
3847 break;
3848 case COMP_DICTCOMP:
3849 /* With 'd[k] = v', v is evaluated before k, so we do
3850 the same. */
3851 VISIT(c, expr, val);
3852 VISIT(c, expr, elt);
3853 ADDOP_I(c, MAP_ADD, gen_index + 1);
3854 break;
3855 default:
3856 return 0;
3857 }
3858
3859 compiler_use_next_block(c, skip);
3860 }
3861 compiler_use_next_block(c, if_cleanup);
3862 ADDOP_JABS(c, JUMP_ABSOLUTE, try);
3863 compiler_use_next_block(c, anchor);
3864 ADDOP(c, POP_TOP);
3865
3866 return 1;
3867 }
3868
3869 static int
3870 compiler_comprehension(struct compiler *c, expr_ty e, int type,
3871 identifier name, asdl_seq *generators, expr_ty elt,
3872 expr_ty val)
3155 { 3873 {
3156 PyCodeObject *co = NULL; 3874 PyCodeObject *co = NULL;
3157 expr_ty outermost_iter; 3875 comprehension_ty outermost;
3158 PyObject *qualname = NULL; 3876 PyObject *qualname = NULL;
3159 3877 int is_async_function = c->u->u_ste->ste_coroutine;
3160 outermost_iter = ((comprehension_ty) 3878 int is_async_generator = 0;
3161 asdl_seq_GET(generators, 0))->iter; 3879
3880 outermost = (comprehension_ty) asdl_seq_GET(generators, 0);
3162 3881
3163 if (!compiler_enter_scope(c, name, COMPILER_SCOPE_COMPREHENSION, 3882 if (!compiler_enter_scope(c, name, COMPILER_SCOPE_COMPREHENSION,
3164 (void *)e, e->lineno)) 3883 (void *)e, e->lineno))
3884 {
3165 goto error; 3885 goto error;
3886 }
3887
3888 is_async_generator = c->u->u_ste->ste_coroutine;
3889
3890 if (is_async_generator && !is_async_function) {
3891 if (e->lineno > c->u->u_lineno) {
3892 c->u->u_lineno = e->lineno;
3893 c->u->u_lineno_set = 0;
3894 }
3895 compiler_error(c, "asynchronous comprehension outside of "
3896 "an asynchronous function");
3897 goto error_in_scope;
3898 }
3166 3899
3167 if (type != COMP_GENEXP) { 3900 if (type != COMP_GENEXP) {
3168 int op; 3901 int op;
3169 switch (type) { 3902 switch (type) {
3170 case COMP_LISTCOMP: 3903 case COMP_LISTCOMP:
3171 op = BUILD_LIST; 3904 op = BUILD_LIST;
3172 break; 3905 break;
3173 case COMP_SETCOMP: 3906 case COMP_SETCOMP:
3174 op = BUILD_SET; 3907 op = BUILD_SET;
3175 break; 3908 break;
(...skipping 22 matching lines...) Expand all
3198 Py_INCREF(qualname); 3931 Py_INCREF(qualname);
3199 compiler_exit_scope(c); 3932 compiler_exit_scope(c);
3200 if (co == NULL) 3933 if (co == NULL)
3201 goto error; 3934 goto error;
3202 3935
3203 if (!compiler_make_closure(c, co, 0, qualname)) 3936 if (!compiler_make_closure(c, co, 0, qualname))
3204 goto error; 3937 goto error;
3205 Py_DECREF(qualname); 3938 Py_DECREF(qualname);
3206 Py_DECREF(co); 3939 Py_DECREF(co);
3207 3940
3208 VISIT(c, expr, outermost_iter); 3941 VISIT(c, expr, outermost->iter);
3209 ADDOP(c, GET_ITER); 3942
3943 if (outermost->is_async) {
3944 ADDOP(c, GET_AITER);
3945 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3946 ADDOP(c, YIELD_FROM);
3947 } else {
3948 ADDOP(c, GET_ITER);
3949 }
3950
3210 ADDOP_I(c, CALL_FUNCTION, 1); 3951 ADDOP_I(c, CALL_FUNCTION, 1);
3952
3953 if (is_async_generator && type != COMP_GENEXP) {
3954 ADDOP(c, GET_AWAITABLE);
3955 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3956 ADDOP(c, YIELD_FROM);
3957 }
3958
3211 return 1; 3959 return 1;
3212 error_in_scope: 3960 error_in_scope:
3213 compiler_exit_scope(c); 3961 compiler_exit_scope(c);
3214 error: 3962 error:
3215 Py_XDECREF(qualname); 3963 Py_XDECREF(qualname);
3216 Py_XDECREF(co); 3964 Py_XDECREF(co);
3217 return 0; 3965 return 0;
3218 } 3966 }
3219 3967
3220 static int 3968 static int
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
3275 assert(e->kind == DictComp_kind); 4023 assert(e->kind == DictComp_kind);
3276 return compiler_comprehension(c, e, COMP_DICTCOMP, name, 4024 return compiler_comprehension(c, e, COMP_DICTCOMP, name,
3277 e->v.DictComp.generators, 4025 e->v.DictComp.generators,
3278 e->v.DictComp.key, e->v.DictComp.value); 4026 e->v.DictComp.key, e->v.DictComp.value);
3279 } 4027 }
3280 4028
3281 4029
3282 static int 4030 static int
3283 compiler_visit_keyword(struct compiler *c, keyword_ty k) 4031 compiler_visit_keyword(struct compiler *c, keyword_ty k)
3284 { 4032 {
3285 ADDOP_O(c, LOAD_CONST, k->arg, consts);
3286 VISIT(c, expr, k->value); 4033 VISIT(c, expr, k->value);
3287 return 1; 4034 return 1;
3288 } 4035 }
3289 4036
3290 /* Test whether expression is constant. For constants, report 4037 /* Test whether expression is constant. For constants, report
3291 whether they are true or false. 4038 whether they are true or false.
3292 4039
3293 Return values: 1 for true, 0 for false, -1 for non-constant. 4040 Return values: 1 for true, 0 for false, -1 for non-constant.
3294 */ 4041 */
3295 4042
3296 static int 4043 static int
3297 expr_constant(struct compiler *c, expr_ty e) 4044 expr_constant(struct compiler *c, expr_ty e)
3298 { 4045 {
3299 char *id; 4046 char *id;
3300 switch (e->kind) { 4047 switch (e->kind) {
3301 case Ellipsis_kind: 4048 case Ellipsis_kind:
3302 return 1; 4049 return 1;
4050 case Constant_kind:
4051 return PyObject_IsTrue(e->v.Constant.value);
3303 case Num_kind: 4052 case Num_kind:
3304 return PyObject_IsTrue(e->v.Num.n); 4053 return PyObject_IsTrue(e->v.Num.n);
3305 case Str_kind: 4054 case Str_kind:
3306 return PyObject_IsTrue(e->v.Str.s); 4055 return PyObject_IsTrue(e->v.Str.s);
3307 case Name_kind: 4056 case Name_kind:
3308 /* optimize away names that can't be reassigned */ 4057 /* optimize away names that can't be reassigned */
3309 id = PyUnicode_AsUTF8(e->v.Name.id); 4058 id = PyUnicode_AsUTF8(e->v.Name.id);
3310 if (id && strcmp(id, "__debug__") == 0) 4059 if (id && strcmp(id, "__debug__") == 0)
3311 return !c->c_optimize; 4060 return !c->c_optimize;
3312 return -1; 4061 return -1;
3313 case NameConstant_kind: { 4062 case NameConstant_kind: {
3314 PyObject *o = e->v.NameConstant.value; 4063 PyObject *o = e->v.NameConstant.value;
3315 if (o == Py_None) 4064 if (o == Py_None)
3316 return 0; 4065 return 0;
3317 else if (o == Py_True) 4066 else if (o == Py_True)
3318 return 1; 4067 return 1;
3319 else if (o == Py_False) 4068 else if (o == Py_False)
3320 return 0; 4069 return 0;
3321 } 4070 }
3322 default: 4071 default:
3323 return -1; 4072 return -1;
3324 } 4073 }
3325 } 4074 }
3326 4075
4076
4077 /*
4078 Implements the async with statement.
4079
4080 The semantics outlined in that PEP are as follows:
4081
4082 async with EXPR as VAR:
4083 BLOCK
4084
4085 It is implemented roughly as:
4086
4087 context = EXPR
4088 exit = context.__aexit__ # not calling it
4089 value = await context.__aenter__()
4090 try:
4091 VAR = value # if VAR present in the syntax
4092 BLOCK
4093 finally:
4094 if an exception was raised:
4095 exc = copy of (exception, instance, traceback)
4096 else:
4097 exc = (None, None, None)
4098 if not (await exit(*exc)):
4099 raise
4100 */
4101 static int
4102 compiler_async_with(struct compiler *c, stmt_ty s, int pos)
4103 {
4104 basicblock *block, *finally;
4105 withitem_ty item = asdl_seq_GET(s->v.AsyncWith.items, pos);
4106
4107 assert(s->kind == AsyncWith_kind);
4108
4109 block = compiler_new_block(c);
4110 finally = compiler_new_block(c);
4111 if (!block || !finally)
4112 return 0;
4113
4114 /* Evaluate EXPR */
4115 VISIT(c, expr, item->context_expr);
4116
4117 ADDOP(c, BEFORE_ASYNC_WITH);
4118 ADDOP(c, GET_AWAITABLE);
4119 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4120 ADDOP(c, YIELD_FROM);
4121
4122 ADDOP_JREL(c, SETUP_ASYNC_WITH, finally);
4123
4124 /* SETUP_ASYNC_WITH pushes a finally block. */
4125 compiler_use_next_block(c, block);
4126 if (!compiler_push_fblock(c, FINALLY_TRY, block)) {
4127 return 0;
4128 }
4129
4130 if (item->optional_vars) {
4131 VISIT(c, expr, item->optional_vars);
4132 }
4133 else {
4134 /* Discard result from context.__aenter__() */
4135 ADDOP(c, POP_TOP);
4136 }
4137
4138 pos++;
4139 if (pos == asdl_seq_LEN(s->v.AsyncWith.items))
4140 /* BLOCK code */
4141 VISIT_SEQ(c, stmt, s->v.AsyncWith.body)
4142 else if (!compiler_async_with(c, s, pos))
4143 return 0;
4144
4145 /* End of try block; start the finally block */
4146 ADDOP(c, POP_BLOCK);
4147 compiler_pop_fblock(c, FINALLY_TRY, block);
4148
4149 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4150 compiler_use_next_block(c, finally);
4151 if (!compiler_push_fblock(c, FINALLY_END, finally))
4152 return 0;
4153
4154 /* Finally block starts; context.__exit__ is on the stack under
4155 the exception or return information. Just issue our magic
4156 opcode. */
4157 ADDOP(c, WITH_CLEANUP_START);
4158
4159 ADDOP(c, GET_AWAITABLE);
4160 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4161 ADDOP(c, YIELD_FROM);
4162
4163 ADDOP(c, WITH_CLEANUP_FINISH);
4164
4165 /* Finally block ends. */
4166 ADDOP(c, END_FINALLY);
4167 compiler_pop_fblock(c, FINALLY_END, finally);
4168 return 1;
4169 }
4170
4171
3327 /* 4172 /*
3328 Implements the with statement from PEP 343. 4173 Implements the with statement from PEP 343.
3329 4174
3330 The semantics outlined in that PEP are as follows: 4175 The semantics outlined in that PEP are as follows:
3331 4176
3332 with EXPR as VAR: 4177 with EXPR as VAR:
3333 BLOCK 4178 BLOCK
3334 4179
3335 It is implemented roughly as: 4180 It is implemented roughly as:
3336 4181
3337 context = EXPR 4182 context = EXPR
3338 exit = context.__exit__ # not calling it 4183 exit = context.__exit__ # not calling it
3339 value = context.__enter__() 4184 value = context.__enter__()
3340 try: 4185 try:
3341 VAR = value # if VAR present in the syntax 4186 VAR = value # if VAR present in the syntax
3342 BLOCK 4187 BLOCK
3343 finally: 4188 finally:
3344 if an exception was raised: 4189 if an exception was raised:
3345 exc = copy of (exception, instance, traceback) 4190 exc = copy of (exception, instance, traceback)
3346 else: 4191 else:
3347 exc = (None, None, None) 4192 exc = (None, None, None)
3348 exit(*exc) 4193 exit(*exc)
3349 */ 4194 */
3350 static int 4195 static int
3351 compiler_with(struct compiler *c, stmt_ty s, int pos) 4196 compiler_with(struct compiler *c, stmt_ty s, int pos)
3352 { 4197 {
3353 basicblock *block, *finally; 4198 basicblock *block, *finally;
3354 withitem_ty item = asdl_seq_GET(s->v.With.items, pos); 4199 withitem_ty item = asdl_seq_GET(s->v.With.items, pos);
3355 4200
3356 assert(s->kind == With_kind); 4201 assert(s->kind == With_kind);
3357 4202
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
3390 compiler_pop_fblock(c, FINALLY_TRY, block); 4235 compiler_pop_fblock(c, FINALLY_TRY, block);
3391 4236
3392 ADDOP_O(c, LOAD_CONST, Py_None, consts); 4237 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3393 compiler_use_next_block(c, finally); 4238 compiler_use_next_block(c, finally);
3394 if (!compiler_push_fblock(c, FINALLY_END, finally)) 4239 if (!compiler_push_fblock(c, FINALLY_END, finally))
3395 return 0; 4240 return 0;
3396 4241
3397 /* Finally block starts; context.__exit__ is on the stack under 4242 /* Finally block starts; context.__exit__ is on the stack under
3398 the exception or return information. Just issue our magic 4243 the exception or return information. Just issue our magic
3399 opcode. */ 4244 opcode. */
3400 ADDOP(c, WITH_CLEANUP); 4245 ADDOP(c, WITH_CLEANUP_START);
4246 ADDOP(c, WITH_CLEANUP_FINISH);
3401 4247
3402 /* Finally block ends. */ 4248 /* Finally block ends. */
3403 ADDOP(c, END_FINALLY); 4249 ADDOP(c, END_FINALLY);
3404 compiler_pop_fblock(c, FINALLY_END, finally); 4250 compiler_pop_fblock(c, FINALLY_END, finally);
3405 return 1; 4251 return 1;
3406 } 4252 }
3407 4253
3408 static int 4254 static int
3409 compiler_visit_expr(struct compiler *c, expr_ty e) 4255 compiler_visit_expr(struct compiler *c, expr_ty e)
3410 { 4256 {
3411 Py_ssize_t i, n;
3412
3413 /* If expr e has a different line number than the last expr/stmt, 4257 /* If expr e has a different line number than the last expr/stmt,
3414 set a new line number for the next instruction. 4258 set a new line number for the next instruction.
3415 */ 4259 */
3416 if (e->lineno > c->u->u_lineno) { 4260 if (e->lineno > c->u->u_lineno) {
3417 c->u->u_lineno = e->lineno; 4261 c->u->u_lineno = e->lineno;
3418 c->u->u_lineno_set = 0; 4262 c->u->u_lineno_set = 0;
3419 } 4263 }
3420 /* Updating the column offset is always harmless. */ 4264 /* Updating the column offset is always harmless. */
3421 c->u->u_col_offset = e->col_offset; 4265 c->u->u_col_offset = e->col_offset;
3422 switch (e->kind) { 4266 switch (e->kind) {
3423 case BoolOp_kind: 4267 case BoolOp_kind:
3424 return compiler_boolop(c, e); 4268 return compiler_boolop(c, e);
3425 case BinOp_kind: 4269 case BinOp_kind:
3426 VISIT(c, expr, e->v.BinOp.left); 4270 VISIT(c, expr, e->v.BinOp.left);
3427 VISIT(c, expr, e->v.BinOp.right); 4271 VISIT(c, expr, e->v.BinOp.right);
3428 ADDOP(c, binop(c, e->v.BinOp.op)); 4272 ADDOP(c, binop(c, e->v.BinOp.op));
3429 break; 4273 break;
3430 case UnaryOp_kind: 4274 case UnaryOp_kind:
3431 VISIT(c, expr, e->v.UnaryOp.operand); 4275 VISIT(c, expr, e->v.UnaryOp.operand);
3432 ADDOP(c, unaryop(e->v.UnaryOp.op)); 4276 ADDOP(c, unaryop(e->v.UnaryOp.op));
3433 break; 4277 break;
3434 case Lambda_kind: 4278 case Lambda_kind:
3435 return compiler_lambda(c, e); 4279 return compiler_lambda(c, e);
3436 case IfExp_kind: 4280 case IfExp_kind:
3437 return compiler_ifexp(c, e); 4281 return compiler_ifexp(c, e);
3438 case Dict_kind: 4282 case Dict_kind:
3439 n = asdl_seq_LEN(e->v.Dict.values); 4283 return compiler_dict(c, e);
3440 /* BUILD_MAP parameter is only used to preallocate the dictionary,
3441 it doesn't need to be exact */
3442 ADDOP_I(c, BUILD_MAP, (n>0xFFFF ? 0xFFFF : n));
3443 for (i = 0; i < n; i++) {
3444 VISIT(c, expr,
3445 (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
3446 VISIT(c, expr,
3447 (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
3448 ADDOP(c, STORE_MAP);
3449 }
3450 break;
3451 case Set_kind: 4284 case Set_kind:
3452 n = asdl_seq_LEN(e->v.Set.elts); 4285 return compiler_set(c, e);
3453 VISIT_SEQ(c, expr, e->v.Set.elts);
3454 ADDOP_I(c, BUILD_SET, n);
3455 break;
3456 case GeneratorExp_kind: 4286 case GeneratorExp_kind:
3457 return compiler_genexp(c, e); 4287 return compiler_genexp(c, e);
3458 case ListComp_kind: 4288 case ListComp_kind:
3459 return compiler_listcomp(c, e); 4289 return compiler_listcomp(c, e);
3460 case SetComp_kind: 4290 case SetComp_kind:
3461 return compiler_setcomp(c, e); 4291 return compiler_setcomp(c, e);
3462 case DictComp_kind: 4292 case DictComp_kind:
3463 return compiler_dictcomp(c, e); 4293 return compiler_dictcomp(c, e);
3464 case Yield_kind: 4294 case Yield_kind:
3465 if (c->u->u_ste->ste_type != FunctionBlock) 4295 if (c->u->u_ste->ste_type != FunctionBlock)
3466 return compiler_error(c, "'yield' outside function"); 4296 return compiler_error(c, "'yield' outside function");
3467 if (e->v.Yield.value) { 4297 if (e->v.Yield.value) {
3468 VISIT(c, expr, e->v.Yield.value); 4298 VISIT(c, expr, e->v.Yield.value);
3469 } 4299 }
3470 else { 4300 else {
3471 ADDOP_O(c, LOAD_CONST, Py_None, consts); 4301 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3472 } 4302 }
3473 ADDOP(c, YIELD_VALUE); 4303 ADDOP(c, YIELD_VALUE);
3474 break; 4304 break;
3475 case YieldFrom_kind: 4305 case YieldFrom_kind:
3476 if (c->u->u_ste->ste_type != FunctionBlock) 4306 if (c->u->u_ste->ste_type != FunctionBlock)
3477 return compiler_error(c, "'yield' outside function"); 4307 return compiler_error(c, "'yield' outside function");
4308
4309 if (c->u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION)
4310 return compiler_error(c, "'yield from' inside async function");
4311
3478 VISIT(c, expr, e->v.YieldFrom.value); 4312 VISIT(c, expr, e->v.YieldFrom.value);
3479 ADDOP(c, GET_ITER); 4313 ADDOP(c, GET_YIELD_FROM_ITER);
4314 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4315 ADDOP(c, YIELD_FROM);
4316 break;
4317 case Await_kind:
4318 if (c->u->u_ste->ste_type != FunctionBlock)
4319 return compiler_error(c, "'await' outside function");
4320
4321 if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION &&
4322 c->u->u_scope_type != COMPILER_SCOPE_COMPREHENSION)
4323 return compiler_error(c, "'await' outside async function");
4324
4325 VISIT(c, expr, e->v.Await.value);
4326 ADDOP(c, GET_AWAITABLE);
3480 ADDOP_O(c, LOAD_CONST, Py_None, consts); 4327 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3481 ADDOP(c, YIELD_FROM); 4328 ADDOP(c, YIELD_FROM);
3482 break; 4329 break;
3483 case Compare_kind: 4330 case Compare_kind:
3484 return compiler_compare(c, e); 4331 return compiler_compare(c, e);
3485 case Call_kind: 4332 case Call_kind:
3486 return compiler_call(c, e); 4333 return compiler_call(c, e);
4334 case Constant_kind:
4335 ADDOP_O(c, LOAD_CONST, e->v.Constant.value, consts);
4336 break;
3487 case Num_kind: 4337 case Num_kind:
3488 ADDOP_O(c, LOAD_CONST, e->v.Num.n, consts); 4338 ADDOP_O(c, LOAD_CONST, e->v.Num.n, consts);
3489 break; 4339 break;
3490 case Str_kind: 4340 case Str_kind:
3491 ADDOP_O(c, LOAD_CONST, e->v.Str.s, consts); 4341 ADDOP_O(c, LOAD_CONST, e->v.Str.s, consts);
3492 break; 4342 break;
4343 case JoinedStr_kind:
4344 return compiler_joined_str(c, e);
4345 case FormattedValue_kind:
4346 return compiler_formatted_value(c, e);
3493 case Bytes_kind: 4347 case Bytes_kind:
3494 ADDOP_O(c, LOAD_CONST, e->v.Bytes.s, consts); 4348 ADDOP_O(c, LOAD_CONST, e->v.Bytes.s, consts);
3495 break; 4349 break;
3496 case Ellipsis_kind: 4350 case Ellipsis_kind:
3497 ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts); 4351 ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
3498 break; 4352 break;
3499 case NameConstant_kind: 4353 case NameConstant_kind:
3500 ADDOP_O(c, LOAD_CONST, e->v.NameConstant.value, consts); 4354 ADDOP_O(c, LOAD_CONST, e->v.NameConstant.value, consts);
3501 break; 4355 break;
3502 /* The following exprs can be assignment targets. */ 4356 /* The following exprs can be assignment targets. */
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
3556 break; 4410 break;
3557 case Starred_kind: 4411 case Starred_kind:
3558 switch (e->v.Starred.ctx) { 4412 switch (e->v.Starred.ctx) {
3559 case Store: 4413 case Store:
3560 /* In all legitimate cases, the Starred node was already replaced 4414 /* In all legitimate cases, the Starred node was already replaced
3561 * by compiler_list/compiler_tuple. XXX: is that okay? */ 4415 * by compiler_list/compiler_tuple. XXX: is that okay? */
3562 return compiler_error(c, 4416 return compiler_error(c,
3563 "starred assignment target must be in a list or tuple"); 4417 "starred assignment target must be in a list or tuple");
3564 default: 4418 default:
3565 return compiler_error(c, 4419 return compiler_error(c,
3566 "can use starred expression only as assignment target"); 4420 "can't use starred expression here");
3567 } 4421 }
3568 break; 4422 break;
3569 case Name_kind: 4423 case Name_kind:
3570 return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx); 4424 return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);
3571 /* child nodes of List and Tuple will have expr_context set */ 4425 /* child nodes of List and Tuple will have expr_context set */
3572 case List_kind: 4426 case List_kind:
3573 return compiler_list(c, e); 4427 return compiler_list(c, e);
3574 case Tuple_kind: 4428 case Tuple_kind:
3575 return compiler_tuple(c, e); 4429 return compiler_tuple(c, e);
3576 } 4430 }
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
3617 default: 4471 default:
3618 PyErr_Format(PyExc_SystemError, 4472 PyErr_Format(PyExc_SystemError,
3619 "invalid node type (%d) for augmented assignment", 4473 "invalid node type (%d) for augmented assignment",
3620 e->kind); 4474 e->kind);
3621 return 0; 4475 return 0;
3622 } 4476 }
3623 return 1; 4477 return 1;
3624 } 4478 }
3625 4479
3626 static int 4480 static int
4481 check_ann_expr(struct compiler *c, expr_ty e)
4482 {
4483 VISIT(c, expr, e);
4484 ADDOP(c, POP_TOP);
4485 return 1;
4486 }
4487
4488 static int
4489 check_annotation(struct compiler *c, stmt_ty s)
4490 {
4491 /* Annotations are only evaluated in a module or class. */
4492 if (c->u->u_scope_type == COMPILER_SCOPE_MODULE ||
4493 c->u->u_scope_type == COMPILER_SCOPE_CLASS) {
4494 return check_ann_expr(c, s->v.AnnAssign.annotation);
4495 }
4496 return 1;
4497 }
4498
4499 static int
4500 check_ann_slice(struct compiler *c, slice_ty sl)
4501 {
4502 switch(sl->kind) {
4503 case Index_kind:
4504 return check_ann_expr(c, sl->v.Index.value);
4505 case Slice_kind:
4506 if (sl->v.Slice.lower && !check_ann_expr(c, sl->v.Slice.lower)) {
4507 return 0;
4508 }
4509 if (sl->v.Slice.upper && !check_ann_expr(c, sl->v.Slice.upper)) {
4510 return 0;
4511 }
4512 if (sl->v.Slice.step && !check_ann_expr(c, sl->v.Slice.step)) {
4513 return 0;
4514 }
4515 break;
4516 default:
4517 PyErr_SetString(PyExc_SystemError,
4518 "unexpected slice kind");
4519 return 0;
4520 }
4521 return 1;
4522 }
4523
4524 static int
4525 check_ann_subscr(struct compiler *c, slice_ty sl)
4526 {
4527 /* We check that everything in a subscript is defined at runtime. */
4528 Py_ssize_t i, n;
4529
4530 switch (sl->kind) {
4531 case Index_kind:
4532 case Slice_kind:
4533 if (!check_ann_slice(c, sl)) {
4534 return 0;
4535 }
4536 break;
4537 case ExtSlice_kind:
4538 n = asdl_seq_LEN(sl->v.ExtSlice.dims);
4539 for (i = 0; i < n; i++) {
4540 slice_ty subsl = (slice_ty)asdl_seq_GET(sl->v.ExtSlice.dims, i);
4541 switch (subsl->kind) {
4542 case Index_kind:
4543 case Slice_kind:
4544 if (!check_ann_slice(c, subsl)) {
4545 return 0;
4546 }
4547 break;
4548 case ExtSlice_kind:
4549 default:
4550 PyErr_SetString(PyExc_SystemError,
4551 "extended slice invalid in nested slice");
4552 return 0;
4553 }
4554 }
4555 break;
4556 default:
4557 PyErr_Format(PyExc_SystemError,
4558 "invalid subscript kind %d", sl->kind);
4559 return 0;
4560 }
4561 return 1;
4562 }
4563
4564 static int
4565 compiler_annassign(struct compiler *c, stmt_ty s)
4566 {
4567 expr_ty targ = s->v.AnnAssign.target;
4568 PyObject* mangled;
4569
4570 assert(s->kind == AnnAssign_kind);
4571
4572 /* We perform the actual assignment first. */
4573 if (s->v.AnnAssign.value) {
4574 VISIT(c, expr, s->v.AnnAssign.value);
4575 VISIT(c, expr, targ);
4576 }
4577 switch (targ->kind) {
4578 case Name_kind:
4579 /* If we have a simple name in a module or class, store annotation. */
4580 if (s->v.AnnAssign.simple &&
4581 (c->u->u_scope_type == COMPILER_SCOPE_MODULE ||
4582 c->u->u_scope_type == COMPILER_SCOPE_CLASS)) {
4583 mangled = _Py_Mangle(c->u->u_private, targ->v.Name.id);
4584 if (!mangled) {
4585 return 0;
4586 }
4587 VISIT(c, expr, s->v.AnnAssign.annotation);
4588 /* ADDOP_N decrefs its argument */
4589 ADDOP_N(c, STORE_ANNOTATION, mangled, names);
4590 }
4591 break;
4592 case Attribute_kind:
4593 if (!s->v.AnnAssign.value &&
4594 !check_ann_expr(c, targ->v.Attribute.value)) {
4595 return 0;
4596 }
4597 break;
4598 case Subscript_kind:
4599 if (!s->v.AnnAssign.value &&
4600 (!check_ann_expr(c, targ->v.Subscript.value) ||
4601 !check_ann_subscr(c, targ->v.Subscript.slice))) {
4602 return 0;
4603 }
4604 break;
4605 default:
4606 PyErr_Format(PyExc_SystemError,
4607 "invalid node type (%d) for annotated assignment",
4608 targ->kind);
4609 return 0;
4610 }
4611 /* Annotation is evaluated last. */
4612 if (!s->v.AnnAssign.simple && !check_annotation(c, s)) {
4613 return 0;
4614 }
4615 return 1;
4616 }
4617
4618 static int
3627 compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b) 4619 compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
3628 { 4620 {
3629 struct fblockinfo *f; 4621 struct fblockinfo *f;
3630 if (c->u->u_nfblocks >= CO_MAXBLOCKS) { 4622 if (c->u->u_nfblocks >= CO_MAXBLOCKS) {
3631 PyErr_SetString(PyExc_SystemError, 4623 PyErr_SetString(PyExc_SyntaxError,
3632 "too many statically nested blocks"); 4624 "too many statically nested blocks");
3633 return 0; 4625 return 0;
3634 } 4626 }
3635 f = &c->u->u_fblock[c->u->u_nfblocks++]; 4627 f = &c->u->u_fblock[c->u->u_nfblocks++];
3636 f->fb_type = t; 4628 f->fb_type = t;
3637 f->fb_block = b; 4629 f->fb_block = b;
3638 return 1; 4630 return 1;
3639 } 4631 }
3640 4632
3641 static void 4633 static void
(...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after
3914 assemble_init(struct assembler *a, int nblocks, int firstlineno) 4906 assemble_init(struct assembler *a, int nblocks, int firstlineno)
3915 { 4907 {
3916 memset(a, 0, sizeof(struct assembler)); 4908 memset(a, 0, sizeof(struct assembler));
3917 a->a_lineno = firstlineno; 4909 a->a_lineno = firstlineno;
3918 a->a_bytecode = PyBytes_FromStringAndSize(NULL, DEFAULT_CODE_SIZE); 4910 a->a_bytecode = PyBytes_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
3919 if (!a->a_bytecode) 4911 if (!a->a_bytecode)
3920 return 0; 4912 return 0;
3921 a->a_lnotab = PyBytes_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE); 4913 a->a_lnotab = PyBytes_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
3922 if (!a->a_lnotab) 4914 if (!a->a_lnotab)
3923 return 0; 4915 return 0;
3924 if ((size_t)nblocks > PY_SIZE_MAX / sizeof(basicblock *)) { 4916 if ((size_t)nblocks > SIZE_MAX / sizeof(basicblock *)) {
3925 PyErr_NoMemory(); 4917 PyErr_NoMemory();
3926 return 0; 4918 return 0;
3927 } 4919 }
3928 a->a_postorder = (basicblock **)PyObject_Malloc( 4920 a->a_postorder = (basicblock **)PyObject_Malloc(
3929 sizeof(basicblock *) * nblocks); 4921 sizeof(basicblock *) * nblocks);
3930 if (!a->a_postorder) { 4922 if (!a->a_postorder) {
3931 PyErr_NoMemory(); 4923 PyErr_NoMemory();
3932 return 0; 4924 return 0;
3933 } 4925 }
3934 return 1; 4926 return 1;
3935 } 4927 }
3936 4928
3937 static void 4929 static void
3938 assemble_free(struct assembler *a) 4930 assemble_free(struct assembler *a)
3939 { 4931 {
3940 Py_XDECREF(a->a_bytecode); 4932 Py_XDECREF(a->a_bytecode);
3941 Py_XDECREF(a->a_lnotab); 4933 Py_XDECREF(a->a_lnotab);
3942 if (a->a_postorder) 4934 if (a->a_postorder)
3943 PyObject_Free(a->a_postorder); 4935 PyObject_Free(a->a_postorder);
3944 } 4936 }
3945 4937
3946 /* Return the size of a basic block in bytes. */
3947
3948 static int
3949 instrsize(struct instr *instr)
3950 {
3951 if (!instr->i_hasarg)
3952 return 1; /* 1 byte for the opcode*/
3953 if (instr->i_oparg > 0xffff)
3954 return 6; /* 1 (opcode) + 1 (EXTENDED_ARG opcode) + 2 (opa rg) + 2(oparg extended) */
3955 return 3; /* 1 (opcode) + 2 (oparg) */
3956 }
3957
3958 static int 4938 static int
3959 blocksize(basicblock *b) 4939 blocksize(basicblock *b)
3960 { 4940 {
3961 int i; 4941 int i;
3962 int size = 0; 4942 int size = 0;
3963 4943
3964 for (i = 0; i < b->b_iused; i++) 4944 for (i = 0; i < b->b_iused; i++)
3965 size += instrsize(&b->b_instr[i]); 4945 size += instrsize(b->b_instr[i].i_oparg);
3966 return size; 4946 return size;
3967 } 4947 }
3968 4948
3969 /* Appends a pair to the end of the line number table, a_lnotab, representing 4949 /* Appends a pair to the end of the line number table, a_lnotab, representing
3970 the instruction's bytecode offset and line number. See 4950 the instruction's bytecode offset and line number. See
3971 Objects/lnotab_notes.txt for the description of the line number table. */ 4951 Objects/lnotab_notes.txt for the description of the line number table. */
3972 4952
3973 static int 4953 static int
3974 assemble_lnotab(struct assembler *a, struct instr *i) 4954 assemble_lnotab(struct assembler *a, struct instr *i)
3975 { 4955 {
3976 int d_bytecode, d_lineno; 4956 int d_bytecode, d_lineno;
3977 Py_ssize_t len; 4957 Py_ssize_t len;
3978 unsigned char *lnotab; 4958 unsigned char *lnotab;
3979 4959
3980 d_bytecode = a->a_offset - a->a_lineno_off; 4960 d_bytecode = (a->a_offset - a->a_lineno_off) * sizeof(_Py_CODEUNIT);
3981 d_lineno = i->i_lineno - a->a_lineno; 4961 d_lineno = i->i_lineno - a->a_lineno;
3982 4962
3983 assert(d_bytecode >= 0); 4963 assert(d_bytecode >= 0);
3984 assert(d_lineno >= 0);
3985 4964
3986 if(d_bytecode == 0 && d_lineno == 0) 4965 if(d_bytecode == 0 && d_lineno == 0)
3987 return 1; 4966 return 1;
3988 4967
3989 if (d_bytecode > 255) { 4968 if (d_bytecode > 255) {
3990 int j, nbytes, ncodes = d_bytecode / 255; 4969 int j, nbytes, ncodes = d_bytecode / 255;
3991 nbytes = a->a_lnotab_off + 2 * ncodes; 4970 nbytes = a->a_lnotab_off + 2 * ncodes;
3992 len = PyBytes_GET_SIZE(a->a_lnotab); 4971 len = PyBytes_GET_SIZE(a->a_lnotab);
3993 if (nbytes >= len) { 4972 if (nbytes >= len) {
3994 if ((len <= INT_MAX / 2) && (len * 2 < nbytes)) 4973 if ((len <= INT_MAX / 2) && (len * 2 < nbytes))
3995 len = nbytes; 4974 len = nbytes;
3996 else if (len <= INT_MAX / 2) 4975 else if (len <= INT_MAX / 2)
3997 len *= 2; 4976 len *= 2;
3998 else { 4977 else {
3999 PyErr_NoMemory(); 4978 PyErr_NoMemory();
4000 return 0; 4979 return 0;
4001 } 4980 }
4002 if (_PyBytes_Resize(&a->a_lnotab, len) < 0) 4981 if (_PyBytes_Resize(&a->a_lnotab, len) < 0)
4003 return 0; 4982 return 0;
4004 } 4983 }
4005 lnotab = (unsigned char *) 4984 lnotab = (unsigned char *)
4006 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off; 4985 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
4007 for (j = 0; j < ncodes; j++) { 4986 for (j = 0; j < ncodes; j++) {
4008 *lnotab++ = 255; 4987 *lnotab++ = 255;
4009 *lnotab++ = 0; 4988 *lnotab++ = 0;
4010 } 4989 }
4011 d_bytecode -= ncodes * 255; 4990 d_bytecode -= ncodes * 255;
4012 a->a_lnotab_off += ncodes * 2; 4991 a->a_lnotab_off += ncodes * 2;
4013 } 4992 }
4014 assert(d_bytecode <= 255); 4993 assert(0 <= d_bytecode && d_bytecode <= 255);
4015 if (d_lineno > 255) { 4994
4016 int j, nbytes, ncodes = d_lineno / 255; 4995 if (d_lineno < -128 || 127 < d_lineno) {
4996 int j, nbytes, ncodes, k;
4997 if (d_lineno < 0) {
4998 k = -128;
4999 /* use division on positive numbers */
5000 ncodes = (-d_lineno) / 128;
5001 }
5002 else {
5003 k = 127;
5004 ncodes = d_lineno / 127;
5005 }
5006 d_lineno -= ncodes * k;
5007 assert(ncodes >= 1);
4017 nbytes = a->a_lnotab_off + 2 * ncodes; 5008 nbytes = a->a_lnotab_off + 2 * ncodes;
4018 len = PyBytes_GET_SIZE(a->a_lnotab); 5009 len = PyBytes_GET_SIZE(a->a_lnotab);
4019 if (nbytes >= len) { 5010 if (nbytes >= len) {
4020 if ((len <= INT_MAX / 2) && len * 2 < nbytes) 5011 if ((len <= INT_MAX / 2) && len * 2 < nbytes)
4021 len = nbytes; 5012 len = nbytes;
4022 else if (len <= INT_MAX / 2) 5013 else if (len <= INT_MAX / 2)
4023 len *= 2; 5014 len *= 2;
4024 else { 5015 else {
4025 PyErr_NoMemory(); 5016 PyErr_NoMemory();
4026 return 0; 5017 return 0;
4027 } 5018 }
4028 if (_PyBytes_Resize(&a->a_lnotab, len) < 0) 5019 if (_PyBytes_Resize(&a->a_lnotab, len) < 0)
4029 return 0; 5020 return 0;
4030 } 5021 }
4031 lnotab = (unsigned char *) 5022 lnotab = (unsigned char *)
4032 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off; 5023 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
4033 *lnotab++ = d_bytecode; 5024 *lnotab++ = d_bytecode;
4034 *lnotab++ = 255; 5025 *lnotab++ = k;
4035 d_bytecode = 0; 5026 d_bytecode = 0;
4036 for (j = 1; j < ncodes; j++) { 5027 for (j = 1; j < ncodes; j++) {
4037 *lnotab++ = 0; 5028 *lnotab++ = 0;
4038 *lnotab++ = 255; 5029 *lnotab++ = k;
4039 } 5030 }
4040 d_lineno -= ncodes * 255;
4041 a->a_lnotab_off += ncodes * 2; 5031 a->a_lnotab_off += ncodes * 2;
4042 } 5032 }
5033 assert(-128 <= d_lineno && d_lineno <= 127);
4043 5034
4044 len = PyBytes_GET_SIZE(a->a_lnotab); 5035 len = PyBytes_GET_SIZE(a->a_lnotab);
4045 if (a->a_lnotab_off + 2 >= len) { 5036 if (a->a_lnotab_off + 2 >= len) {
4046 if (_PyBytes_Resize(&a->a_lnotab, len * 2) < 0) 5037 if (_PyBytes_Resize(&a->a_lnotab, len * 2) < 0)
4047 return 0; 5038 return 0;
4048 } 5039 }
4049 lnotab = (unsigned char *) 5040 lnotab = (unsigned char *)
4050 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off; 5041 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
4051 5042
4052 a->a_lnotab_off += 2; 5043 a->a_lnotab_off += 2;
(...skipping 11 matching lines...) Expand all
4064 } 5055 }
4065 5056
4066 /* assemble_emit() 5057 /* assemble_emit()
4067 Extend the bytecode with a new instruction. 5058 Extend the bytecode with a new instruction.
4068 Update lnotab if necessary. 5059 Update lnotab if necessary.
4069 */ 5060 */
4070 5061
4071 static int 5062 static int
4072 assemble_emit(struct assembler *a, struct instr *i) 5063 assemble_emit(struct assembler *a, struct instr *i)
4073 { 5064 {
4074 int size, arg = 0, ext = 0; 5065 int size, arg = 0;
4075 Py_ssize_t len = PyBytes_GET_SIZE(a->a_bytecode); 5066 Py_ssize_t len = PyBytes_GET_SIZE(a->a_bytecode);
4076 char *code; 5067 _Py_CODEUNIT *code;
4077 5068
4078 size = instrsize(i); 5069 arg = i->i_oparg;
4079 if (i->i_hasarg) { 5070 size = instrsize(arg);
4080 arg = i->i_oparg;
4081 ext = arg >> 16;
4082 }
4083 if (i->i_lineno && !assemble_lnotab(a, i)) 5071 if (i->i_lineno && !assemble_lnotab(a, i))
4084 return 0; 5072 return 0;
4085 if (a->a_offset + size >= len) { 5073 if (a->a_offset + size >= len / (int)sizeof(_Py_CODEUNIT)) {
4086 if (len > PY_SSIZE_T_MAX / 2) 5074 if (len > PY_SSIZE_T_MAX / 2)
4087 return 0; 5075 return 0;
4088 if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0) 5076 if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0)
4089 return 0; 5077 return 0;
4090 } 5078 }
4091 code = PyBytes_AS_STRING(a->a_bytecode) + a->a_offset; 5079 code = (_Py_CODEUNIT *)PyBytes_AS_STRING(a->a_bytecode) + a->a_offset;
4092 a->a_offset += size; 5080 a->a_offset += size;
4093 if (size == 6) { 5081 write_op_arg(code, i->i_opcode, arg, size);
4094 assert(i->i_hasarg);
4095 *code++ = (char)EXTENDED_ARG;
4096 *code++ = ext & 0xff;
4097 *code++ = ext >> 8;
4098 arg &= 0xffff;
4099 }
4100 *code++ = i->i_opcode;
4101 if (i->i_hasarg) {
4102 assert(size == 3 || size == 6);
4103 *code++ = arg & 0xff;
4104 *code++ = arg >> 8;
4105 }
4106 return 1; 5082 return 1;
4107 } 5083 }
4108 5084
4109 static void 5085 static void
4110 assemble_jump_offsets(struct assembler *a, struct compiler *c) 5086 assemble_jump_offsets(struct assembler *a, struct compiler *c)
4111 { 5087 {
4112 basicblock *b; 5088 basicblock *b;
4113 int bsize, totsize, extended_arg_count = 0, last_extended_arg_count; 5089 int bsize, totsize, extended_arg_recompile;
4114 int i; 5090 int i;
4115 5091
4116 /* Compute the size of each block and fixup jump args. 5092 /* Compute the size of each block and fixup jump args.
4117 Replace block pointer with position in bytecode. */ 5093 Replace block pointer with position in bytecode. */
4118 do { 5094 do {
4119 totsize = 0; 5095 totsize = 0;
4120 for (i = a->a_nblocks - 1; i >= 0; i--) { 5096 for (i = a->a_nblocks - 1; i >= 0; i--) {
4121 b = a->a_postorder[i]; 5097 b = a->a_postorder[i];
4122 bsize = blocksize(b); 5098 bsize = blocksize(b);
4123 b->b_offset = totsize; 5099 b->b_offset = totsize;
4124 totsize += bsize; 5100 totsize += bsize;
4125 } 5101 }
4126 last_extended_arg_count = extended_arg_count; 5102 extended_arg_recompile = 0;
4127 extended_arg_count = 0;
4128 for (b = c->u->u_blocks; b != NULL; b = b->b_list) { 5103 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
4129 bsize = b->b_offset; 5104 bsize = b->b_offset;
4130 for (i = 0; i < b->b_iused; i++) { 5105 for (i = 0; i < b->b_iused; i++) {
4131 struct instr *instr = &b->b_instr[i]; 5106 struct instr *instr = &b->b_instr[i];
5107 int isize = instrsize(instr->i_oparg);
4132 /* Relative jumps are computed relative to 5108 /* Relative jumps are computed relative to
4133 the instruction pointer after fetching 5109 the instruction pointer after fetching
4134 the jump instruction. 5110 the jump instruction.
4135 */ 5111 */
4136 bsize += instrsize(instr); 5112 bsize += isize;
4137 if (instr->i_jabs) 5113 if (instr->i_jabs || instr->i_jrel) {
4138 instr->i_oparg = instr->i_target->b_offset; 5114 instr->i_oparg = instr->i_target->b_offset;
4139 else if (instr->i_jrel) { 5115 if (instr->i_jrel) {
4140 int delta = instr->i_target->b_offset - bsize; 5116 instr->i_oparg -= bsize;
4141 instr->i_oparg = delta; 5117 }
5118 instr->i_oparg *= sizeof(_Py_CODEUNIT);
5119 if (instrsize(instr->i_oparg) != isize) {
5120 extended_arg_recompile = 1;
5121 }
4142 } 5122 }
4143 else
4144 continue;
4145 if (instr->i_oparg > 0xffff)
4146 extended_arg_count++;
4147 } 5123 }
4148 } 5124 }
4149 5125
4150 /* XXX: This is an awful hack that could hurt performance, but 5126 /* XXX: This is an awful hack that could hurt performance, but
4151 on the bright side it should work until we come up 5127 on the bright side it should work until we come up
4152 with a better solution. 5128 with a better solution.
4153 5129
4154 The issue is that in the first loop blocksize() is called 5130 The issue is that in the first loop blocksize() is called
4155 which calls instrsize() which requires i_oparg be set 5131 which calls instrsize() which requires i_oparg be set
4156 appropriately. There is a bootstrap problem because 5132 appropriately. There is a bootstrap problem because
4157 i_oparg is calculated in the second loop above. 5133 i_oparg is calculated in the second loop above.
4158 5134
4159 So we loop until we stop seeing new EXTENDED_ARGs. 5135 So we loop until we stop seeing new EXTENDED_ARGs.
4160 The only EXTENDED_ARGs that could be popping up are 5136 The only EXTENDED_ARGs that could be popping up are
4161 ones in jump instructions. So this should converge 5137 ones in jump instructions. So this should converge
4162 fairly quickly. 5138 fairly quickly.
4163 */ 5139 */
4164 } while (last_extended_arg_count != extended_arg_count); 5140 } while (extended_arg_recompile);
4165 } 5141 }
4166 5142
4167 static PyObject * 5143 static PyObject *
4168 dict_keys_inorder(PyObject *dict, Py_ssize_t offset) 5144 dict_keys_inorder(PyObject *dict, Py_ssize_t offset)
4169 { 5145 {
4170 PyObject *tuple, *k, *v; 5146 PyObject *tuple, *k, *v;
4171 Py_ssize_t i, pos = 0, size = PyDict_Size(dict); 5147 Py_ssize_t i, pos = 0, size = PyDict_Size(dict);
4172 5148
4173 tuple = PyTuple_New(size); 5149 tuple = PyTuple_New(size);
4174 if (tuple == NULL) 5150 if (tuple == NULL)
4175 return NULL; 5151 return NULL;
4176 while (PyDict_Next(dict, &pos, &k, &v)) { 5152 while (PyDict_Next(dict, &pos, &k, &v)) {
4177 i = PyLong_AS_LONG(v); 5153 i = PyLong_AS_LONG(v);
4178 /* The keys of the dictionary are tuples. (see compiler_add_o) 5154 /* The keys of the dictionary are tuples. (see compiler_add_o
4179 The object we want is always first, though. */ 5155 * and _PyCode_ConstantKey). The object we want is always second,
4180 k = PyTuple_GET_ITEM(k, 0); 5156 * though. */
5157 k = PyTuple_GET_ITEM(k, 1);
4181 Py_INCREF(k); 5158 Py_INCREF(k);
4182 assert((i - offset) < size); 5159 assert((i - offset) < size);
4183 assert((i - offset) >= 0); 5160 assert((i - offset) >= 0);
4184 PyTuple_SET_ITEM(tuple, i - offset, k); 5161 PyTuple_SET_ITEM(tuple, i - offset, k);
4185 } 5162 }
4186 return tuple; 5163 return tuple;
4187 } 5164 }
4188 5165
4189 static int 5166 static int
4190 compute_code_flags(struct compiler *c) 5167 compute_code_flags(struct compiler *c)
4191 { 5168 {
4192 PySTEntryObject *ste = c->u->u_ste; 5169 PySTEntryObject *ste = c->u->u_ste;
4193 int flags = 0; 5170 int flags = 0;
4194 Py_ssize_t n; 5171 Py_ssize_t n;
4195 if (ste->ste_type == FunctionBlock) { 5172 if (ste->ste_type == FunctionBlock) {
4196 flags |= CO_NEWLOCALS; 5173 flags |= CO_NEWLOCALS | CO_OPTIMIZED;
4197 if (!ste->ste_unoptimized)
4198 flags |= CO_OPTIMIZED;
4199 if (ste->ste_nested) 5174 if (ste->ste_nested)
4200 flags |= CO_NESTED; 5175 flags |= CO_NESTED;
4201 if (ste->ste_generator) 5176 if (ste->ste_generator && !ste->ste_coroutine)
4202 flags |= CO_GENERATOR; 5177 flags |= CO_GENERATOR;
5178 if (!ste->ste_generator && ste->ste_coroutine)
5179 flags |= CO_COROUTINE;
5180 if (ste->ste_generator && ste->ste_coroutine)
5181 flags |= CO_ASYNC_GENERATOR;
4203 if (ste->ste_varargs) 5182 if (ste->ste_varargs)
4204 flags |= CO_VARARGS; 5183 flags |= CO_VARARGS;
4205 if (ste->ste_varkeywords) 5184 if (ste->ste_varkeywords)
4206 flags |= CO_VARKEYWORDS; 5185 flags |= CO_VARKEYWORDS;
4207 } 5186 }
4208 5187
4209 /* (Only) inherit compilerflags in PyCF_MASK */ 5188 /* (Only) inherit compilerflags in PyCF_MASK */
4210 flags |= (c->c_flags->cf_flags & PyCF_MASK); 5189 flags |= (c->c_flags->cf_flags & PyCF_MASK);
4211 5190
4212 n = PyDict_Size(c->u->u_freevars); 5191 n = PyDict_Size(c->u->u_freevars);
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
4301 /* For debugging purposes only */ 5280 /* For debugging purposes only */
4302 #if 0 5281 #if 0
4303 static void 5282 static void
4304 dump_instr(const struct instr *i) 5283 dump_instr(const struct instr *i)
4305 { 5284 {
4306 const char *jrel = i->i_jrel ? "jrel " : ""; 5285 const char *jrel = i->i_jrel ? "jrel " : "";
4307 const char *jabs = i->i_jabs ? "jabs " : ""; 5286 const char *jabs = i->i_jabs ? "jabs " : "";
4308 char arg[128]; 5287 char arg[128];
4309 5288
4310 *arg = '\0'; 5289 *arg = '\0';
4311 if (i->i_hasarg) 5290 if (HAS_ARG(i->i_opcode)) {
4312 sprintf(arg, "arg: %d ", i->i_oparg); 5291 sprintf(arg, "arg: %d ", i->i_oparg);
4313 5292 }
4314 fprintf(stderr, "line: %d, opcode: %d %s%s%s\n", 5293 fprintf(stderr, "line: %d, opcode: %d %s%s%s\n",
4315 i->i_lineno, i->i_opcode, arg, jabs, jrel); 5294 i->i_lineno, i->i_opcode, arg, jabs, jrel);
4316 } 5295 }
4317 5296
4318 static void 5297 static void
4319 dump_basicblock(const basicblock *b) 5298 dump_basicblock(const basicblock *b)
4320 { 5299 {
4321 const char *seen = b->b_seen ? "seen " : ""; 5300 const char *seen = b->b_seen ? "seen " : "";
4322 const char *b_return = b->b_return ? "return " : ""; 5301 const char *b_return = b->b_return ? "return " : "";
4323 fprintf(stderr, "used: %d, depth: %d, offset: %d %s%s\n", 5302 fprintf(stderr, "used: %d, depth: %d, offset: %d %s%s\n",
(...skipping 29 matching lines...) Expand all
4353 5332
4354 nblocks = 0; 5333 nblocks = 0;
4355 entryblock = NULL; 5334 entryblock = NULL;
4356 for (b = c->u->u_blocks; b != NULL; b = b->b_list) { 5335 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
4357 nblocks++; 5336 nblocks++;
4358 entryblock = b; 5337 entryblock = b;
4359 } 5338 }
4360 5339
4361 /* Set firstlineno if it wasn't explicitly set. */ 5340 /* Set firstlineno if it wasn't explicitly set. */
4362 if (!c->u->u_firstlineno) { 5341 if (!c->u->u_firstlineno) {
4363 if (entryblock && entryblock->b_instr) 5342 if (entryblock && entryblock->b_instr && entryblock->b_instr->i_lineno)
4364 c->u->u_firstlineno = entryblock->b_instr->i_lineno; 5343 c->u->u_firstlineno = entryblock->b_instr->i_lineno;
4365 else 5344 else
4366 c->u->u_firstlineno = 1; 5345 c->u->u_firstlineno = 1;
4367 } 5346 }
4368 if (!assemble_init(&a, nblocks, c->u->u_firstlineno)) 5347 if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
4369 goto error; 5348 goto error;
4370 dfs(c, entryblock, &a); 5349 dfs(c, entryblock, &a);
4371 5350
4372 /* Can't modify the bytecode after computing jump offsets. */ 5351 /* Can't modify the bytecode after computing jump offsets. */
4373 assemble_jump_offsets(&a, c); 5352 assemble_jump_offsets(&a, c);
4374 5353
4375 /* Emit code in reverse postorder from dfs. */ 5354 /* Emit code in reverse postorder from dfs. */
4376 for (i = a.a_nblocks - 1; i >= 0; i--) { 5355 for (i = a.a_nblocks - 1; i >= 0; i--) {
4377 b = a.a_postorder[i]; 5356 b = a.a_postorder[i];
4378 for (j = 0; j < b->b_iused; j++) 5357 for (j = 0; j < b->b_iused; j++)
4379 if (!assemble_emit(&a, &b->b_instr[j])) 5358 if (!assemble_emit(&a, &b->b_instr[j]))
4380 goto error; 5359 goto error;
4381 } 5360 }
4382 5361
4383 if (_PyBytes_Resize(&a.a_lnotab, a.a_lnotab_off) < 0) 5362 if (_PyBytes_Resize(&a.a_lnotab, a.a_lnotab_off) < 0)
4384 goto error; 5363 goto error;
4385 if (_PyBytes_Resize(&a.a_bytecode, a.a_offset) < 0) 5364 if (_PyBytes_Resize(&a.a_bytecode, a.a_offset * sizeof(_Py_CODEUNIT)) < 0)
4386 goto error; 5365 goto error;
4387 5366
4388 co = makecode(c, &a); 5367 co = makecode(c, &a);
4389 error: 5368 error:
4390 assemble_free(&a); 5369 assemble_free(&a);
4391 return co; 5370 return co;
4392 } 5371 }
4393 5372
4394 #undef PyAST_Compile 5373 #undef PyAST_Compile
4395 PyAPI_FUNC(PyCodeObject *) 5374 PyAPI_FUNC(PyCodeObject *)
4396 PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags, 5375 PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags,
4397 PyArena *arena) 5376 PyArena *arena)
4398 { 5377 {
4399 return PyAST_CompileEx(mod, filename, flags, -1, arena); 5378 return PyAST_CompileEx(mod, filename, flags, -1, arena);
4400 } 5379 }
4401
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+