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

Delta Between Two Patch Sets: Python/compile.c

Issue 11619: On Windows, don't encode filenames in the import machinery
Left Patch Set: Created 8 years, 4 months ago
Right Patch Set: Created 5 years, 11 months 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
« no previous file with change/comment | « Python/ast.c ('k') | Python/errors.c » ('j') | 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 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
83 compiler IR. 83 compiler IR.
84 */ 84 */
85 85
86 enum fblocktype { LOOP, EXCEPT, FINALLY_TRY, FINALLY_END }; 86 enum fblocktype { LOOP, EXCEPT, FINALLY_TRY, FINALLY_END };
87 87
88 struct fblockinfo { 88 struct fblockinfo {
89 enum fblocktype fb_type; 89 enum fblocktype fb_type;
90 basicblock *fb_block; 90 basicblock *fb_block;
91 }; 91 };
92 92
93 enum {
94 COMPILER_SCOPE_MODULE,
95 COMPILER_SCOPE_CLASS,
96 COMPILER_SCOPE_FUNCTION,
97 COMPILER_SCOPE_COMPREHENSION,
98 };
99
93 /* The following items change on entry and exit of code blocks. 100 /* The following items change on entry and exit of code blocks.
94 They must be saved and restored when returning to a block. 101 They must be saved and restored when returning to a block.
95 */ 102 */
96 struct compiler_unit { 103 struct compiler_unit {
97 PySTEntryObject *u_ste; 104 PySTEntryObject *u_ste;
98 105
99 PyObject *u_name; 106 PyObject *u_name;
107 PyObject *u_qualname; /* dot-separated qualified name (lazy) */
108 int u_scope_type;
109
100 /* The following fields are dicts that map objects to 110 /* The following fields are dicts that map objects to
101 the index of them in co_XXX. The index is used as 111 the index of them in co_XXX. The index is used as
102 the argument for opcodes that refer to those collections. 112 the argument for opcodes that refer to those collections.
103 */ 113 */
104 PyObject *u_consts; /* all constants */ 114 PyObject *u_consts; /* all constants */
105 PyObject *u_names; /* all names */ 115 PyObject *u_names; /* all names */
106 PyObject *u_varnames; /* local variables */ 116 PyObject *u_varnames; /* local variables */
107 PyObject *u_cellvars; /* cell variables */ 117 PyObject *u_cellvars; /* cell variables */
108 PyObject *u_freevars; /* free variables */ 118 PyObject *u_freevars; /* free variables */
109 119
(...skipping 14 matching lines...) Expand all
124 int u_col_offset; /* the offset of the current stmt */ 134 int u_col_offset; /* the offset of the current stmt */
125 int u_lineno_set; /* boolean to indicate whether instr 135 int u_lineno_set; /* boolean to indicate whether instr
126 has been generated with current lineno */ 136 has been generated with current lineno */
127 }; 137 };
128 138
129 /* This struct captures the global state of a compilation. 139 /* This struct captures the global state of a compilation.
130 140
131 The u pointer points to the current compilation unit, while units 141 The u pointer points to the current compilation unit, while units
132 for enclosing blocks are stored in c_stack. The u and c_stack are 142 for enclosing blocks are stored in c_stack. The u and c_stack are
133 managed by compiler_enter_scope() and compiler_exit_scope(). 143 managed by compiler_enter_scope() and compiler_exit_scope().
144
145 Note that we don't track recursion levels during compilation - the
146 task of detecting and rejecting excessive levels of nesting is
147 handled by the symbol analysis pass.
148
134 */ 149 */
135 150
136 struct compiler { 151 struct compiler {
137 PyObject *filename; 152 PyObject *c_filename;
138 struct symtable *c_st; 153 struct symtable *c_st;
139 PyFutureFeatures *c_future; /* pointer to module's __future__ */ 154 PyFutureFeatures *c_future; /* pointer to module's __future__ */
140 PyCompilerFlags *c_flags; 155 PyCompilerFlags *c_flags;
141 156
142 int c_optimize; /* optimization level */ 157 int c_optimize; /* optimization level */
143 int c_interactive; /* true if in interactive mode */ 158 int c_interactive; /* true if in interactive mode */
144 int c_nestlevel; 159 int c_nestlevel;
145 160
146 struct compiler_unit *u; /* compiler state for current block */ 161 struct compiler_unit *u; /* compiler state for current block */
147 PyObject *c_stack; /* Python list holding compiler_unit ptrs */ 162 PyObject *c_stack; /* Python list holding compiler_unit ptrs */
148 PyArena *c_arena; /* pointer to memory allocation arena */ 163 PyArena *c_arena; /* pointer to memory allocation arena */
149 }; 164 };
150 165
151 static int compiler_enter_scope(struct compiler *, identifier, void *, int); 166 static int compiler_enter_scope(struct compiler *, identifier, int, void *, int) ;
152 static void compiler_free(struct compiler *); 167 static void compiler_free(struct compiler *);
153 static basicblock *compiler_new_block(struct compiler *); 168 static basicblock *compiler_new_block(struct compiler *);
154 static int compiler_next_instr(struct compiler *, basicblock *); 169 static int compiler_next_instr(struct compiler *, basicblock *);
155 static int compiler_addop(struct compiler *, int); 170 static int compiler_addop(struct compiler *, int);
156 static int compiler_addop_o(struct compiler *, int, PyObject *, PyObject *); 171 static int compiler_addop_o(struct compiler *, int, PyObject *, PyObject *);
157 static int compiler_addop_i(struct compiler *, int, int); 172 static int compiler_addop_i(struct compiler *, int, int);
158 static int compiler_addop_j(struct compiler *, int, basicblock *, int); 173 static int compiler_addop_j(struct compiler *, int, basicblock *, int);
159 static basicblock *compiler_use_new_block(struct compiler *); 174 static basicblock *compiler_use_new_block(struct compiler *);
160 static int compiler_error(struct compiler *, const char *); 175 static int compiler_error(struct compiler *, const char *);
161 static int compiler_nameop(struct compiler *, identifier, expr_context_ty); 176 static int compiler_nameop(struct compiler *, identifier, expr_context_ty);
162 177
163 static PyCodeObject *compiler_mod(struct compiler *, mod_ty); 178 static PyCodeObject *compiler_mod(struct compiler *, mod_ty);
164 static int compiler_visit_stmt(struct compiler *, stmt_ty); 179 static int compiler_visit_stmt(struct compiler *, stmt_ty);
165 static int compiler_visit_keyword(struct compiler *, keyword_ty); 180 static int compiler_visit_keyword(struct compiler *, keyword_ty);
166 static int compiler_visit_expr(struct compiler *, expr_ty); 181 static int compiler_visit_expr(struct compiler *, expr_ty);
167 static int compiler_augassign(struct compiler *, stmt_ty); 182 static int compiler_augassign(struct compiler *, stmt_ty);
168 static int compiler_visit_slice(struct compiler *, slice_ty, 183 static int compiler_visit_slice(struct compiler *, slice_ty,
169 expr_context_ty); 184 expr_context_ty);
170 185
171 static int compiler_push_fblock(struct compiler *, enum fblocktype, 186 static int compiler_push_fblock(struct compiler *, enum fblocktype,
172 basicblock *); 187 basicblock *);
173 static void compiler_pop_fblock(struct compiler *, enum fblocktype, 188 static void compiler_pop_fblock(struct compiler *, enum fblocktype,
174 basicblock *); 189 basicblock *);
175 /* Returns true if there is a loop on the fblock stack. */ 190 /* Returns true if there is a loop on the fblock stack. */
176 static int compiler_in_loop(struct compiler *); 191 static int compiler_in_loop(struct compiler *);
177 192
178 static int inplace_binop(struct compiler *, operator_ty); 193 static int inplace_binop(struct compiler *, operator_ty);
179 static int expr_constant(struct compiler *, expr_ty); 194 static int expr_constant(struct compiler *, expr_ty);
180 195
181 static int compiler_with(struct compiler *, stmt_ty); 196 static int compiler_with(struct compiler *, stmt_ty, int);
182 static int compiler_call_helper(struct compiler *c, int n, 197 static int compiler_call_helper(struct compiler *c, int n,
183 asdl_seq *args, 198 asdl_seq *args,
184 asdl_seq *keywords, 199 asdl_seq *keywords,
185 expr_ty starargs, 200 expr_ty starargs,
186 expr_ty kwargs); 201 expr_ty kwargs);
202 static int compiler_try_except(struct compiler *, stmt_ty);
187 203
188 static PyCodeObject *assemble(struct compiler *, int addNone); 204 static PyCodeObject *assemble(struct compiler *, int addNone);
189 static PyObject *__doc__; 205 static PyObject *__doc__;
190 206
191 #define COMPILER_CAPSULE_NAME_COMPILER_UNIT "compile.c compiler unit" 207 #define COMPILER_CAPSULE_NAME_COMPILER_UNIT "compile.c compiler unit"
192 208
193 PyObject * 209 PyObject *
194 _Py_Mangle(PyObject *privateobj, PyObject *ident) 210 _Py_Mangle(PyObject *privateobj, PyObject *ident)
195 { 211 {
196 /* Name mangling: __private becomes _classname__private. 212 /* Name mangling: __private becomes _classname__private.
197 This is independent from how the name is used. */ 213 This is independent from how the name is used. */
198 const Py_UNICODE *p, *name = PyUnicode_AS_UNICODE(ident); 214 PyObject *result;
199 Py_UNICODE *buffer; 215 size_t nlen, plen, ipriv;
200 size_t nlen, plen; 216 Py_UCS4 maxchar;
201 if (privateobj == NULL || !PyUnicode_Check(privateobj) || 217 if (privateobj == NULL || !PyUnicode_Check(privateobj) ||
202 name == NULL || name[0] != '_' || name[1] != '_') { 218 PyUnicode_READ_CHAR(ident, 0) != '_' ||
219 PyUnicode_READ_CHAR(ident, 1) != '_') {
203 Py_INCREF(ident); 220 Py_INCREF(ident);
204 return ident; 221 return ident;
205 } 222 }
206 p = PyUnicode_AS_UNICODE(privateobj); 223 nlen = PyUnicode_GET_LENGTH(ident);
207 nlen = Py_UNICODE_strlen(name); 224 plen = PyUnicode_GET_LENGTH(privateobj);
208 /* Don't mangle __id__ or names with dots. 225 /* Don't mangle __id__ or names with dots.
209 226
210 The only time a name with a dot can occur is when 227 The only time a name with a dot can occur is when
211 we are compiling an import statement that has a 228 we are compiling an import statement that has a
212 package name. 229 package name.
213 230
214 TODO(jhylton): Decide whether we want to support 231 TODO(jhylton): Decide whether we want to support
215 mangling of the module name, e.g. __M.X. 232 mangling of the module name, e.g. __M.X.
216 */ 233 */
217 if ((name[nlen-1] == '_' && name[nlen-2] == '_') 234 if ((PyUnicode_READ_CHAR(ident, nlen-1) == '_' &&
218 || Py_UNICODE_strchr(name, '.')) { 235 PyUnicode_READ_CHAR(ident, nlen-2) == '_') ||
236 PyUnicode_FindChar(ident, '.', 0, nlen, 1) != -1) {
219 Py_INCREF(ident); 237 Py_INCREF(ident);
220 return ident; /* Don't mangle __whatever__ */ 238 return ident; /* Don't mangle __whatever__ */
221 } 239 }
222 /* Strip leading underscores from class name */ 240 /* Strip leading underscores from class name */
223 while (*p == '_') 241 ipriv = 0;
224 p++; 242 while (PyUnicode_READ_CHAR(privateobj, ipriv) == '_')
225 if (*p == 0) { 243 ipriv++;
244 if (ipriv == plen) {
226 Py_INCREF(ident); 245 Py_INCREF(ident);
227 return ident; /* Don't mangle if class is just underscores */ 246 return ident; /* Don't mangle if class is just underscores */
228 } 247 }
229 plen = Py_UNICODE_strlen(p); 248 plen -= ipriv;
230 249
231 assert(1 <= PY_SSIZE_T_MAX - nlen); 250 if (plen + nlen >= PY_SSIZE_T_MAX - 1) {
232 assert(1 + nlen <= PY_SSIZE_T_MAX - plen); 251 PyErr_SetString(PyExc_OverflowError,
233 252 "private identifier too large to be mangled");
234 ident = PyUnicode_FromStringAndSize(NULL, 1 + nlen + plen); 253 return NULL;
235 if (!ident) 254 }
236 return 0; 255
237 /* ident = "_" + p[:plen] + name # i.e. 1+plen+nlen bytes */ 256 maxchar = PyUnicode_MAX_CHAR_VALUE(ident);
238 buffer = PyUnicode_AS_UNICODE(ident); 257 if (PyUnicode_MAX_CHAR_VALUE(privateobj) > maxchar)
239 buffer[0] = '_'; 258 maxchar = PyUnicode_MAX_CHAR_VALUE(privateobj);
240 Py_UNICODE_strncpy(buffer+1, p, plen); 259
241 Py_UNICODE_strcpy(buffer+1+plen, name); 260 result = PyUnicode_New(1 + nlen + plen, maxchar);
242 return ident; 261 if (!result)
262 return 0;
263 /* ident = "_" + priv[ipriv:] + ident # i.e. 1+plen+nlen bytes */
264 PyUnicode_WRITE(PyUnicode_KIND(result), PyUnicode_DATA(result), 0, '_');
265 if (PyUnicode_CopyCharacters(result, 1, privateobj, ipriv, plen) < 0) {
266 Py_DECREF(result);
267 return NULL;
268 }
269 if (PyUnicode_CopyCharacters(result, plen+1, ident, 0, nlen) < 0) {
270 Py_DECREF(result);
271 return NULL;
272 }
273 assert(_PyUnicode_CheckConsistency(result, 1));
274 return result;
243 } 275 }
244 276
245 static int 277 static int
246 compiler_init(struct compiler *c) 278 compiler_init(struct compiler *c)
247 { 279 {
248 memset(c, 0, sizeof(struct compiler)); 280 memset(c, 0, sizeof(struct compiler));
249 281
250 c->c_stack = PyList_New(0); 282 c->c_stack = PyList_New(0);
251 if (!c->c_stack) 283 if (!c->c_stack)
252 return 0; 284 return 0;
(...skipping 12 matching lines...) Expand all
265 297
266 if (!__doc__) { 298 if (!__doc__) {
267 __doc__ = PyUnicode_InternFromString("__doc__"); 299 __doc__ = PyUnicode_InternFromString("__doc__");
268 if (!__doc__) 300 if (!__doc__)
269 return NULL; 301 return NULL;
270 } 302 }
271 303
272 if (!compiler_init(&c)) 304 if (!compiler_init(&c))
273 return NULL; 305 return NULL;
274 Py_INCREF(filename); 306 Py_INCREF(filename);
275 c.filename = filename; 307 c.c_filename = filename;
276 c.c_arena = arena; 308 c.c_arena = arena;
277 c.c_future = PyFuture_FromASTObject(mod, filename); 309 c.c_future = PyFuture_FromASTObject(mod, filename);
278 if (c.c_future == NULL) 310 if (c.c_future == NULL)
279 goto finally; 311 goto finally;
280 if (!flags) { 312 if (!flags) {
281 local_flags.cf_flags = 0; 313 local_flags.cf_flags = 0;
282 flags = &local_flags; 314 flags = &local_flags;
283 } 315 }
284 merged = c.c_future->ff_features | flags->cf_flags; 316 merged = c.c_future->ff_features | flags->cf_flags;
285 c.c_future->ff_features = merged; 317 c.c_future->ff_features = merged;
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
334 } 366 }
335 367
336 static void 368 static void
337 compiler_free(struct compiler *c) 369 compiler_free(struct compiler *c)
338 { 370 {
339 if (c->c_st) 371 if (c->c_st)
340 PySymtable_Free(c->c_st); 372 PySymtable_Free(c->c_st);
341 if (c->c_future) 373 if (c->c_future)
342 PyObject_Free(c->c_future); 374 PyObject_Free(c->c_future);
343 Py_DECREF(c->c_stack); 375 Py_DECREF(c->c_stack);
344 Py_XDECREF(c->filename); 376 Py_XDECREF(c->c_filename);
345 } 377 }
346 378
347 static PyObject * 379 static PyObject *
348 list2dict(PyObject *list) 380 list2dict(PyObject *list)
349 { 381 {
350 Py_ssize_t i, n; 382 Py_ssize_t i, n;
351 PyObject *v, *k; 383 PyObject *v, *k;
352 PyObject *dict = PyDict_New(); 384 PyObject *dict = PyDict_New();
353 if (!dict) return NULL; 385 if (!dict) return NULL;
354 386
(...skipping 22 matching lines...) Expand all
377 409
378 src is a symbol table dictionary. If the scope of a name matches 410 src is a symbol table dictionary. If the scope of a name matches
379 either scope_type or flag is set, insert it into the new dict. The 411 either scope_type or flag is set, insert it into the new dict. The
380 values are integers, starting at offset and increasing by one for 412 values are integers, starting at offset and increasing by one for
381 each key. 413 each key.
382 */ 414 */
383 415
384 static PyObject * 416 static PyObject *
385 dictbytype(PyObject *src, int scope_type, int flag, int offset) 417 dictbytype(PyObject *src, int scope_type, int flag, int offset)
386 { 418 {
387 Py_ssize_t pos = 0, i = offset, scope; 419 Py_ssize_t i = offset, scope, num_keys, key_i;
388 PyObject *k, *v, *dest = PyDict_New(); 420 PyObject *k, *v, *dest = PyDict_New();
421 PyObject *sorted_keys;
389 422
390 assert(offset >= 0); 423 assert(offset >= 0);
391 if (dest == NULL) 424 if (dest == NULL)
392 return NULL; 425 return NULL;
393 426
394 while (PyDict_Next(src, &pos, &k, &v)) { 427 /* Sort the keys so that we have a deterministic order on the indexes
428 saved in the returned dictionary. These indexes are used as indexes
429 into the free and cell var storage. Therefore if they aren't
430 deterministic, then the generated bytecode is not deterministic.
431 */
432 sorted_keys = PyDict_Keys(src);
433 if (sorted_keys == NULL)
434 return NULL;
435 if (PyList_Sort(sorted_keys) != 0) {
436 Py_DECREF(sorted_keys);
437 return NULL;
438 }
439 num_keys = PyList_GET_SIZE(sorted_keys);
440
441 for (key_i = 0; key_i < num_keys; key_i++) {
395 /* XXX this should probably be a macro in symtable.h */ 442 /* XXX this should probably be a macro in symtable.h */
396 long vi; 443 long vi;
444 k = PyList_GET_ITEM(sorted_keys, key_i);
445 v = PyDict_GetItem(src, k);
397 assert(PyLong_Check(v)); 446 assert(PyLong_Check(v));
398 vi = PyLong_AS_LONG(v); 447 vi = PyLong_AS_LONG(v);
399 scope = (vi >> SCOPE_OFFSET) & SCOPE_MASK; 448 scope = (vi >> SCOPE_OFFSET) & SCOPE_MASK;
400 449
401 if (scope == scope_type || vi & flag) { 450 if (scope == scope_type || vi & flag) {
402 PyObject *tuple, *item = PyLong_FromLong(i); 451 PyObject *tuple, *item = PyLong_FromLong(i);
403 if (item == NULL) { 452 if (item == NULL) {
453 Py_DECREF(sorted_keys);
404 Py_DECREF(dest); 454 Py_DECREF(dest);
405 return NULL; 455 return NULL;
406 } 456 }
407 i++; 457 i++;
408 tuple = PyTuple_Pack(2, k, k->ob_type); 458 tuple = PyTuple_Pack(2, k, k->ob_type);
409 if (!tuple || PyDict_SetItem(dest, tuple, item) < 0) { 459 if (!tuple || PyDict_SetItem(dest, tuple, item) < 0) {
460 Py_DECREF(sorted_keys);
410 Py_DECREF(item); 461 Py_DECREF(item);
411 Py_DECREF(dest); 462 Py_DECREF(dest);
412 Py_XDECREF(tuple); 463 Py_XDECREF(tuple);
413 return NULL; 464 return NULL;
414 } 465 }
415 Py_DECREF(item); 466 Py_DECREF(item);
416 Py_DECREF(tuple); 467 Py_DECREF(tuple);
417 } 468 }
418 } 469 }
470 Py_DECREF(sorted_keys);
419 return dest; 471 return dest;
420 } 472 }
421 473
422 static void 474 static void
423 compiler_unit_check(struct compiler_unit *u) 475 compiler_unit_check(struct compiler_unit *u)
424 { 476 {
425 basicblock *block; 477 basicblock *block;
426 for (block = u->u_blocks; block != NULL; block = block->b_list) { 478 for (block = u->u_blocks; block != NULL; block = block->b_list) {
427 assert((void *)block != (void *)0xcbcbcbcb); 479 assert((void *)block != (void *)0xcbcbcbcb);
428 assert((void *)block != (void *)0xfbfbfbfb); 480 assert((void *)block != (void *)0xfbfbfbfb);
(...skipping 19 matching lines...) Expand all
448 b = u->u_blocks; 500 b = u->u_blocks;
449 while (b != NULL) { 501 while (b != NULL) {
450 if (b->b_instr) 502 if (b->b_instr)
451 PyObject_Free((void *)b->b_instr); 503 PyObject_Free((void *)b->b_instr);
452 next = b->b_list; 504 next = b->b_list;
453 PyObject_Free((void *)b); 505 PyObject_Free((void *)b);
454 b = next; 506 b = next;
455 } 507 }
456 Py_CLEAR(u->u_ste); 508 Py_CLEAR(u->u_ste);
457 Py_CLEAR(u->u_name); 509 Py_CLEAR(u->u_name);
510 Py_CLEAR(u->u_qualname);
458 Py_CLEAR(u->u_consts); 511 Py_CLEAR(u->u_consts);
459 Py_CLEAR(u->u_names); 512 Py_CLEAR(u->u_names);
460 Py_CLEAR(u->u_varnames); 513 Py_CLEAR(u->u_varnames);
461 Py_CLEAR(u->u_freevars); 514 Py_CLEAR(u->u_freevars);
462 Py_CLEAR(u->u_cellvars); 515 Py_CLEAR(u->u_cellvars);
463 Py_CLEAR(u->u_private); 516 Py_CLEAR(u->u_private);
464 PyObject_Free(u); 517 PyObject_Free(u);
465 } 518 }
466 519
467 static int 520 static int
468 compiler_enter_scope(struct compiler *c, identifier name, void *key, 521 compiler_enter_scope(struct compiler *c, identifier name,
469 int lineno) 522 int scope_type, void *key, int lineno)
470 { 523 {
471 struct compiler_unit *u; 524 struct compiler_unit *u;
472 525
473 u = (struct compiler_unit *)PyObject_Malloc(sizeof( 526 u = (struct compiler_unit *)PyObject_Malloc(sizeof(
474 struct compiler_unit)); 527 struct compiler_unit));
475 if (!u) { 528 if (!u) {
476 PyErr_NoMemory(); 529 PyErr_NoMemory();
477 return 0; 530 return 0;
478 } 531 }
479 memset(u, 0, sizeof(struct compiler_unit)); 532 memset(u, 0, sizeof(struct compiler_unit));
533 u->u_scope_type = scope_type;
480 u->u_argcount = 0; 534 u->u_argcount = 0;
481 u->u_kwonlyargcount = 0; 535 u->u_kwonlyargcount = 0;
482 u->u_ste = PySymtable_Lookup(c->c_st, key); 536 u->u_ste = PySymtable_Lookup(c->c_st, key);
483 if (!u->u_ste) { 537 if (!u->u_ste) {
484 compiler_unit_free(u); 538 compiler_unit_free(u);
485 return 0; 539 return 0;
486 } 540 }
487 Py_INCREF(name); 541 Py_INCREF(name);
488 u->u_name = name; 542 u->u_name = name;
489 u->u_varnames = list2dict(u->u_ste->ste_varnames); 543 u->u_varnames = list2dict(u->u_ste->ste_varnames);
490 u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0); 544 u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0);
491 if (!u->u_varnames || !u->u_cellvars) { 545 if (!u->u_varnames || !u->u_cellvars) {
492 compiler_unit_free(u); 546 compiler_unit_free(u);
493 return 0; 547 return 0;
548 }
549 if (u->u_ste->ste_needs_class_closure) {
550 /* Cook up a implicit __class__ cell. */
551 _Py_IDENTIFIER(__class__);
552 PyObject *tuple, *name, *zero;
553 int res;
554 assert(u->u_scope_type == COMPILER_SCOPE_CLASS);
555 assert(PyDict_Size(u->u_cellvars) == 0);
556 name = _PyUnicode_FromId(&PyId___class__);
557 if (!name) {
558 compiler_unit_free(u);
559 return 0;
560 }
561 tuple = PyTuple_Pack(2, name, Py_TYPE(name));
562 if (!tuple) {
563 compiler_unit_free(u);
564 return 0;
565 }
566 zero = PyLong_FromLong(0);
567 if (!zero) {
568 Py_DECREF(tuple);
569 compiler_unit_free(u);
570 return 0;
571 }
572 res = PyDict_SetItem(u->u_cellvars, tuple, zero);
573 Py_DECREF(tuple);
574 Py_DECREF(zero);
575 if (res < 0) {
576 compiler_unit_free(u);
577 return 0;
578 }
494 } 579 }
495 580
496 u->u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS, 581 u->u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS,
497 PyDict_Size(u->u_cellvars)); 582 PyDict_Size(u->u_cellvars));
498 if (!u->u_freevars) { 583 if (!u->u_freevars) {
499 compiler_unit_free(u); 584 compiler_unit_free(u);
500 return 0; 585 return 0;
501 } 586 }
502 587
503 u->u_blocks = NULL; 588 u->u_blocks = NULL;
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
557 /* we are deleting from a list so this really shouldn't fail */ 642 /* we are deleting from a list so this really shouldn't fail */
558 if (PySequence_DelItem(c->c_stack, n) < 0) 643 if (PySequence_DelItem(c->c_stack, n) < 0)
559 Py_FatalError("compiler_exit_scope()"); 644 Py_FatalError("compiler_exit_scope()");
560 compiler_unit_check(c->u); 645 compiler_unit_check(c->u);
561 } 646 }
562 else 647 else
563 c->u = NULL; 648 c->u = NULL;
564 649
565 } 650 }
566 651
652 static PyObject *
653 compiler_scope_qualname(struct compiler *c)
654 {
655 Py_ssize_t stack_size, i;
656 _Py_static_string(dot, ".");
657 _Py_static_string(locals, "<locals>");
658 struct compiler_unit *u;
659 PyObject *capsule, *name, *seq, *dot_str, *locals_str;
660
661 u = c->u;
662 if (u->u_qualname != NULL) {
663 Py_INCREF(u->u_qualname);
664 return u->u_qualname;
665 }
666
667 seq = PyList_New(0);
668 if (seq == NULL)
669 return NULL;
670
671 stack_size = PyList_GET_SIZE(c->c_stack);
672 for (i = 0; i < stack_size; i++) {
673 capsule = PyList_GET_ITEM(c->c_stack, i);
674 u = (struct compiler_unit *)PyCapsule_GetPointer(capsule, COMPILER_CAPSU LE_NAME_COMPILER_UNIT);
675 assert(u);
676 if (u->u_scope_type == COMPILER_SCOPE_MODULE)
677 continue;
678 if (PyList_Append(seq, u->u_name))
679 goto _error;
680 if (u->u_scope_type == COMPILER_SCOPE_FUNCTION) {
681 locals_str = _PyUnicode_FromId(&locals);
682 if (locals_str == NULL)
683 goto _error;
684 if (PyList_Append(seq, locals_str))
685 goto _error;
686 }
687 }
688 u = c->u;
689 if (PyList_Append(seq, u->u_name))
690 goto _error;
691 dot_str = _PyUnicode_FromId(&dot);
692 if (dot_str == NULL)
693 goto _error;
694 name = PyUnicode_Join(dot_str, seq);
695 Py_DECREF(seq);
696 u->u_qualname = name;
697 Py_XINCREF(name);
698 return name;
699
700 _error:
701 Py_XDECREF(seq);
702 return NULL;
703 }
704
567 /* Allocate a new block and return a pointer to it. 705 /* Allocate a new block and return a pointer to it.
568 Returns NULL on error. 706 Returns NULL on error.
569 */ 707 */
570 708
571 static basicblock * 709 static basicblock *
572 compiler_new_block(struct compiler *c) 710 compiler_new_block(struct compiler *c)
573 { 711 {
574 basicblock *b; 712 basicblock *b;
575 struct compiler_unit *u; 713 struct compiler_unit *u;
576 714
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after
759 case INPLACE_AND: 897 case INPLACE_AND:
760 case INPLACE_XOR: 898 case INPLACE_XOR:
761 case INPLACE_OR: 899 case INPLACE_OR:
762 return -1; 900 return -1;
763 case BREAK_LOOP: 901 case BREAK_LOOP:
764 return 0; 902 return 0;
765 case SETUP_WITH: 903 case SETUP_WITH:
766 return 7; 904 return 7;
767 case WITH_CLEANUP: 905 case WITH_CLEANUP:
768 return -1; /* XXX Sometimes more */ 906 return -1; /* XXX Sometimes more */
769 case STORE_LOCALS:
770 return -1;
771 case RETURN_VALUE: 907 case RETURN_VALUE:
772 return -1; 908 return -1;
773 case IMPORT_STAR: 909 case IMPORT_STAR:
774 return -1; 910 return -1;
775 case YIELD_VALUE: 911 case YIELD_VALUE:
776 return 0; 912 return 0;
777 913 case YIELD_FROM:
914 return -1;
778 case POP_BLOCK: 915 case POP_BLOCK:
779 return 0; 916 return 0;
780 case POP_EXCEPT: 917 case POP_EXCEPT:
781 return 0; /* -3 except if bad bytecode */ 918 return 0; /* -3 except if bad bytecode */
782 case END_FINALLY: 919 case END_FINALLY:
783 return -1; /* or -2 or -3 if exception occurred */ 920 return -1; /* or -2 or -3 if exception occurred */
784 921
785 case STORE_NAME: 922 case STORE_NAME:
786 return -1; 923 return -1;
787 case DELETE_NAME: 924 case DELETE_NAME:
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
853 return -oparg; 990 return -oparg;
854 #define NARGS(o) (((o) % 256) + 2*(((o) / 256) % 256)) 991 #define NARGS(o) (((o) % 256) + 2*(((o) / 256) % 256))
855 case CALL_FUNCTION: 992 case CALL_FUNCTION:
856 return -NARGS(oparg); 993 return -NARGS(oparg);
857 case CALL_FUNCTION_VAR: 994 case CALL_FUNCTION_VAR:
858 case CALL_FUNCTION_KW: 995 case CALL_FUNCTION_KW:
859 return -NARGS(oparg)-1; 996 return -NARGS(oparg)-1;
860 case CALL_FUNCTION_VAR_KW: 997 case CALL_FUNCTION_VAR_KW:
861 return -NARGS(oparg)-2; 998 return -NARGS(oparg)-2;
862 case MAKE_FUNCTION: 999 case MAKE_FUNCTION:
863 return -NARGS(oparg) - ((oparg >> 16) & 0xffff); 1000 return -1 -NARGS(oparg) - ((oparg >> 16) & 0xffff);
864 case MAKE_CLOSURE: 1001 case MAKE_CLOSURE:
865 return -1 - NARGS(oparg) - ((oparg >> 16) & 0xffff); 1002 return -2 - NARGS(oparg) - ((oparg >> 16) & 0xffff);
866 #undef NARGS 1003 #undef NARGS
867 case BUILD_SLICE: 1004 case BUILD_SLICE:
868 if (oparg == 3) 1005 if (oparg == 3)
869 return -2; 1006 return -2;
870 else 1007 else
871 return -1; 1008 return -1;
872 1009
873 case LOAD_CLOSURE: 1010 case LOAD_CLOSURE:
874 return 1; 1011 return 1;
875 case LOAD_DEREF: 1012 case LOAD_DEREF:
1013 case LOAD_CLASSDEREF:
876 return 1; 1014 return 1;
877 case STORE_DEREF: 1015 case STORE_DEREF:
878 return -1; 1016 return -1;
879 case DELETE_DEREF: 1017 case DELETE_DEREF:
880 return 0; 1018 return 0;
881 default: 1019 default:
882 fprintf(stderr, "opcode = %d\n", opcode); 1020 fprintf(stderr, "opcode = %d\n", opcode);
883 Py_FatalError("opcode_stack_effect()"); 1021 Py_FatalError("opcode_stack_effect()");
884 1022
885 } 1023 }
(...skipping 299 matching lines...) Expand 10 before | Expand all | Expand 10 after
1185 { 1323 {
1186 PyCodeObject *co; 1324 PyCodeObject *co;
1187 int addNone = 1; 1325 int addNone = 1;
1188 static PyObject *module; 1326 static PyObject *module;
1189 if (!module) { 1327 if (!module) {
1190 module = PyUnicode_InternFromString("<module>"); 1328 module = PyUnicode_InternFromString("<module>");
1191 if (!module) 1329 if (!module)
1192 return NULL; 1330 return NULL;
1193 } 1331 }
1194 /* Use 0 for firstlineno initially, will fixup in assemble(). */ 1332 /* Use 0 for firstlineno initially, will fixup in assemble(). */
1195 if (!compiler_enter_scope(c, module, mod, 0)) 1333 if (!compiler_enter_scope(c, module, COMPILER_SCOPE_MODULE, mod, 0))
1196 return NULL; 1334 return NULL;
1197 switch (mod->kind) { 1335 switch (mod->kind) {
1198 case Module_kind: 1336 case Module_kind:
1199 if (!compiler_body(c, mod->v.Module.body)) { 1337 if (!compiler_body(c, mod->v.Module.body)) {
1200 compiler_exit_scope(c); 1338 compiler_exit_scope(c);
1201 return 0; 1339 return 0;
1202 } 1340 }
1203 break; 1341 break;
1204 case Interactive_kind: 1342 case Interactive_kind:
1205 c->c_interactive = 1; 1343 c->c_interactive = 1;
(...skipping 20 matching lines...) Expand all
1226 } 1364 }
1227 1365
1228 /* The test for LOCAL must come before the test for FREE in order to 1366 /* The test for LOCAL must come before the test for FREE in order to
1229 handle classes where name is both local and free. The local var is 1367 handle classes where name is both local and free. The local var is
1230 a method and the free var is a free var referenced within a method. 1368 a method and the free var is a free var referenced within a method.
1231 */ 1369 */
1232 1370
1233 static int 1371 static int
1234 get_ref_type(struct compiler *c, PyObject *name) 1372 get_ref_type(struct compiler *c, PyObject *name)
1235 { 1373 {
1236 int scope = PyST_GetScope(c->u->u_ste, name); 1374 int scope;
1375 if (c->u->u_scope_type == COMPILER_SCOPE_CLASS &&
1376 !PyUnicode_CompareWithASCIIString(name, "__class__"))
1377 return CELL;
1378 scope = PyST_GetScope(c->u->u_ste, name);
1237 if (scope == 0) { 1379 if (scope == 0) {
1238 char buf[350]; 1380 char buf[350];
1239 PyOS_snprintf(buf, sizeof(buf), 1381 PyOS_snprintf(buf, sizeof(buf),
1240 "unknown scope for %.100s in %.100s(%s)\n" 1382 "unknown scope for %.100s in %.100s(%s)\n"
1241 "symbols: %s\nlocals: %s\nglobals: %s", 1383 "symbols: %s\nlocals: %s\nglobals: %s",
1242 PyBytes_AS_STRING(name), 1384 PyBytes_AS_STRING(name),
1243 PyBytes_AS_STRING(c->u->u_name), 1385 PyBytes_AS_STRING(c->u->u_name),
1244 PyObject_REPR(c->u->u_ste->ste_id), 1386 PyObject_REPR(c->u->u_ste->ste_id),
1245 PyObject_REPR(c->u->u_ste->ste_symbols), 1387 PyObject_REPR(c->u->u_ste->ste_symbols),
1246 PyObject_REPR(c->u->u_varnames), 1388 PyObject_REPR(c->u->u_varnames),
(...skipping 13 matching lines...) Expand all
1260 if (k == NULL) 1402 if (k == NULL)
1261 return -1; 1403 return -1;
1262 v = PyDict_GetItem(dict, k); 1404 v = PyDict_GetItem(dict, k);
1263 Py_DECREF(k); 1405 Py_DECREF(k);
1264 if (v == NULL) 1406 if (v == NULL)
1265 return -1; 1407 return -1;
1266 return PyLong_AS_LONG(v); 1408 return PyLong_AS_LONG(v);
1267 } 1409 }
1268 1410
1269 static int 1411 static int
1270 compiler_make_closure(struct compiler *c, PyCodeObject *co, int args) 1412 compiler_make_closure(struct compiler *c, PyCodeObject *co, int args, PyObject * qualname)
1271 { 1413 {
1272 int i, free = PyCode_GetNumFree(co); 1414 int i, free = PyCode_GetNumFree(co);
1415 if (qualname == NULL)
1416 qualname = co->co_name;
1417
1273 if (free == 0) { 1418 if (free == 0) {
1274 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts); 1419 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
1420 ADDOP_O(c, LOAD_CONST, qualname, consts);
1275 ADDOP_I(c, MAKE_FUNCTION, args); 1421 ADDOP_I(c, MAKE_FUNCTION, args);
1276 return 1; 1422 return 1;
1277 } 1423 }
1278 for (i = 0; i < free; ++i) { 1424 for (i = 0; i < free; ++i) {
1279 /* Bypass com_addop_varname because it will generate 1425 /* Bypass com_addop_varname because it will generate
1280 LOAD_DEREF but LOAD_CLOSURE is needed. 1426 LOAD_DEREF but LOAD_CLOSURE is needed.
1281 */ 1427 */
1282 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i); 1428 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
1283 int arg, reftype; 1429 int arg, reftype;
1284 1430
(...skipping 16 matching lines...) Expand all
1301 PyBytes_AS_STRING(c->u->u_name), 1447 PyBytes_AS_STRING(c->u->u_name),
1302 reftype, arg, 1448 reftype, arg,
1303 _PyUnicode_AsString(co->co_name), 1449 _PyUnicode_AsString(co->co_name),
1304 PyObject_REPR(co->co_freevars)); 1450 PyObject_REPR(co->co_freevars));
1305 Py_FatalError("compiler_make_closure()"); 1451 Py_FatalError("compiler_make_closure()");
1306 } 1452 }
1307 ADDOP_I(c, LOAD_CLOSURE, arg); 1453 ADDOP_I(c, LOAD_CLOSURE, arg);
1308 } 1454 }
1309 ADDOP_I(c, BUILD_TUPLE, free); 1455 ADDOP_I(c, BUILD_TUPLE, free);
1310 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts); 1456 ADDOP_O(c, LOAD_CONST, (PyObject*)co, consts);
1457 ADDOP_O(c, LOAD_CONST, qualname, consts);
1311 ADDOP_I(c, MAKE_CLOSURE, args); 1458 ADDOP_I(c, MAKE_CLOSURE, args);
1312 return 1; 1459 return 1;
1313 } 1460 }
1314 1461
1315 static int 1462 static int
1316 compiler_decorators(struct compiler *c, asdl_seq* decos) 1463 compiler_decorators(struct compiler *c, asdl_seq* decos)
1317 { 1464 {
1318 int i; 1465 int i;
1319 1466
1320 if (!decos) 1467 if (!decos)
1321 return 1; 1468 return 1;
1322 1469
1323 for (i = 0; i < asdl_seq_LEN(decos); i++) { 1470 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1324 VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i)); 1471 VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i));
1325 } 1472 }
1326 return 1; 1473 return 1;
1327 } 1474 }
1328 1475
1329 static int 1476 static int
1330 compiler_visit_kwonlydefaults(struct compiler *c, asdl_seq *kwonlyargs, 1477 compiler_visit_kwonlydefaults(struct compiler *c, asdl_seq *kwonlyargs,
1331 asdl_seq *kw_defaults) 1478 asdl_seq *kw_defaults)
1332 { 1479 {
1333 int i, default_count = 0; 1480 int i, default_count = 0;
1334 for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) { 1481 for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) {
1335 arg_ty arg = asdl_seq_GET(kwonlyargs, i); 1482 arg_ty arg = asdl_seq_GET(kwonlyargs, i);
1336 expr_ty default_ = asdl_seq_GET(kw_defaults, i); 1483 expr_ty default_ = asdl_seq_GET(kw_defaults, i);
1337 if (default_) { 1484 if (default_) {
1338 ADDOP_O(c, LOAD_CONST, arg->arg, consts); 1485 PyObject *mangled = _Py_Mangle(c->u->u_private, arg->arg);
1486 if (!mangled)
1487 return -1;
1488 ADDOP_O(c, LOAD_CONST, mangled, consts);
1489 Py_DECREF(mangled);
1339 if (!compiler_visit_expr(c, default_)) { 1490 if (!compiler_visit_expr(c, default_)) {
1340 return -1; 1491 return -1;
1341 } 1492 }
1342 default_count++; 1493 default_count++;
1343 } 1494 }
1344 } 1495 }
1345 return default_count; 1496 return default_count;
1346 } 1497 }
1347 1498
1348 static int 1499 static int
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
1387 */ 1538 */
1388 static identifier return_str; 1539 static identifier return_str;
1389 PyObject *names; 1540 PyObject *names;
1390 int len; 1541 int len;
1391 names = PyList_New(0); 1542 names = PyList_New(0);
1392 if (!names) 1543 if (!names)
1393 return -1; 1544 return -1;
1394 1545
1395 if (compiler_visit_argannotations(c, args->args, names)) 1546 if (compiler_visit_argannotations(c, args->args, names))
1396 goto error; 1547 goto error;
1397 if (args->varargannotation && 1548 if (args->vararg && args->vararg->annotation &&
1398 compiler_visit_argannotation(c, args->vararg, 1549 compiler_visit_argannotation(c, args->vararg->arg,
1399 args->varargannotation, names)) 1550 args->vararg->annotation, names))
1400 goto error; 1551 goto error;
1401 if (compiler_visit_argannotations(c, args->kwonlyargs, names)) 1552 if (compiler_visit_argannotations(c, args->kwonlyargs, names))
1402 goto error; 1553 goto error;
1403 if (args->kwargannotation && 1554 if (args->kwarg && args->kwarg->annotation &&
1404 compiler_visit_argannotation(c, args->kwarg, 1555 compiler_visit_argannotation(c, args->kwarg->arg,
1405 args->kwargannotation, names)) 1556 args->kwarg->annotation, names))
1406 goto error; 1557 goto error;
1407 1558
1408 if (!return_str) { 1559 if (!return_str) {
1409 return_str = PyUnicode_InternFromString("return"); 1560 return_str = PyUnicode_InternFromString("return");
1410 if (!return_str) 1561 if (!return_str)
1411 goto error; 1562 goto error;
1412 } 1563 }
1413 if (compiler_visit_argannotation(c, return_str, returns, names)) { 1564 if (compiler_visit_argannotation(c, return_str, returns, names)) {
1414 goto error; 1565 goto error;
1415 } 1566 }
(...skipping 26 matching lines...) Expand all
1442 1593
1443 error: 1594 error:
1444 Py_DECREF(names); 1595 Py_DECREF(names);
1445 return -1; 1596 return -1;
1446 } 1597 }
1447 1598
1448 static int 1599 static int
1449 compiler_function(struct compiler *c, stmt_ty s) 1600 compiler_function(struct compiler *c, stmt_ty s)
1450 { 1601 {
1451 PyCodeObject *co; 1602 PyCodeObject *co;
1452 PyObject *first_const = Py_None; 1603 PyObject *qualname, *first_const = Py_None;
1453 arguments_ty args = s->v.FunctionDef.args; 1604 arguments_ty args = s->v.FunctionDef.args;
1454 expr_ty returns = s->v.FunctionDef.returns; 1605 expr_ty returns = s->v.FunctionDef.returns;
1455 asdl_seq* decos = s->v.FunctionDef.decorator_list; 1606 asdl_seq* decos = s->v.FunctionDef.decorator_list;
1456 stmt_ty st; 1607 stmt_ty st;
1457 int i, n, docstring, kw_default_count = 0, arglength; 1608 int i, n, docstring, kw_default_count = 0, arglength;
1458 int num_annotations; 1609 int num_annotations;
1459 1610
1460 assert(s->kind == FunctionDef_kind); 1611 assert(s->kind == FunctionDef_kind);
1461 1612
1462 if (!compiler_decorators(c, decos)) 1613 if (!compiler_decorators(c, decos))
1463 return 0; 1614 return 0;
1615 if (args->defaults)
1616 VISIT_SEQ(c, expr, args->defaults);
1464 if (args->kwonlyargs) { 1617 if (args->kwonlyargs) {
1465 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs, 1618 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
1466 args->kw_defaults); 1619 args->kw_defaults);
1467 if (res < 0) 1620 if (res < 0)
1468 return 0; 1621 return 0;
1469 kw_default_count = res; 1622 kw_default_count = res;
1470 } 1623 }
1471 if (args->defaults)
1472 VISIT_SEQ(c, expr, args->defaults);
1473 num_annotations = compiler_visit_annotations(c, args, returns); 1624 num_annotations = compiler_visit_annotations(c, args, returns);
1474 if (num_annotations < 0) 1625 if (num_annotations < 0)
1475 return 0; 1626 return 0;
1476 assert((num_annotations & 0xFFFF) == num_annotations); 1627 assert((num_annotations & 0xFFFF) == num_annotations);
1477 1628
1478 if (!compiler_enter_scope(c, s->v.FunctionDef.name, (void *)s, 1629 if (!compiler_enter_scope(c, s->v.FunctionDef.name,
1630 COMPILER_SCOPE_FUNCTION, (void *)s,
1479 s->lineno)) 1631 s->lineno))
1480 return 0; 1632 return 0;
1481 1633
1482 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, 0); 1634 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, 0);
1483 docstring = compiler_isdocstring(st); 1635 docstring = compiler_isdocstring(st);
1484 if (docstring && c->c_optimize < 2) 1636 if (docstring && c->c_optimize < 2)
1485 first_const = st->v.Expr.value->v.Str.s; 1637 first_const = st->v.Expr.value->v.Str.s;
1486 if (compiler_add_o(c, c->u->u_consts, first_const) < 0) { 1638 if (compiler_add_o(c, c->u->u_consts, first_const) < 0) {
1487 compiler_exit_scope(c); 1639 compiler_exit_scope(c);
1488 return 0; 1640 return 0;
1489 } 1641 }
1490 1642
1491 c->u->u_argcount = asdl_seq_LEN(args->args); 1643 c->u->u_argcount = asdl_seq_LEN(args->args);
1492 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs); 1644 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
1493 n = asdl_seq_LEN(s->v.FunctionDef.body); 1645 n = asdl_seq_LEN(s->v.FunctionDef.body);
1494 /* if there was a docstring, we need to skip the first statement */ 1646 /* if there was a docstring, we need to skip the first statement */
1495 for (i = docstring; i < n; i++) { 1647 for (i = docstring; i < n; i++) {
1496 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, i); 1648 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, i);
1497 VISIT_IN_SCOPE(c, stmt, st); 1649 VISIT_IN_SCOPE(c, stmt, st);
1498 } 1650 }
1499 co = assemble(c, 1); 1651 co = assemble(c, 1);
1652 qualname = compiler_scope_qualname(c);
1500 compiler_exit_scope(c); 1653 compiler_exit_scope(c);
1501 if (co == NULL) 1654 if (qualname == NULL || co == NULL) {
1502 return 0; 1655 Py_XDECREF(qualname);
1656 Py_XDECREF(co);
1657 return 0;
1658 }
1503 1659
1504 arglength = asdl_seq_LEN(args->defaults); 1660 arglength = asdl_seq_LEN(args->defaults);
1505 arglength |= kw_default_count << 8; 1661 arglength |= kw_default_count << 8;
1506 arglength |= num_annotations << 16; 1662 arglength |= num_annotations << 16;
1507 compiler_make_closure(c, co, arglength); 1663 compiler_make_closure(c, co, arglength, qualname);
1664 Py_DECREF(qualname);
1508 Py_DECREF(co); 1665 Py_DECREF(co);
1509 1666
1510 /* decorators */ 1667 /* decorators */
1511 for (i = 0; i < asdl_seq_LEN(decos); i++) { 1668 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1512 ADDOP_I(c, CALL_FUNCTION, 1); 1669 ADDOP_I(c, CALL_FUNCTION, 1);
1513 } 1670 }
1514 1671
1515 return compiler_nameop(c, s->v.FunctionDef.name, Store); 1672 return compiler_nameop(c, s->v.FunctionDef.name, Store);
1516 } 1673 }
1517 1674
(...skipping 14 matching lines...) Expand all
1532 <func> is a function/closure created from the class body; 1689 <func> is a function/closure created from the class body;
1533 it has a single argument (__locals__) where the dict 1690 it has a single argument (__locals__) where the dict
1534 (or MutableSequence) representing the locals is passed 1691 (or MutableSequence) representing the locals is passed
1535 <name> is the class name 1692 <name> is the class name
1536 <bases> is the positional arguments and *varargs argument 1693 <bases> is the positional arguments and *varargs argument
1537 <keywords> is the keyword arguments and **kwds argument 1694 <keywords> is the keyword arguments and **kwds argument
1538 This borrows from compiler_call. 1695 This borrows from compiler_call.
1539 */ 1696 */
1540 1697
1541 /* 1. compile the class body into a code object */ 1698 /* 1. compile the class body into a code object */
1542 if (!compiler_enter_scope(c, s->v.ClassDef.name, (void *)s, s->lineno)) 1699 if (!compiler_enter_scope(c, s->v.ClassDef.name,
1700 COMPILER_SCOPE_CLASS, (void *)s, s->lineno))
1543 return 0; 1701 return 0;
1544 /* this block represents what we do in the new scope */ 1702 /* this block represents what we do in the new scope */
1545 { 1703 {
1546 /* use the class name for name mangling */ 1704 /* use the class name for name mangling */
1547 Py_INCREF(s->v.ClassDef.name); 1705 Py_INCREF(s->v.ClassDef.name);
1548 Py_XDECREF(c->u->u_private); 1706 Py_XDECREF(c->u->u_private);
1549 c->u->u_private = s->v.ClassDef.name; 1707 c->u->u_private = s->v.ClassDef.name;
1550 /* force it to have one mandatory argument */
1551 c->u->u_argcount = 1;
1552 /* load the first argument (__locals__) ... */
1553 ADDOP_I(c, LOAD_FAST, 0);
1554 /* ... and store it into f_locals */
1555 ADDOP_IN_SCOPE(c, STORE_LOCALS);
1556 /* load (global) __name__ ... */ 1708 /* load (global) __name__ ... */
1557 str = PyUnicode_InternFromString("__name__"); 1709 str = PyUnicode_InternFromString("__name__");
1558 if (!str || !compiler_nameop(c, str, Load)) { 1710 if (!str || !compiler_nameop(c, str, Load)) {
1559 Py_XDECREF(str); 1711 Py_XDECREF(str);
1560 compiler_exit_scope(c); 1712 compiler_exit_scope(c);
1561 return 0; 1713 return 0;
1562 } 1714 }
1563 Py_DECREF(str); 1715 Py_DECREF(str);
1564 /* ... and store it as __module__ */ 1716 /* ... and store it as __module__ */
1565 str = PyUnicode_InternFromString("__module__"); 1717 str = PyUnicode_InternFromString("__module__");
1566 if (!str || !compiler_nameop(c, str, Store)) { 1718 if (!str || !compiler_nameop(c, str, Store)) {
1567 Py_XDECREF(str); 1719 Py_XDECREF(str);
1568 compiler_exit_scope(c); 1720 compiler_exit_scope(c);
1569 return 0; 1721 return 0;
1570 } 1722 }
1571 Py_DECREF(str); 1723 Py_DECREF(str);
1724 /* store the __qualname__ */
1725 str = compiler_scope_qualname(c);
1726 if (!str) {
1727 compiler_exit_scope(c);
1728 return 0;
1729 }
1730 ADDOP_O(c, LOAD_CONST, str, consts);
1731 Py_DECREF(str);
1732 str = PyUnicode_InternFromString("__qualname__");
1733 if (!str || !compiler_nameop(c, str, Store)) {
1734 Py_XDECREF(str);
1735 compiler_exit_scope(c);
1736 return 0;
1737 }
1738 Py_DECREF(str);
1572 /* compile the body proper */ 1739 /* compile the body proper */
1573 if (!compiler_body(c, s->v.ClassDef.body)) { 1740 if (!compiler_body(c, s->v.ClassDef.body)) {
1574 compiler_exit_scope(c); 1741 compiler_exit_scope(c);
1575 return 0; 1742 return 0;
1576 } 1743 }
1577 /* return the (empty) __class__ cell */ 1744 if (c->u->u_ste->ste_needs_class_closure) {
1578 str = PyUnicode_InternFromString("__class__"); 1745 /* return the (empty) __class__ cell */
1579 if (str == NULL) { 1746 str = PyUnicode_InternFromString("__class__");
1580 compiler_exit_scope(c); 1747 if (str == NULL) {
1581 return 0; 1748 compiler_exit_scope(c);
1582 } 1749 return 0;
1583 i = compiler_lookup_arg(c->u->u_cellvars, str); 1750 }
1584 Py_DECREF(str); 1751 i = compiler_lookup_arg(c->u->u_cellvars, str);
1585 if (i == -1) { 1752 Py_DECREF(str);
1586 /* This happens when nobody references the cell */ 1753 assert(i == 0);
1587 PyErr_Clear();
1588 /* Return None */
1589 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1590 }
1591 else {
1592 /* Return the cell where to store __class__ */ 1754 /* Return the cell where to store __class__ */
1593 ADDOP_I(c, LOAD_CLOSURE, i); 1755 ADDOP_I(c, LOAD_CLOSURE, i);
1756 }
1757 else {
1758 assert(PyDict_Size(c->u->u_cellvars) == 0);
1759 /* This happens when nobody references the cell. Return None. */
1760 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1594 } 1761 }
1595 ADDOP_IN_SCOPE(c, RETURN_VALUE); 1762 ADDOP_IN_SCOPE(c, RETURN_VALUE);
1596 /* create the code object */ 1763 /* create the code object */
1597 co = assemble(c, 1); 1764 co = assemble(c, 1);
1598 } 1765 }
1599 /* leave the new scope */ 1766 /* leave the new scope */
1600 compiler_exit_scope(c); 1767 compiler_exit_scope(c);
1601 if (co == NULL) 1768 if (co == NULL)
1602 return 0; 1769 return 0;
1603 1770
1604 /* 2. load the 'build_class' function */ 1771 /* 2. load the 'build_class' function */
1605 ADDOP(c, LOAD_BUILD_CLASS); 1772 ADDOP(c, LOAD_BUILD_CLASS);
1606 1773
1607 /* 3. load a function (or closure) made from the code object */ 1774 /* 3. load a function (or closure) made from the code object */
1608 compiler_make_closure(c, co, 0); 1775 compiler_make_closure(c, co, 0, NULL);
1609 Py_DECREF(co); 1776 Py_DECREF(co);
1610 1777
1611 /* 4. load class name */ 1778 /* 4. load class name */
1612 ADDOP_O(c, LOAD_CONST, s->v.ClassDef.name, consts); 1779 ADDOP_O(c, LOAD_CONST, s->v.ClassDef.name, consts);
1613 1780
1614 /* 5. generate the rest of the code for the call */ 1781 /* 5. generate the rest of the code for the call */
1615 if (!compiler_call_helper(c, 2, 1782 if (!compiler_call_helper(c, 2,
1616 s->v.ClassDef.bases, 1783 s->v.ClassDef.bases,
1617 s->v.ClassDef.keywords, 1784 s->v.ClassDef.keywords,
1618 s->v.ClassDef.starargs, 1785 s->v.ClassDef.starargs,
(...skipping 30 matching lines...) Expand all
1649 compiler_use_next_block(c, next); 1816 compiler_use_next_block(c, next);
1650 VISIT(c, expr, e->v.IfExp.orelse); 1817 VISIT(c, expr, e->v.IfExp.orelse);
1651 compiler_use_next_block(c, end); 1818 compiler_use_next_block(c, end);
1652 return 1; 1819 return 1;
1653 } 1820 }
1654 1821
1655 static int 1822 static int
1656 compiler_lambda(struct compiler *c, expr_ty e) 1823 compiler_lambda(struct compiler *c, expr_ty e)
1657 { 1824 {
1658 PyCodeObject *co; 1825 PyCodeObject *co;
1826 PyObject *qualname;
1659 static identifier name; 1827 static identifier name;
1660 int kw_default_count = 0, arglength; 1828 int kw_default_count = 0, arglength;
1661 arguments_ty args = e->v.Lambda.args; 1829 arguments_ty args = e->v.Lambda.args;
1662 assert(e->kind == Lambda_kind); 1830 assert(e->kind == Lambda_kind);
1663 1831
1664 if (!name) { 1832 if (!name) {
1665 name = PyUnicode_InternFromString("<lambda>"); 1833 name = PyUnicode_InternFromString("<lambda>");
1666 if (!name) 1834 if (!name)
1667 return 0; 1835 return 0;
1668 } 1836 }
1669 1837
1838 if (args->defaults)
1839 VISIT_SEQ(c, expr, args->defaults);
1670 if (args->kwonlyargs) { 1840 if (args->kwonlyargs) {
1671 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs, 1841 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
1672 args->kw_defaults); 1842 args->kw_defaults);
1673 if (res < 0) return 0; 1843 if (res < 0) return 0;
1674 kw_default_count = res; 1844 kw_default_count = res;
1675 } 1845 }
1676 if (args->defaults) 1846 if (!compiler_enter_scope(c, name, COMPILER_SCOPE_FUNCTION,
1677 VISIT_SEQ(c, expr, args->defaults); 1847 (void *)e, e->lineno))
1678 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
1679 return 0; 1848 return 0;
1680 1849
1681 /* Make None the first constant, so the lambda can't have a 1850 /* Make None the first constant, so the lambda can't have a
1682 docstring. */ 1851 docstring. */
1683 if (compiler_add_o(c, c->u->u_consts, Py_None) < 0) 1852 if (compiler_add_o(c, c->u->u_consts, Py_None) < 0)
1684 return 0; 1853 return 0;
1685 1854
1686 c->u->u_argcount = asdl_seq_LEN(args->args); 1855 c->u->u_argcount = asdl_seq_LEN(args->args);
1687 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs); 1856 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
1688 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body); 1857 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
1689 if (c->u->u_ste->ste_generator) { 1858 if (c->u->u_ste->ste_generator) {
1690 ADDOP_IN_SCOPE(c, POP_TOP); 1859 ADDOP_IN_SCOPE(c, POP_TOP);
1691 } 1860 }
1692 else { 1861 else {
1693 ADDOP_IN_SCOPE(c, RETURN_VALUE); 1862 ADDOP_IN_SCOPE(c, RETURN_VALUE);
1694 } 1863 }
1695 co = assemble(c, 1); 1864 co = assemble(c, 1);
1865 qualname = compiler_scope_qualname(c);
1696 compiler_exit_scope(c); 1866 compiler_exit_scope(c);
1697 if (co == NULL) 1867 if (qualname == NULL || co == NULL)
1698 return 0; 1868 return 0;
1699 1869
1700 arglength = asdl_seq_LEN(args->defaults); 1870 arglength = asdl_seq_LEN(args->defaults);
1701 arglength |= kw_default_count << 8; 1871 arglength |= kw_default_count << 8;
1702 compiler_make_closure(c, co, arglength); 1872 compiler_make_closure(c, co, arglength, qualname);
1873 Py_DECREF(qualname);
1703 Py_DECREF(co); 1874 Py_DECREF(co);
1704 1875
1705 return 1; 1876 return 1;
1706 } 1877 }
1707 1878
1708 static int 1879 static int
1709 compiler_if(struct compiler *c, stmt_ty s) 1880 compiler_if(struct compiler *c, stmt_ty s)
1710 { 1881 {
1711 basicblock *end, *next; 1882 basicblock *end, *next;
1712 int constant; 1883 int constant;
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after
1901 basicblock *body, *end; 2072 basicblock *body, *end;
1902 body = compiler_new_block(c); 2073 body = compiler_new_block(c);
1903 end = compiler_new_block(c); 2074 end = compiler_new_block(c);
1904 if (body == NULL || end == NULL) 2075 if (body == NULL || end == NULL)
1905 return 0; 2076 return 0;
1906 2077
1907 ADDOP_JREL(c, SETUP_FINALLY, end); 2078 ADDOP_JREL(c, SETUP_FINALLY, end);
1908 compiler_use_next_block(c, body); 2079 compiler_use_next_block(c, body);
1909 if (!compiler_push_fblock(c, FINALLY_TRY, body)) 2080 if (!compiler_push_fblock(c, FINALLY_TRY, body))
1910 return 0; 2081 return 0;
1911 VISIT_SEQ(c, stmt, s->v.TryFinally.body); 2082 if (s->v.Try.handlers && asdl_seq_LEN(s->v.Try.handlers)) {
2083 if (!compiler_try_except(c, s))
2084 return 0;
2085 }
2086 else {
2087 VISIT_SEQ(c, stmt, s->v.Try.body);
2088 }
1912 ADDOP(c, POP_BLOCK); 2089 ADDOP(c, POP_BLOCK);
1913 compiler_pop_fblock(c, FINALLY_TRY, body); 2090 compiler_pop_fblock(c, FINALLY_TRY, body);
1914 2091
1915 ADDOP_O(c, LOAD_CONST, Py_None, consts); 2092 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1916 compiler_use_next_block(c, end); 2093 compiler_use_next_block(c, end);
1917 if (!compiler_push_fblock(c, FINALLY_END, end)) 2094 if (!compiler_push_fblock(c, FINALLY_END, end))
1918 return 0; 2095 return 0;
1919 VISIT_SEQ(c, stmt, s->v.TryFinally.finalbody); 2096 VISIT_SEQ(c, stmt, s->v.Try.finalbody);
1920 ADDOP(c, END_FINALLY); 2097 ADDOP(c, END_FINALLY);
1921 compiler_pop_fblock(c, FINALLY_END, end); 2098 compiler_pop_fblock(c, FINALLY_END, end);
1922 2099
1923 return 1; 2100 return 1;
1924 } 2101 }
1925 2102
1926 /* 2103 /*
1927 Code generated for "try: S except E1 as V1: S1 except E2 as V2: S2 ...": 2104 Code generated for "try: S except E1 as V1: S1 except E2 as V2: S2 ...":
1928 (The contents of the value stack is shown in [], with the top 2105 (The contents of the value stack is shown in [], with the top
1929 at the right; 'tb' is trace-back info, 'val' the exception's 2106 at the right; 'tb' is trace-back info, 'val' the exception's
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
1963 body = compiler_new_block(c); 2140 body = compiler_new_block(c);
1964 except = compiler_new_block(c); 2141 except = compiler_new_block(c);
1965 orelse = compiler_new_block(c); 2142 orelse = compiler_new_block(c);
1966 end = compiler_new_block(c); 2143 end = compiler_new_block(c);
1967 if (body == NULL || except == NULL || orelse == NULL || end == NULL) 2144 if (body == NULL || except == NULL || orelse == NULL || end == NULL)
1968 return 0; 2145 return 0;
1969 ADDOP_JREL(c, SETUP_EXCEPT, except); 2146 ADDOP_JREL(c, SETUP_EXCEPT, except);
1970 compiler_use_next_block(c, body); 2147 compiler_use_next_block(c, body);
1971 if (!compiler_push_fblock(c, EXCEPT, body)) 2148 if (!compiler_push_fblock(c, EXCEPT, body))
1972 return 0; 2149 return 0;
1973 VISIT_SEQ(c, stmt, s->v.TryExcept.body); 2150 VISIT_SEQ(c, stmt, s->v.Try.body);
1974 ADDOP(c, POP_BLOCK); 2151 ADDOP(c, POP_BLOCK);
1975 compiler_pop_fblock(c, EXCEPT, body); 2152 compiler_pop_fblock(c, EXCEPT, body);
1976 ADDOP_JREL(c, JUMP_FORWARD, orelse); 2153 ADDOP_JREL(c, JUMP_FORWARD, orelse);
1977 n = asdl_seq_LEN(s->v.TryExcept.handlers); 2154 n = asdl_seq_LEN(s->v.Try.handlers);
1978 compiler_use_next_block(c, except); 2155 compiler_use_next_block(c, except);
1979 for (i = 0; i < n; i++) { 2156 for (i = 0; i < n; i++) {
1980 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET( 2157 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
1981 s->v.TryExcept.handlers, i); 2158 s->v.Try.handlers, i);
1982 if (!handler->v.ExceptHandler.type && i < n-1) 2159 if (!handler->v.ExceptHandler.type && i < n-1)
1983 return compiler_error(c, "default 'except:' must be last"); 2160 return compiler_error(c, "default 'except:' must be last");
1984 c->u->u_lineno_set = 0; 2161 c->u->u_lineno_set = 0;
1985 c->u->u_lineno = handler->lineno; 2162 c->u->u_lineno = handler->lineno;
1986 c->u->u_col_offset = handler->col_offset; 2163 c->u->u_col_offset = handler->col_offset;
1987 except = compiler_new_block(c); 2164 except = compiler_new_block(c);
1988 if (except == NULL) 2165 if (except == NULL)
1989 return 0; 2166 return 0;
1990 if (handler->v.ExceptHandler.type) { 2167 if (handler->v.ExceptHandler.type) {
1991 ADDOP(c, DUP_TOP); 2168 ADDOP(c, DUP_TOP);
1992 VISIT(c, expr, handler->v.ExceptHandler.type); 2169 VISIT(c, expr, handler->v.ExceptHandler.type);
1993 ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH); 2170 ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH);
1994 ADDOP_JABS(c, POP_JUMP_IF_FALSE, except); 2171 ADDOP_JABS(c, POP_JUMP_IF_FALSE, except);
1995 } 2172 }
1996 ADDOP(c, POP_TOP); 2173 ADDOP(c, POP_TOP);
1997 if (handler->v.ExceptHandler.name) { 2174 if (handler->v.ExceptHandler.name) {
1998 basicblock *cleanup_end, *cleanup_body; 2175 basicblock *cleanup_end, *cleanup_body;
1999 2176
2000 cleanup_end = compiler_new_block(c); 2177 cleanup_end = compiler_new_block(c);
2001 cleanup_body = compiler_new_block(c); 2178 cleanup_body = compiler_new_block(c);
2002 if(!(cleanup_end || cleanup_body)) 2179 if (!(cleanup_end || cleanup_body))
2003 return 0; 2180 return 0;
2004 2181
2005 compiler_nameop(c, handler->v.ExceptHandler.name, Store); 2182 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
2006 ADDOP(c, POP_TOP); 2183 ADDOP(c, POP_TOP);
2007 2184
2008 /* 2185 /*
2009 try: 2186 try:
2010 # body 2187 # body
2011 except type as name: 2188 except type as name:
2012 try: 2189 try:
2013 # body 2190 # body
2014 finally: 2191 finally:
2015 name = None 2192 name = None
2016 del name 2193 del name
2017 */ 2194 */
2018 2195
2019 /* second try: */ 2196 /* second try: */
2020 ADDOP_JREL(c, SETUP_FINALLY, cleanup_end); 2197 ADDOP_JREL(c, SETUP_FINALLY, cleanup_end);
2021 compiler_use_next_block(c, cleanup_body); 2198 compiler_use_next_block(c, cleanup_body);
2022 if (!compiler_push_fblock(c, FINALLY_TRY, cleanup_body)) 2199 if (!compiler_push_fblock(c, FINALLY_TRY, cleanup_body))
2023 return 0; 2200 return 0;
2024 2201
2025 /* second # body */ 2202 /* second # body */
2026 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body); 2203 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
2027 ADDOP(c, POP_BLOCK); 2204 ADDOP(c, POP_BLOCK);
2028 ADDOP(c, POP_EXCEPT); 2205 ADDOP(c, POP_EXCEPT);
2029 compiler_pop_fblock(c, FINALLY_TRY, cleanup_body); 2206 compiler_pop_fblock(c, FINALLY_TRY, cleanup_body);
2030 2207
2031 /* finally: */ 2208 /* finally: */
2032 ADDOP_O(c, LOAD_CONST, Py_None, consts); 2209 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2033 compiler_use_next_block(c, cleanup_end); 2210 compiler_use_next_block(c, cleanup_end);
2034 if (!compiler_push_fblock(c, FINALLY_END, cleanup_end)) 2211 if (!compiler_push_fblock(c, FINALLY_END, cleanup_end))
2035 return 0; 2212 return 0;
2036 2213
2037 /* name = None */ 2214 /* name = None */
2038 ADDOP_O(c, LOAD_CONST, Py_None, consts); 2215 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2039 compiler_nameop(c, handler->v.ExceptHandler.name, Store); 2216 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
2040 2217
2041 /* del name */ 2218 /* del name */
2042 compiler_nameop(c, handler->v.ExceptHandler.name, Del); 2219 compiler_nameop(c, handler->v.ExceptHandler.name, Del);
2043 2220
2044 ADDOP(c, END_FINALLY); 2221 ADDOP(c, END_FINALLY);
2045 compiler_pop_fblock(c, FINALLY_END, cleanup_end); 2222 compiler_pop_fblock(c, FINALLY_END, cleanup_end);
2046 } 2223 }
2047 else { 2224 else {
2048 basicblock *cleanup_body; 2225 basicblock *cleanup_body;
2049 2226
2050 cleanup_body = compiler_new_block(c); 2227 cleanup_body = compiler_new_block(c);
2051 if(!cleanup_body) 2228 if (!cleanup_body)
2052 return 0; 2229 return 0;
2053 2230
2054 ADDOP(c, POP_TOP); 2231 ADDOP(c, POP_TOP);
2055 ADDOP(c, POP_TOP); 2232 ADDOP(c, POP_TOP);
2056 compiler_use_next_block(c, cleanup_body); 2233 compiler_use_next_block(c, cleanup_body);
2057 if (!compiler_push_fblock(c, FINALLY_TRY, cleanup_body)) 2234 if (!compiler_push_fblock(c, FINALLY_TRY, cleanup_body))
2058 return 0; 2235 return 0;
2059 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body); 2236 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
2060 ADDOP(c, POP_EXCEPT); 2237 ADDOP(c, POP_EXCEPT);
2061 compiler_pop_fblock(c, FINALLY_TRY, cleanup_body); 2238 compiler_pop_fblock(c, FINALLY_TRY, cleanup_body);
2062 } 2239 }
2063 ADDOP_JREL(c, JUMP_FORWARD, end); 2240 ADDOP_JREL(c, JUMP_FORWARD, end);
2064 compiler_use_next_block(c, except); 2241 compiler_use_next_block(c, except);
2065 } 2242 }
2066 ADDOP(c, END_FINALLY); 2243 ADDOP(c, END_FINALLY);
2067 compiler_use_next_block(c, orelse); 2244 compiler_use_next_block(c, orelse);
2068 VISIT_SEQ(c, stmt, s->v.TryExcept.orelse); 2245 VISIT_SEQ(c, stmt, s->v.Try.orelse);
2069 compiler_use_next_block(c, end); 2246 compiler_use_next_block(c, end);
2070 return 1; 2247 return 1;
2071 } 2248 }
2249
2250 static int
2251 compiler_try(struct compiler *c, stmt_ty s) {
2252 if (s->v.Try.finalbody && asdl_seq_LEN(s->v.Try.finalbody))
2253 return compiler_try_finally(c, s);
2254 else
2255 return compiler_try_except(c, s);
2256 }
2257
2072 2258
2073 static int 2259 static int
2074 compiler_import_as(struct compiler *c, identifier name, identifier asname) 2260 compiler_import_as(struct compiler *c, identifier name, identifier asname)
2075 { 2261 {
2076 /* The IMPORT_NAME opcode was already generated. This function 2262 /* The IMPORT_NAME opcode was already generated. This function
2077 merely needs to bind the result to a name. 2263 merely needs to bind the result to a name.
2078 2264
2079 If there is a dot in name, we need to split it and emit a 2265 If there is a dot in name, we need to split it and emit a
2080 LOAD_ATTR for each name. 2266 LOAD_ATTR for each name.
2081 */ 2267 */
2082 const Py_UNICODE *src = PyUnicode_AS_UNICODE(name); 2268 Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0,
2083 const Py_UNICODE *dot = Py_UNICODE_strchr(src, '.'); 2269 PyUnicode_GET_LENGTH(name), 1);
2084 if (dot) { 2270 if (dot == -2)
2271 return -1;
2272 if (dot != -1) {
2085 /* Consume the base module name to get the first attribute */ 2273 /* Consume the base module name to get the first attribute */
2086 src = dot + 1; 2274 Py_ssize_t pos = dot + 1;
2087 while (dot) { 2275 while (dot != -1) {
2088 /* NB src is only defined when dot != NULL */
2089 PyObject *attr; 2276 PyObject *attr;
2090 dot = Py_UNICODE_strchr(src, '.'); 2277 dot = PyUnicode_FindChar(name, '.', pos,
2091 attr = PyUnicode_FromUnicode(src, 2278 PyUnicode_GET_LENGTH(name), 1);
2092 dot ? dot - src : Py_UNICODE_strlen(src)); 2279 if (dot == -2)
2280 return -1;
2281 attr = PyUnicode_Substring(name, pos,
2282 (dot != -1) ? dot :
2283 PyUnicode_GET_LENGTH(name));
2093 if (!attr) 2284 if (!attr)
2094 return -1; 2285 return -1;
2095 ADDOP_O(c, LOAD_ATTR, attr, names); 2286 ADDOP_O(c, LOAD_ATTR, attr, names);
2096 Py_DECREF(attr); 2287 Py_DECREF(attr);
2097 src = dot + 1; 2288 pos = dot + 1;
2098 } 2289 }
2099 } 2290 }
2100 return compiler_nameop(c, asname, Store); 2291 return compiler_nameop(c, asname, Store);
2101 } 2292 }
2102 2293
2103 static int 2294 static int
2104 compiler_import(struct compiler *c, stmt_ty s) 2295 compiler_import(struct compiler *c, stmt_ty s)
2105 { 2296 {
2106 /* The Import node stores a module name like a.b.c as a single 2297 /* The Import node stores a module name like a.b.c as a single
2107 string. This is convenient for all cases except 2298 string. This is convenient for all cases except
(...skipping 18 matching lines...) Expand all
2126 ADDOP_O(c, LOAD_CONST, Py_None, consts); 2317 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2127 ADDOP_NAME(c, IMPORT_NAME, alias->name, names); 2318 ADDOP_NAME(c, IMPORT_NAME, alias->name, names);
2128 2319
2129 if (alias->asname) { 2320 if (alias->asname) {
2130 r = compiler_import_as(c, alias->name, alias->asname); 2321 r = compiler_import_as(c, alias->name, alias->asname);
2131 if (!r) 2322 if (!r)
2132 return r; 2323 return r;
2133 } 2324 }
2134 else { 2325 else {
2135 identifier tmp = alias->name; 2326 identifier tmp = alias->name;
2136 const Py_UNICODE *base = PyUnicode_AS_UNICODE(alias->name); 2327 Py_ssize_t dot = PyUnicode_FindChar(
2137 Py_UNICODE *dot = Py_UNICODE_strchr(base, '.'); 2328 alias->name, '.', 0, PyUnicode_GET_LENGTH(alias->name), 1);
2138 if (dot) 2329 if (dot != -1) {
2139 tmp = PyUnicode_FromUnicode(base, 2330 tmp = PyUnicode_Substring(alias->name, 0, dot);
2140 dot - base); 2331 if (tmp == NULL)
2332 return 0;
2333 }
2141 r = compiler_nameop(c, tmp, Store); 2334 r = compiler_nameop(c, tmp, Store);
2142 if (dot) { 2335 if (dot != -1) {
2143 Py_DECREF(tmp); 2336 Py_DECREF(tmp);
2144 } 2337 }
2145 if (!r) 2338 if (!r)
2146 return r; 2339 return r;
2147 } 2340 }
2148 } 2341 }
2149 return 1; 2342 return 1;
2150 } 2343 }
2151 2344
2152 static int 2345 static int
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
2195 if (s->v.ImportFrom.module) { 2388 if (s->v.ImportFrom.module) {
2196 ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names); 2389 ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
2197 } 2390 }
2198 else { 2391 else {
2199 ADDOP_NAME(c, IMPORT_NAME, empty_string, names); 2392 ADDOP_NAME(c, IMPORT_NAME, empty_string, names);
2200 } 2393 }
2201 for (i = 0; i < n; i++) { 2394 for (i = 0; i < n; i++) {
2202 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i); 2395 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
2203 identifier store_name; 2396 identifier store_name;
2204 2397
2205 if (i == 0 && *PyUnicode_AS_UNICODE(alias->name) == '*') { 2398 if (i == 0 && PyUnicode_READ_CHAR(alias->name, 0) == '*') {
2206 assert(n == 1); 2399 assert(n == 1);
2207 ADDOP(c, IMPORT_STAR); 2400 ADDOP(c, IMPORT_STAR);
2208 return 1; 2401 return 1;
2209 } 2402 }
2210 2403
2211 ADDOP_NAME(c, IMPORT_FROM, alias->name, names); 2404 ADDOP_NAME(c, IMPORT_FROM, alias->name, names);
2212 store_name = alias->name; 2405 store_name = alias->name;
2213 if (alias->asname) 2406 if (alias->asname)
2214 store_name = alias->asname; 2407 store_name = alias->asname;
2215 2408
(...skipping 21 matching lines...) Expand all
2237 if (assertion_error == NULL) 2430 if (assertion_error == NULL)
2238 return 0; 2431 return 0;
2239 } 2432 }
2240 if (s->v.Assert.test->kind == Tuple_kind && 2433 if (s->v.Assert.test->kind == Tuple_kind &&
2241 asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) { 2434 asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) {
2242 msg = PyUnicode_FromString("assertion is always true, " 2435 msg = PyUnicode_FromString("assertion is always true, "
2243 "perhaps remove parentheses?"); 2436 "perhaps remove parentheses?");
2244 if (msg == NULL) 2437 if (msg == NULL)
2245 return 0; 2438 return 0;
2246 if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg, 2439 if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg,
2247 c->filename, c->u->u_lineno, 2440 c->c_filename, c->u->u_lineno,
2248 NULL, NULL) == -1) { 2441 NULL, NULL) == -1) {
2249 Py_DECREF(msg); 2442 Py_DECREF(msg);
2250 return 0; 2443 return 0;
2251 } 2444 }
2252 Py_DECREF(msg); 2445 Py_DECREF(msg);
2253 } 2446 }
2254 VISIT(c, expr, s->v.Assert.test); 2447 VISIT(c, expr, s->v.Assert.test);
2255 end = compiler_new_block(c); 2448 end = compiler_new_block(c);
2256 if (end == NULL) 2449 if (end == NULL)
2257 return 0; 2450 return 0;
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
2317 if (s->v.Raise.exc) { 2510 if (s->v.Raise.exc) {
2318 VISIT(c, expr, s->v.Raise.exc); 2511 VISIT(c, expr, s->v.Raise.exc);
2319 n++; 2512 n++;
2320 if (s->v.Raise.cause) { 2513 if (s->v.Raise.cause) {
2321 VISIT(c, expr, s->v.Raise.cause); 2514 VISIT(c, expr, s->v.Raise.cause);
2322 n++; 2515 n++;
2323 } 2516 }
2324 } 2517 }
2325 ADDOP_I(c, RAISE_VARARGS, n); 2518 ADDOP_I(c, RAISE_VARARGS, n);
2326 break; 2519 break;
2327 case TryExcept_kind: 2520 case Try_kind:
2328 return compiler_try_except(c, s); 2521 return compiler_try(c, s);
2329 case TryFinally_kind:
2330 return compiler_try_finally(c, s);
2331 case Assert_kind: 2522 case Assert_kind:
2332 return compiler_assert(c, s); 2523 return compiler_assert(c, s);
2333 case Import_kind: 2524 case Import_kind:
2334 return compiler_import(c, s); 2525 return compiler_import(c, s);
2335 case ImportFrom_kind: 2526 case ImportFrom_kind:
2336 return compiler_from_import(c, s); 2527 return compiler_from_import(c, s);
2337 case Global_kind: 2528 case Global_kind:
2338 case Nonlocal_kind: 2529 case Nonlocal_kind:
2339 break; 2530 break;
2340 case Expr_kind: 2531 case Expr_kind:
(...skipping 10 matching lines...) Expand all
2351 case Pass_kind: 2542 case Pass_kind:
2352 break; 2543 break;
2353 case Break_kind: 2544 case Break_kind:
2354 if (!compiler_in_loop(c)) 2545 if (!compiler_in_loop(c))
2355 return compiler_error(c, "'break' outside loop"); 2546 return compiler_error(c, "'break' outside loop");
2356 ADDOP(c, BREAK_LOOP); 2547 ADDOP(c, BREAK_LOOP);
2357 break; 2548 break;
2358 case Continue_kind: 2549 case Continue_kind:
2359 return compiler_continue(c); 2550 return compiler_continue(c);
2360 case With_kind: 2551 case With_kind:
2361 return compiler_with(c, s); 2552 return compiler_with(c, s, 0);
2362 } 2553 }
2363 return 1; 2554 return 1;
2364 } 2555 }
2365 2556
2366 static int 2557 static int
2367 unaryop(unaryop_ty op) 2558 unaryop(unaryop_ty op)
2368 { 2559 {
2369 switch (op) { 2560 switch (op) {
2370 case Invert: 2561 case Invert:
2371 return UNARY_INVERT; 2562 return UNARY_INVERT;
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
2488 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype; 2679 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
2489 2680
2490 PyObject *dict = c->u->u_names; 2681 PyObject *dict = c->u->u_names;
2491 PyObject *mangled; 2682 PyObject *mangled;
2492 /* XXX AugStore isn't used anywhere! */ 2683 /* XXX AugStore isn't used anywhere! */
2493 2684
2494 mangled = _Py_Mangle(c->u->u_private, name); 2685 mangled = _Py_Mangle(c->u->u_private, name);
2495 if (!mangled) 2686 if (!mangled)
2496 return 0; 2687 return 0;
2497 2688
2689 assert(PyUnicode_CompareWithASCIIString(name, "None") &&
2690 PyUnicode_CompareWithASCIIString(name, "True") &&
2691 PyUnicode_CompareWithASCIIString(name, "False"));
2692
2498 op = 0; 2693 op = 0;
2499 optype = OP_NAME; 2694 optype = OP_NAME;
2500 scope = PyST_GetScope(c->u->u_ste, mangled); 2695 scope = PyST_GetScope(c->u->u_ste, mangled);
2501 switch (scope) { 2696 switch (scope) {
2502 case FREE: 2697 case FREE:
2503 dict = c->u->u_freevars; 2698 dict = c->u->u_freevars;
2504 optype = OP_DEREF; 2699 optype = OP_DEREF;
2505 break; 2700 break;
2506 case CELL: 2701 case CELL:
2507 dict = c->u->u_cellvars; 2702 dict = c->u->u_cellvars;
(...skipping 10 matching lines...) Expand all
2518 break; 2713 break;
2519 case GLOBAL_EXPLICIT: 2714 case GLOBAL_EXPLICIT:
2520 optype = OP_GLOBAL; 2715 optype = OP_GLOBAL;
2521 break; 2716 break;
2522 default: 2717 default:
2523 /* scope can be 0 */ 2718 /* scope can be 0 */
2524 break; 2719 break;
2525 } 2720 }
2526 2721
2527 /* XXX Leave assert here, but handle __doc__ and the like better */ 2722 /* XXX Leave assert here, but handle __doc__ and the like better */
2528 assert(scope || PyUnicode_AS_UNICODE(name)[0] == '_'); 2723 assert(scope || PyUnicode_READ_CHAR(name, 0) == '_');
2529 2724
2530 switch (optype) { 2725 switch (optype) {
2531 case OP_DEREF: 2726 case OP_DEREF:
2532 switch (ctx) { 2727 switch (ctx) {
2533 case Load: op = LOAD_DEREF; break; 2728 case Load:
2729 op = (c->u->u_ste->ste_type == ClassBlock) ? LOAD_CLASSDEREF : LOAD_ DEREF;
2730 break;
2534 case Store: op = STORE_DEREF; break; 2731 case Store: op = STORE_DEREF; break;
2535 case AugLoad: 2732 case AugLoad:
2536 case AugStore: 2733 case AugStore:
2537 break; 2734 break;
2538 case Del: op = DELETE_DEREF; break; 2735 case Del: op = DELETE_DEREF; break;
2539 case Param: 2736 case Param:
2540 default: 2737 default:
2541 PyErr_SetString(PyExc_SystemError, 2738 PyErr_SetString(PyExc_SystemError,
2542 "param invalid for deref variable"); 2739 "param invalid for deref variable");
2543 return 0; 2740 return 0;
(...skipping 352 matching lines...) Expand 10 before | Expand all | Expand 10 after
2896 3093
2897 return 1; 3094 return 1;
2898 } 3095 }
2899 3096
2900 static int 3097 static int
2901 compiler_comprehension(struct compiler *c, expr_ty e, int type, identifier name, 3098 compiler_comprehension(struct compiler *c, expr_ty e, int type, identifier name,
2902 asdl_seq *generators, expr_ty elt, expr_ty val) 3099 asdl_seq *generators, expr_ty elt, expr_ty val)
2903 { 3100 {
2904 PyCodeObject *co = NULL; 3101 PyCodeObject *co = NULL;
2905 expr_ty outermost_iter; 3102 expr_ty outermost_iter;
3103 PyObject *qualname = NULL;
2906 3104
2907 outermost_iter = ((comprehension_ty) 3105 outermost_iter = ((comprehension_ty)
2908 asdl_seq_GET(generators, 0))->iter; 3106 asdl_seq_GET(generators, 0))->iter;
2909 3107
2910 if (!compiler_enter_scope(c, name, (void *)e, e->lineno)) 3108 if (!compiler_enter_scope(c, name, COMPILER_SCOPE_COMPREHENSION,
3109 (void *)e, e->lineno))
2911 goto error; 3110 goto error;
2912 3111
2913 if (type != COMP_GENEXP) { 3112 if (type != COMP_GENEXP) {
2914 int op; 3113 int op;
2915 switch (type) { 3114 switch (type) {
2916 case COMP_LISTCOMP: 3115 case COMP_LISTCOMP:
2917 op = BUILD_LIST; 3116 op = BUILD_LIST;
2918 break; 3117 break;
2919 case COMP_SETCOMP: 3118 case COMP_SETCOMP:
2920 op = BUILD_SET; 3119 op = BUILD_SET;
(...skipping 12 matching lines...) Expand all
2933 3132
2934 if (!compiler_comprehension_generator(c, generators, 0, elt, 3133 if (!compiler_comprehension_generator(c, generators, 0, elt,
2935 val, type)) 3134 val, type))
2936 goto error_in_scope; 3135 goto error_in_scope;
2937 3136
2938 if (type != COMP_GENEXP) { 3137 if (type != COMP_GENEXP) {
2939 ADDOP(c, RETURN_VALUE); 3138 ADDOP(c, RETURN_VALUE);
2940 } 3139 }
2941 3140
2942 co = assemble(c, 1); 3141 co = assemble(c, 1);
3142 qualname = compiler_scope_qualname(c);
2943 compiler_exit_scope(c); 3143 compiler_exit_scope(c);
2944 if (co == NULL) 3144 if (qualname == NULL || co == NULL)
2945 goto error; 3145 goto error;
2946 3146
2947 if (!compiler_make_closure(c, co, 0)) 3147 if (!compiler_make_closure(c, co, 0, qualname))
2948 goto error; 3148 goto error;
3149 Py_DECREF(qualname);
2949 Py_DECREF(co); 3150 Py_DECREF(co);
2950 3151
2951 VISIT(c, expr, outermost_iter); 3152 VISIT(c, expr, outermost_iter);
2952 ADDOP(c, GET_ITER); 3153 ADDOP(c, GET_ITER);
2953 ADDOP_I(c, CALL_FUNCTION, 1); 3154 ADDOP_I(c, CALL_FUNCTION, 1);
2954 return 1; 3155 return 1;
2955 error_in_scope: 3156 error_in_scope:
2956 compiler_exit_scope(c); 3157 compiler_exit_scope(c);
2957 error: 3158 error:
3159 Py_XDECREF(qualname);
2958 Py_XDECREF(co); 3160 Py_XDECREF(co);
2959 return 0; 3161 return 0;
2960 } 3162 }
2961 3163
2962 static int 3164 static int
2963 compiler_genexp(struct compiler *c, expr_ty e) 3165 compiler_genexp(struct compiler *c, expr_ty e)
2964 { 3166 {
2965 static identifier name; 3167 static identifier name;
2966 if (!name) { 3168 if (!name) {
2967 name = PyUnicode_FromString("<genexpr>"); 3169 name = PyUnicode_FromString("<genexpr>");
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
3041 char *id; 3243 char *id;
3042 switch (e->kind) { 3244 switch (e->kind) {
3043 case Ellipsis_kind: 3245 case Ellipsis_kind:
3044 return 1; 3246 return 1;
3045 case Num_kind: 3247 case Num_kind:
3046 return PyObject_IsTrue(e->v.Num.n); 3248 return PyObject_IsTrue(e->v.Num.n);
3047 case Str_kind: 3249 case Str_kind:
3048 return PyObject_IsTrue(e->v.Str.s); 3250 return PyObject_IsTrue(e->v.Str.s);
3049 case Name_kind: 3251 case Name_kind:
3050 /* optimize away names that can't be reassigned */ 3252 /* optimize away names that can't be reassigned */
3051 id = PyBytes_AS_STRING( 3253 id = PyUnicode_AsUTF8(e->v.Name.id);
3052 _PyUnicode_AsDefaultEncodedString(e->v.Name.id)); 3254 if (id && strcmp(id, "__debug__") == 0)
3053 if (strcmp(id, "True") == 0) return 1; 3255 return !c->c_optimize;
3054 if (strcmp(id, "False") == 0) return 0; 3256 return -1;
3055 if (strcmp(id, "None") == 0) return 0; 3257 case NameConstant_kind: {
3056 if (strcmp(id, "__debug__") == 0) 3258 PyObject *o = e->v.NameConstant.value;
3057 return ! c->c_optimize; 3259 if (o == Py_None)
3058 /* fall through */ 3260 return 0;
3261 else if (o == Py_True)
3262 return 1;
3263 else if (o == Py_False)
3264 return 0;
3265 }
3059 default: 3266 default:
3060 return -1; 3267 return -1;
3061 } 3268 }
3062 } 3269 }
3063 3270
3064 /* 3271 /*
3065 Implements the with statement from PEP 343. 3272 Implements the with statement from PEP 343.
3066 3273
3067 The semantics outlined in that PEP are as follows: 3274 The semantics outlined in that PEP are as follows:
3068 3275
3069 with EXPR as VAR: 3276 with EXPR as VAR:
3070 BLOCK 3277 BLOCK
3071 3278
3072 It is implemented roughly as: 3279 It is implemented roughly as:
3073 3280
3074 context = EXPR 3281 context = EXPR
3075 exit = context.__exit__ # not calling it 3282 exit = context.__exit__ # not calling it
3076 value = context.__enter__() 3283 value = context.__enter__()
3077 try: 3284 try:
3078 VAR = value # if VAR present in the syntax 3285 VAR = value # if VAR present in the syntax
3079 BLOCK 3286 BLOCK
3080 finally: 3287 finally:
3081 if an exception was raised: 3288 if an exception was raised:
3082 exc = copy of (exception, instance, traceback) 3289 exc = copy of (exception, instance, traceback)
3083 else: 3290 else:
3084 exc = (None, None, None) 3291 exc = (None, None, None)
3085 exit(*exc) 3292 exit(*exc)
3086 */ 3293 */
3087 static int 3294 static int
3088 compiler_with(struct compiler *c, stmt_ty s) 3295 compiler_with(struct compiler *c, stmt_ty s, int pos)
3089 { 3296 {
3090 basicblock *block, *finally; 3297 basicblock *block, *finally;
3298 withitem_ty item = asdl_seq_GET(s->v.With.items, pos);
3091 3299
3092 assert(s->kind == With_kind); 3300 assert(s->kind == With_kind);
3093 3301
3094 block = compiler_new_block(c); 3302 block = compiler_new_block(c);
3095 finally = compiler_new_block(c); 3303 finally = compiler_new_block(c);
3096 if (!block || !finally) 3304 if (!block || !finally)
3097 return 0; 3305 return 0;
3098 3306
3099 /* Evaluate EXPR */ 3307 /* Evaluate EXPR */
3100 VISIT(c, expr, s->v.With.context_expr); 3308 VISIT(c, expr, item->context_expr);
3101 ADDOP_JREL(c, SETUP_WITH, finally); 3309 ADDOP_JREL(c, SETUP_WITH, finally);
3102 3310
3103 /* SETUP_WITH pushes a finally block. */ 3311 /* SETUP_WITH pushes a finally block. */
3104 compiler_use_next_block(c, block); 3312 compiler_use_next_block(c, block);
3105 if (!compiler_push_fblock(c, FINALLY_TRY, block)) { 3313 if (!compiler_push_fblock(c, FINALLY_TRY, block)) {
3106 return 0; 3314 return 0;
3107 } 3315 }
3108 3316
3109 if (s->v.With.optional_vars) { 3317 if (item->optional_vars) {
3110 VISIT(c, expr, s->v.With.optional_vars); 3318 VISIT(c, expr, item->optional_vars);
3111 } 3319 }
3112 else { 3320 else {
3113 /* Discard result from context.__enter__() */ 3321 /* Discard result from context.__enter__() */
3114 ADDOP(c, POP_TOP); 3322 ADDOP(c, POP_TOP);
3115 } 3323 }
3116 3324
3117 /* BLOCK code */ 3325 pos++;
3118 VISIT_SEQ(c, stmt, s->v.With.body); 3326 if (pos == asdl_seq_LEN(s->v.With.items))
3327 /* BLOCK code */
3328 VISIT_SEQ(c, stmt, s->v.With.body)
3329 else if (!compiler_with(c, s, pos))
3330 return 0;
3119 3331
3120 /* End of try block; start the finally block */ 3332 /* End of try block; start the finally block */
3121 ADDOP(c, POP_BLOCK); 3333 ADDOP(c, POP_BLOCK);
3122 compiler_pop_fblock(c, FINALLY_TRY, block); 3334 compiler_pop_fblock(c, FINALLY_TRY, block);
3123 3335
3124 ADDOP_O(c, LOAD_CONST, Py_None, consts); 3336 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3125 compiler_use_next_block(c, finally); 3337 compiler_use_next_block(c, finally);
3126 if (!compiler_push_fblock(c, FINALLY_END, finally)) 3338 if (!compiler_push_fblock(c, FINALLY_END, finally))
3127 return 0; 3339 return 0;
3128 3340
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
3195 if (c->u->u_ste->ste_type != FunctionBlock) 3407 if (c->u->u_ste->ste_type != FunctionBlock)
3196 return compiler_error(c, "'yield' outside function"); 3408 return compiler_error(c, "'yield' outside function");
3197 if (e->v.Yield.value) { 3409 if (e->v.Yield.value) {
3198 VISIT(c, expr, e->v.Yield.value); 3410 VISIT(c, expr, e->v.Yield.value);
3199 } 3411 }
3200 else { 3412 else {
3201 ADDOP_O(c, LOAD_CONST, Py_None, consts); 3413 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3202 } 3414 }
3203 ADDOP(c, YIELD_VALUE); 3415 ADDOP(c, YIELD_VALUE);
3204 break; 3416 break;
3417 case YieldFrom_kind:
3418 if (c->u->u_ste->ste_type != FunctionBlock)
3419 return compiler_error(c, "'yield' outside function");
3420 VISIT(c, expr, e->v.YieldFrom.value);
3421 ADDOP(c, GET_ITER);
3422 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3423 ADDOP(c, YIELD_FROM);
3424 break;
3205 case Compare_kind: 3425 case Compare_kind:
3206 return compiler_compare(c, e); 3426 return compiler_compare(c, e);
3207 case Call_kind: 3427 case Call_kind:
3208 return compiler_call(c, e); 3428 return compiler_call(c, e);
3209 case Num_kind: 3429 case Num_kind:
3210 ADDOP_O(c, LOAD_CONST, e->v.Num.n, consts); 3430 ADDOP_O(c, LOAD_CONST, e->v.Num.n, consts);
3211 break; 3431 break;
3212 case Str_kind: 3432 case Str_kind:
3213 ADDOP_O(c, LOAD_CONST, e->v.Str.s, consts); 3433 ADDOP_O(c, LOAD_CONST, e->v.Str.s, consts);
3214 break; 3434 break;
3215 case Bytes_kind: 3435 case Bytes_kind:
3216 ADDOP_O(c, LOAD_CONST, e->v.Bytes.s, consts); 3436 ADDOP_O(c, LOAD_CONST, e->v.Bytes.s, consts);
3217 break; 3437 break;
3218 case Ellipsis_kind: 3438 case Ellipsis_kind:
3219 ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts); 3439 ADDOP_O(c, LOAD_CONST, Py_Ellipsis, consts);
3440 break;
3441 case NameConstant_kind:
3442 ADDOP_O(c, LOAD_CONST, e->v.NameConstant.value, consts);
3220 break; 3443 break;
3221 /* The following exprs can be assignment targets. */ 3444 /* The following exprs can be assignment targets. */
3222 case Attribute_kind: 3445 case Attribute_kind:
3223 if (e->v.Attribute.ctx != AugStore) 3446 if (e->v.Attribute.ctx != AugStore)
3224 VISIT(c, expr, e->v.Attribute.value); 3447 VISIT(c, expr, e->v.Attribute.value);
3225 switch (e->v.Attribute.ctx) { 3448 switch (e->v.Attribute.ctx) {
3226 case AugLoad: 3449 case AugLoad:
3227 ADDOP(c, DUP_TOP); 3450 ADDOP(c, DUP_TOP);
3228 /* Fall through to load */ 3451 /* Fall through to load */
3229 case Load: 3452 case Load:
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after
3380 /* Raises a SyntaxError and returns 0. 3603 /* Raises a SyntaxError and returns 0.
3381 If something goes wrong, a different exception may be raised. 3604 If something goes wrong, a different exception may be raised.
3382 */ 3605 */
3383 3606
3384 static int 3607 static int
3385 compiler_error(struct compiler *c, const char *errstr) 3608 compiler_error(struct compiler *c, const char *errstr)
3386 { 3609 {
3387 PyObject *loc; 3610 PyObject *loc;
3388 PyObject *u = NULL, *v = NULL; 3611 PyObject *u = NULL, *v = NULL;
3389 3612
3390 loc = PyErr_ProgramTextObject(c->filename, c->u->u_lineno); 3613 loc = PyErr_ProgramTextObject(c->c_filename, c->u->u_lineno);
3391 if (!loc) { 3614 if (!loc) {
3392 Py_INCREF(Py_None); 3615 Py_INCREF(Py_None);
3393 loc = Py_None; 3616 loc = Py_None;
3394 } 3617 }
3395 u = Py_BuildValue("(OiiO)", c->filename, c->u->u_lineno, 3618 u = Py_BuildValue("(OiiO)", c->c_filename, c->u->u_lineno,
3396 c->u->u_col_offset, loc); 3619 c->u->u_col_offset, loc);
3397 if (!u) 3620 if (!u)
3398 goto exit; 3621 goto exit;
3399 v = Py_BuildValue("(zO)", errstr, u); 3622 v = Py_BuildValue("(zO)", errstr, u);
3400 if (!v) 3623 if (!v)
3401 goto exit; 3624 goto exit;
3402 PyErr_SetObject(PyExc_SyntaxError, v); 3625 PyErr_SetObject(PyExc_SyntaxError, v);
3403 exit: 3626 exit:
3404 Py_DECREF(loc); 3627 Py_DECREF(loc);
3405 Py_XDECREF(u); 3628 Py_XDECREF(u);
(...skipping 487 matching lines...) Expand 10 before | Expand all | Expand 10 after
3893 PyTuple_SET_ITEM(tuple, i - offset, k); 4116 PyTuple_SET_ITEM(tuple, i - offset, k);
3894 } 4117 }
3895 return tuple; 4118 return tuple;
3896 } 4119 }
3897 4120
3898 static int 4121 static int
3899 compute_code_flags(struct compiler *c) 4122 compute_code_flags(struct compiler *c)
3900 { 4123 {
3901 PySTEntryObject *ste = c->u->u_ste; 4124 PySTEntryObject *ste = c->u->u_ste;
3902 int flags = 0, n; 4125 int flags = 0, n;
3903 if (ste->ste_type != ModuleBlock) 4126 if (ste->ste_type == FunctionBlock) {
3904 flags |= CO_NEWLOCALS; 4127 flags |= CO_NEWLOCALS;
3905 if (ste->ste_type == FunctionBlock) {
3906 if (!ste->ste_unoptimized) 4128 if (!ste->ste_unoptimized)
3907 flags |= CO_OPTIMIZED; 4129 flags |= CO_OPTIMIZED;
3908 if (ste->ste_nested) 4130 if (ste->ste_nested)
3909 flags |= CO_NESTED; 4131 flags |= CO_NESTED;
3910 if (ste->ste_generator) 4132 if (ste->ste_generator)
3911 flags |= CO_GENERATOR; 4133 flags |= CO_GENERATOR;
3912 if (ste->ste_varargs) 4134 if (ste->ste_varargs)
3913 flags |= CO_VARARGS; 4135 flags |= CO_VARARGS;
3914 if (ste->ste_varkeywords) 4136 if (ste->ste_varkeywords)
3915 flags |= CO_VARKEYWORDS; 4137 flags |= CO_VARKEYWORDS;
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
3957 varnames = dict_keys_inorder(c->u->u_varnames, 0); 4179 varnames = dict_keys_inorder(c->u->u_varnames, 0);
3958 if (!consts || !names || !varnames) 4180 if (!consts || !names || !varnames)
3959 goto error; 4181 goto error;
3960 4182
3961 cellvars = dict_keys_inorder(c->u->u_cellvars, 0); 4183 cellvars = dict_keys_inorder(c->u->u_cellvars, 0);
3962 if (!cellvars) 4184 if (!cellvars)
3963 goto error; 4185 goto error;
3964 freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_Size(cellvars)); 4186 freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_Size(cellvars));
3965 if (!freevars) 4187 if (!freevars)
3966 goto error; 4188 goto error;
3967
3968 nlocals = PyDict_Size(c->u->u_varnames); 4189 nlocals = PyDict_Size(c->u->u_varnames);
3969 flags = compute_code_flags(c); 4190 flags = compute_code_flags(c);
3970 if (flags < 0) 4191 if (flags < 0)
3971 goto error; 4192 goto error;
3972 4193
3973 bytecode = PyCode_Optimize(a->a_bytecode, consts, names, a->a_lnotab); 4194 bytecode = PyCode_Optimize(a->a_bytecode, consts, names, a->a_lnotab);
3974 if (!bytecode) 4195 if (!bytecode)
3975 goto error; 4196 goto error;
3976 4197
3977 tmp = PyList_AsTuple(consts); /* PyCode_New requires a tuple */ 4198 tmp = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
3978 if (!tmp) 4199 if (!tmp)
3979 goto error; 4200 goto error;
3980 Py_DECREF(consts); 4201 Py_DECREF(consts);
3981 consts = tmp; 4202 consts = tmp;
3982 4203
3983 co = PyCode_New(c->u->u_argcount, c->u->u_kwonlyargcount, 4204 co = PyCode_New(c->u->u_argcount, c->u->u_kwonlyargcount,
3984 nlocals, stackdepth(c), flags, 4205 nlocals, stackdepth(c), flags,
3985 bytecode, consts, names, varnames, 4206 bytecode, consts, names, varnames,
3986 freevars, cellvars, 4207 freevars, cellvars,
3987 c->filename, c->u->u_name, 4208 c->c_filename, c->u->u_name,
3988 c->u->u_firstlineno, 4209 c->u->u_firstlineno,
3989 a->a_lnotab); 4210 a->a_lnotab);
3990 error: 4211 error:
3991 Py_XDECREF(consts); 4212 Py_XDECREF(consts);
3992 Py_XDECREF(names); 4213 Py_XDECREF(names);
3993 Py_XDECREF(varnames); 4214 Py_XDECREF(varnames);
3994 Py_XDECREF(name); 4215 Py_XDECREF(name);
3995 Py_XDECREF(freevars); 4216 Py_XDECREF(freevars);
3996 Py_XDECREF(cellvars); 4217 Py_XDECREF(cellvars);
3997 Py_XDECREF(bytecode); 4218 Py_XDECREF(bytecode);
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
4094 4315
4095 #undef PyAST_Compile 4316 #undef PyAST_Compile
4096 PyAPI_FUNC(PyCodeObject *) 4317 PyAPI_FUNC(PyCodeObject *)
4097 PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags, 4318 PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags,
4098 PyArena *arena) 4319 PyArena *arena)
4099 { 4320 {
4100 return PyAST_CompileEx(mod, filename, flags, -1, arena); 4321 return PyAST_CompileEx(mod, filename, flags, -1, arena);
4101 } 4322 }
4102 4323
4103 4324
LEFTRIGHT

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