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

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, 12 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;
253 285
254 return 1; 286 return 1;
255 } 287 }
256 288
257 PyCodeObject * 289 PyCodeObject *
258 PyAST_CompileEx(mod_ty mod, const char *filename, PyCompilerFlags *flags, 290 PyAST_CompileObject(mod_ty mod, PyObject *filename, PyCompilerFlags *flags,
259 int optimize, PyArena *arena) 291 int optimize, PyArena *arena)
260 { 292 {
261 struct compiler c; 293 struct compiler c;
262 PyCodeObject *co = NULL; 294 PyCodeObject *co = NULL;
263 PyCompilerFlags local_flags; 295 PyCompilerFlags local_flags;
264 int merged; 296 int merged;
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 c.filename = PyUnicode_DecodeFSDefault(filename); 306 Py_INCREF(filename);
275 if (c.filename == NULL) 307 c.c_filename = filename;
276 goto finally;
277 c.c_arena = arena; 308 c.c_arena = arena;
278 c.c_future = PyFuture_FromAST(mod, filename); 309 c.c_future = PyFuture_FromASTObject(mod, filename);
279 if (c.c_future == NULL) 310 if (c.c_future == NULL)
280 goto finally; 311 goto finally;
281 if (!flags) { 312 if (!flags) {
282 local_flags.cf_flags = 0; 313 local_flags.cf_flags = 0;
283 flags = &local_flags; 314 flags = &local_flags;
284 } 315 }
285 merged = c.c_future->ff_features | flags->cf_flags; 316 merged = c.c_future->ff_features | flags->cf_flags;
286 c.c_future->ff_features = merged; 317 c.c_future->ff_features = merged;
287 flags->cf_flags = merged; 318 flags->cf_flags = merged;
288 c.c_flags = flags; 319 c.c_flags = flags;
289 c.c_optimize = (optimize == -1) ? Py_OptimizeFlag : optimize; 320 c.c_optimize = (optimize == -1) ? Py_OptimizeFlag : optimize;
290 c.c_nestlevel = 0; 321 c.c_nestlevel = 0;
291 322
292 c.c_st = PySymtable_Build(mod, filename, c.c_future); 323 c.c_st = PySymtable_BuildObject(mod, filename, c.c_future);
293 if (c.c_st == NULL) { 324 if (c.c_st == NULL) {
294 if (!PyErr_Occurred()) 325 if (!PyErr_Occurred())
295 PyErr_SetString(PyExc_SystemError, "no symtable"); 326 PyErr_SetString(PyExc_SystemError, "no symtable");
296 goto finally; 327 goto finally;
297 } 328 }
298 329
299 co = compiler_mod(&c, mod); 330 co = compiler_mod(&c, mod);
300 331
301 finally: 332 finally:
302 compiler_free(&c); 333 compiler_free(&c);
303 assert(co || PyErr_Occurred()); 334 assert(co || PyErr_Occurred());
304 return co; 335 return co;
336 }
337
338 PyCodeObject *
339 PyAST_CompileEx(mod_ty mod, const char *filename_str, PyCompilerFlags *flags,
340 int optimize, PyArena *arena)
341 {
342 PyObject *filename;
343 PyCodeObject *co;
344 filename = PyUnicode_DecodeFSDefault(filename_str);
345 if (filename == NULL)
346 return NULL;
347 co = PyAST_CompileObject(mod, filename, flags, optimize, arena);
348 Py_DECREF(filename);
349 return co;
350
305 } 351 }
306 352
307 PyCodeObject * 353 PyCodeObject *
308 PyNode_Compile(struct _node *n, const char *filename) 354 PyNode_Compile(struct _node *n, const char *filename)
309 { 355 {
310 PyCodeObject *co = NULL; 356 PyCodeObject *co = NULL;
311 mod_ty mod; 357 mod_ty mod;
312 PyArena *arena = PyArena_New(); 358 PyArena *arena = PyArena_New();
313 if (!arena) 359 if (!arena)
314 return NULL; 360 return NULL;
315 mod = PyAST_FromNode(n, NULL, filename, arena); 361 mod = PyAST_FromNode(n, NULL, filename, arena);
316 if (mod) 362 if (mod)
317 co = PyAST_Compile(mod, filename, NULL, arena); 363 co = PyAST_Compile(mod, filename, NULL, arena);
318 PyArena_Free(arena); 364 PyArena_Free(arena);
319 return co; 365 return co;
320 } 366 }
321 367
322 static void 368 static void
323 compiler_free(struct compiler *c) 369 compiler_free(struct compiler *c)
324 { 370 {
325 if (c->c_st) 371 if (c->c_st)
326 PySymtable_Free(c->c_st); 372 PySymtable_Free(c->c_st);
327 if (c->c_future) 373 if (c->c_future)
328 PyObject_Free(c->c_future); 374 PyObject_Free(c->c_future);
329 Py_DECREF(c->c_stack); 375 Py_DECREF(c->c_stack);
330 Py_XDECREF(c->filename); 376 Py_XDECREF(c->c_filename);
331 } 377 }
332 378
333 static PyObject * 379 static PyObject *
334 list2dict(PyObject *list) 380 list2dict(PyObject *list)
335 { 381 {
336 Py_ssize_t i, n; 382 Py_ssize_t i, n;
337 PyObject *v, *k; 383 PyObject *v, *k;
338 PyObject *dict = PyDict_New(); 384 PyObject *dict = PyDict_New();
339 if (!dict) return NULL; 385 if (!dict) return NULL;
340 386
(...skipping 22 matching lines...) Expand all
363 409
364 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
365 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
366 values are integers, starting at offset and increasing by one for 412 values are integers, starting at offset and increasing by one for
367 each key. 413 each key.
368 */ 414 */
369 415
370 static PyObject * 416 static PyObject *
371 dictbytype(PyObject *src, int scope_type, int flag, int offset) 417 dictbytype(PyObject *src, int scope_type, int flag, int offset)
372 { 418 {
373 Py_ssize_t pos = 0, i = offset, scope; 419 Py_ssize_t i = offset, scope, num_keys, key_i;
374 PyObject *k, *v, *dest = PyDict_New(); 420 PyObject *k, *v, *dest = PyDict_New();
421 PyObject *sorted_keys;
375 422
376 assert(offset >= 0); 423 assert(offset >= 0);
377 if (dest == NULL) 424 if (dest == NULL)
378 return NULL; 425 return NULL;
379 426
380 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++) {
381 /* XXX this should probably be a macro in symtable.h */ 442 /* XXX this should probably be a macro in symtable.h */
382 long vi; 443 long vi;
444 k = PyList_GET_ITEM(sorted_keys, key_i);
445 v = PyDict_GetItem(src, k);
383 assert(PyLong_Check(v)); 446 assert(PyLong_Check(v));
384 vi = PyLong_AS_LONG(v); 447 vi = PyLong_AS_LONG(v);
385 scope = (vi >> SCOPE_OFFSET) & SCOPE_MASK; 448 scope = (vi >> SCOPE_OFFSET) & SCOPE_MASK;
386 449
387 if (scope == scope_type || vi & flag) { 450 if (scope == scope_type || vi & flag) {
388 PyObject *tuple, *item = PyLong_FromLong(i); 451 PyObject *tuple, *item = PyLong_FromLong(i);
389 if (item == NULL) { 452 if (item == NULL) {
453 Py_DECREF(sorted_keys);
390 Py_DECREF(dest); 454 Py_DECREF(dest);
391 return NULL; 455 return NULL;
392 } 456 }
393 i++; 457 i++;
394 tuple = PyTuple_Pack(2, k, k->ob_type); 458 tuple = PyTuple_Pack(2, k, k->ob_type);
395 if (!tuple || PyDict_SetItem(dest, tuple, item) < 0) { 459 if (!tuple || PyDict_SetItem(dest, tuple, item) < 0) {
460 Py_DECREF(sorted_keys);
396 Py_DECREF(item); 461 Py_DECREF(item);
397 Py_DECREF(dest); 462 Py_DECREF(dest);
398 Py_XDECREF(tuple); 463 Py_XDECREF(tuple);
399 return NULL; 464 return NULL;
400 } 465 }
401 Py_DECREF(item); 466 Py_DECREF(item);
402 Py_DECREF(tuple); 467 Py_DECREF(tuple);
403 } 468 }
404 } 469 }
470 Py_DECREF(sorted_keys);
405 return dest; 471 return dest;
406 } 472 }
407 473
408 static void 474 static void
409 compiler_unit_check(struct compiler_unit *u) 475 compiler_unit_check(struct compiler_unit *u)
410 { 476 {
411 basicblock *block; 477 basicblock *block;
412 for (block = u->u_blocks; block != NULL; block = block->b_list) { 478 for (block = u->u_blocks; block != NULL; block = block->b_list) {
413 assert((void *)block != (void *)0xcbcbcbcb); 479 assert((void *)block != (void *)0xcbcbcbcb);
414 assert((void *)block != (void *)0xfbfbfbfb); 480 assert((void *)block != (void *)0xfbfbfbfb);
(...skipping 19 matching lines...) Expand all
434 b = u->u_blocks; 500 b = u->u_blocks;
435 while (b != NULL) { 501 while (b != NULL) {
436 if (b->b_instr) 502 if (b->b_instr)
437 PyObject_Free((void *)b->b_instr); 503 PyObject_Free((void *)b->b_instr);
438 next = b->b_list; 504 next = b->b_list;
439 PyObject_Free((void *)b); 505 PyObject_Free((void *)b);
440 b = next; 506 b = next;
441 } 507 }
442 Py_CLEAR(u->u_ste); 508 Py_CLEAR(u->u_ste);
443 Py_CLEAR(u->u_name); 509 Py_CLEAR(u->u_name);
510 Py_CLEAR(u->u_qualname);
444 Py_CLEAR(u->u_consts); 511 Py_CLEAR(u->u_consts);
445 Py_CLEAR(u->u_names); 512 Py_CLEAR(u->u_names);
446 Py_CLEAR(u->u_varnames); 513 Py_CLEAR(u->u_varnames);
447 Py_CLEAR(u->u_freevars); 514 Py_CLEAR(u->u_freevars);
448 Py_CLEAR(u->u_cellvars); 515 Py_CLEAR(u->u_cellvars);
449 Py_CLEAR(u->u_private); 516 Py_CLEAR(u->u_private);
450 PyObject_Free(u); 517 PyObject_Free(u);
451 } 518 }
452 519
453 static int 520 static int
454 compiler_enter_scope(struct compiler *c, identifier name, void *key, 521 compiler_enter_scope(struct compiler *c, identifier name,
455 int lineno) 522 int scope_type, void *key, int lineno)
456 { 523 {
457 struct compiler_unit *u; 524 struct compiler_unit *u;
458 525
459 u = (struct compiler_unit *)PyObject_Malloc(sizeof( 526 u = (struct compiler_unit *)PyObject_Malloc(sizeof(
460 struct compiler_unit)); 527 struct compiler_unit));
461 if (!u) { 528 if (!u) {
462 PyErr_NoMemory(); 529 PyErr_NoMemory();
463 return 0; 530 return 0;
464 } 531 }
465 memset(u, 0, sizeof(struct compiler_unit)); 532 memset(u, 0, sizeof(struct compiler_unit));
533 u->u_scope_type = scope_type;
466 u->u_argcount = 0; 534 u->u_argcount = 0;
467 u->u_kwonlyargcount = 0; 535 u->u_kwonlyargcount = 0;
468 u->u_ste = PySymtable_Lookup(c->c_st, key); 536 u->u_ste = PySymtable_Lookup(c->c_st, key);
469 if (!u->u_ste) { 537 if (!u->u_ste) {
470 compiler_unit_free(u); 538 compiler_unit_free(u);
471 return 0; 539 return 0;
472 } 540 }
473 Py_INCREF(name); 541 Py_INCREF(name);
474 u->u_name = name; 542 u->u_name = name;
475 u->u_varnames = list2dict(u->u_ste->ste_varnames); 543 u->u_varnames = list2dict(u->u_ste->ste_varnames);
476 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);
477 if (!u->u_varnames || !u->u_cellvars) { 545 if (!u->u_varnames || !u->u_cellvars) {
478 compiler_unit_free(u); 546 compiler_unit_free(u);
479 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 }
480 } 579 }
481 580
482 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,
483 PyDict_Size(u->u_cellvars)); 582 PyDict_Size(u->u_cellvars));
484 if (!u->u_freevars) { 583 if (!u->u_freevars) {
485 compiler_unit_free(u); 584 compiler_unit_free(u);
486 return 0; 585 return 0;
487 } 586 }
488 587
489 u->u_blocks = NULL; 588 u->u_blocks = NULL;
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
543 /* 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 */
544 if (PySequence_DelItem(c->c_stack, n) < 0) 643 if (PySequence_DelItem(c->c_stack, n) < 0)
545 Py_FatalError("compiler_exit_scope()"); 644 Py_FatalError("compiler_exit_scope()");
546 compiler_unit_check(c->u); 645 compiler_unit_check(c->u);
547 } 646 }
548 else 647 else
549 c->u = NULL; 648 c->u = NULL;
550 649
551 } 650 }
552 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
553 /* Allocate a new block and return a pointer to it. 705 /* Allocate a new block and return a pointer to it.
554 Returns NULL on error. 706 Returns NULL on error.
555 */ 707 */
556 708
557 static basicblock * 709 static basicblock *
558 compiler_new_block(struct compiler *c) 710 compiler_new_block(struct compiler *c)
559 { 711 {
560 basicblock *b; 712 basicblock *b;
561 struct compiler_unit *u; 713 struct compiler_unit *u;
562 714
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after
745 case INPLACE_AND: 897 case INPLACE_AND:
746 case INPLACE_XOR: 898 case INPLACE_XOR:
747 case INPLACE_OR: 899 case INPLACE_OR:
748 return -1; 900 return -1;
749 case BREAK_LOOP: 901 case BREAK_LOOP:
750 return 0; 902 return 0;
751 case SETUP_WITH: 903 case SETUP_WITH:
752 return 7; 904 return 7;
753 case WITH_CLEANUP: 905 case WITH_CLEANUP:
754 return -1; /* XXX Sometimes more */ 906 return -1; /* XXX Sometimes more */
755 case STORE_LOCALS:
756 return -1;
757 case RETURN_VALUE: 907 case RETURN_VALUE:
758 return -1; 908 return -1;
759 case IMPORT_STAR: 909 case IMPORT_STAR:
760 return -1; 910 return -1;
761 case YIELD_VALUE: 911 case YIELD_VALUE:
762 return 0; 912 return 0;
763 913 case YIELD_FROM:
914 return -1;
764 case POP_BLOCK: 915 case POP_BLOCK:
765 return 0; 916 return 0;
766 case POP_EXCEPT: 917 case POP_EXCEPT:
767 return 0; /* -3 except if bad bytecode */ 918 return 0; /* -3 except if bad bytecode */
768 case END_FINALLY: 919 case END_FINALLY:
769 return -1; /* or -2 or -3 if exception occurred */ 920 return -1; /* or -2 or -3 if exception occurred */
770 921
771 case STORE_NAME: 922 case STORE_NAME:
772 return -1; 923 return -1;
773 case DELETE_NAME: 924 case DELETE_NAME:
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
839 return -oparg; 990 return -oparg;
840 #define NARGS(o) (((o) % 256) + 2*(((o) / 256) % 256)) 991 #define NARGS(o) (((o) % 256) + 2*(((o) / 256) % 256))
841 case CALL_FUNCTION: 992 case CALL_FUNCTION:
842 return -NARGS(oparg); 993 return -NARGS(oparg);
843 case CALL_FUNCTION_VAR: 994 case CALL_FUNCTION_VAR:
844 case CALL_FUNCTION_KW: 995 case CALL_FUNCTION_KW:
845 return -NARGS(oparg)-1; 996 return -NARGS(oparg)-1;
846 case CALL_FUNCTION_VAR_KW: 997 case CALL_FUNCTION_VAR_KW:
847 return -NARGS(oparg)-2; 998 return -NARGS(oparg)-2;
848 case MAKE_FUNCTION: 999 case MAKE_FUNCTION:
849 return -NARGS(oparg) - ((oparg >> 16) & 0xffff); 1000 return -1 -NARGS(oparg) - ((oparg >> 16) & 0xffff);
850 case MAKE_CLOSURE: 1001 case MAKE_CLOSURE:
851 return -1 - NARGS(oparg) - ((oparg >> 16) & 0xffff); 1002 return -2 - NARGS(oparg) - ((oparg >> 16) & 0xffff);
852 #undef NARGS 1003 #undef NARGS
853 case BUILD_SLICE: 1004 case BUILD_SLICE:
854 if (oparg == 3) 1005 if (oparg == 3)
855 return -2; 1006 return -2;
856 else 1007 else
857 return -1; 1008 return -1;
858 1009
859 case LOAD_CLOSURE: 1010 case LOAD_CLOSURE:
860 return 1; 1011 return 1;
861 case LOAD_DEREF: 1012 case LOAD_DEREF:
1013 case LOAD_CLASSDEREF:
862 return 1; 1014 return 1;
863 case STORE_DEREF: 1015 case STORE_DEREF:
864 return -1; 1016 return -1;
865 case DELETE_DEREF: 1017 case DELETE_DEREF:
866 return 0; 1018 return 0;
867 default: 1019 default:
868 fprintf(stderr, "opcode = %d\n", opcode); 1020 fprintf(stderr, "opcode = %d\n", opcode);
869 Py_FatalError("opcode_stack_effect()"); 1021 Py_FatalError("opcode_stack_effect()");
870 1022
871 } 1023 }
(...skipping 299 matching lines...) Expand 10 before | Expand all | Expand 10 after
1171 { 1323 {
1172 PyCodeObject *co; 1324 PyCodeObject *co;
1173 int addNone = 1; 1325 int addNone = 1;
1174 static PyObject *module; 1326 static PyObject *module;
1175 if (!module) { 1327 if (!module) {
1176 module = PyUnicode_InternFromString("<module>"); 1328 module = PyUnicode_InternFromString("<module>");
1177 if (!module) 1329 if (!module)
1178 return NULL; 1330 return NULL;
1179 } 1331 }
1180 /* Use 0 for firstlineno initially, will fixup in assemble(). */ 1332 /* Use 0 for firstlineno initially, will fixup in assemble(). */
1181 if (!compiler_enter_scope(c, module, mod, 0)) 1333 if (!compiler_enter_scope(c, module, COMPILER_SCOPE_MODULE, mod, 0))
1182 return NULL; 1334 return NULL;
1183 switch (mod->kind) { 1335 switch (mod->kind) {
1184 case Module_kind: 1336 case Module_kind:
1185 if (!compiler_body(c, mod->v.Module.body)) { 1337 if (!compiler_body(c, mod->v.Module.body)) {
1186 compiler_exit_scope(c); 1338 compiler_exit_scope(c);
1187 return 0; 1339 return 0;
1188 } 1340 }
1189 break; 1341 break;
1190 case Interactive_kind: 1342 case Interactive_kind:
1191 c->c_interactive = 1; 1343 c->c_interactive = 1;
(...skipping 20 matching lines...) Expand all
1212 } 1364 }
1213 1365
1214 /* 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
1215 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
1216 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.
1217 */ 1369 */
1218 1370
1219 static int 1371 static int
1220 get_ref_type(struct compiler *c, PyObject *name) 1372 get_ref_type(struct compiler *c, PyObject *name)
1221 { 1373 {
1222 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);
1223 if (scope == 0) { 1379 if (scope == 0) {
1224 char buf[350]; 1380 char buf[350];
1225 PyOS_snprintf(buf, sizeof(buf), 1381 PyOS_snprintf(buf, sizeof(buf),
1226 "unknown scope for %.100s in %.100s(%s)\n" 1382 "unknown scope for %.100s in %.100s(%s)\n"
1227 "symbols: %s\nlocals: %s\nglobals: %s", 1383 "symbols: %s\nlocals: %s\nglobals: %s",
1228 PyBytes_AS_STRING(name), 1384 PyBytes_AS_STRING(name),
1229 PyBytes_AS_STRING(c->u->u_name), 1385 PyBytes_AS_STRING(c->u->u_name),
1230 PyObject_REPR(c->u->u_ste->ste_id), 1386 PyObject_REPR(c->u->u_ste->ste_id),
1231 PyObject_REPR(c->u->u_ste->ste_symbols), 1387 PyObject_REPR(c->u->u_ste->ste_symbols),
1232 PyObject_REPR(c->u->u_varnames), 1388 PyObject_REPR(c->u->u_varnames),
(...skipping 13 matching lines...) Expand all
1246 if (k == NULL) 1402 if (k == NULL)
1247 return -1; 1403 return -1;
1248 v = PyDict_GetItem(dict, k); 1404 v = PyDict_GetItem(dict, k);
1249 Py_DECREF(k); 1405 Py_DECREF(k);
1250 if (v == NULL) 1406 if (v == NULL)
1251 return -1; 1407 return -1;
1252 return PyLong_AS_LONG(v); 1408 return PyLong_AS_LONG(v);
1253 } 1409 }
1254 1410
1255 static int 1411 static int
1256 compiler_make_closure(struct compiler *c, PyCodeObject *co, int args) 1412 compiler_make_closure(struct compiler *c, PyCodeObject *co, int args, PyObject * qualname)
1257 { 1413 {
1258 int i, free = PyCode_GetNumFree(co); 1414 int i, free = PyCode_GetNumFree(co);
1415 if (qualname == NULL)
1416 qualname = co->co_name;
1417
1259 if (free == 0) { 1418 if (free == 0) {
1260 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);
1261 ADDOP_I(c, MAKE_FUNCTION, args); 1421 ADDOP_I(c, MAKE_FUNCTION, args);
1262 return 1; 1422 return 1;
1263 } 1423 }
1264 for (i = 0; i < free; ++i) { 1424 for (i = 0; i < free; ++i) {
1265 /* Bypass com_addop_varname because it will generate 1425 /* Bypass com_addop_varname because it will generate
1266 LOAD_DEREF but LOAD_CLOSURE is needed. 1426 LOAD_DEREF but LOAD_CLOSURE is needed.
1267 */ 1427 */
1268 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i); 1428 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
1269 int arg, reftype; 1429 int arg, reftype;
1270 1430
(...skipping 16 matching lines...) Expand all
1287 PyBytes_AS_STRING(c->u->u_name), 1447 PyBytes_AS_STRING(c->u->u_name),
1288 reftype, arg, 1448 reftype, arg,
1289 _PyUnicode_AsString(co->co_name), 1449 _PyUnicode_AsString(co->co_name),
1290 PyObject_REPR(co->co_freevars)); 1450 PyObject_REPR(co->co_freevars));
1291 Py_FatalError("compiler_make_closure()"); 1451 Py_FatalError("compiler_make_closure()");
1292 } 1452 }
1293 ADDOP_I(c, LOAD_CLOSURE, arg); 1453 ADDOP_I(c, LOAD_CLOSURE, arg);
1294 } 1454 }
1295 ADDOP_I(c, BUILD_TUPLE, free); 1455 ADDOP_I(c, BUILD_TUPLE, free);
1296 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);
1297 ADDOP_I(c, MAKE_CLOSURE, args); 1458 ADDOP_I(c, MAKE_CLOSURE, args);
1298 return 1; 1459 return 1;
1299 } 1460 }
1300 1461
1301 static int 1462 static int
1302 compiler_decorators(struct compiler *c, asdl_seq* decos) 1463 compiler_decorators(struct compiler *c, asdl_seq* decos)
1303 { 1464 {
1304 int i; 1465 int i;
1305 1466
1306 if (!decos) 1467 if (!decos)
1307 return 1; 1468 return 1;
1308 1469
1309 for (i = 0; i < asdl_seq_LEN(decos); i++) { 1470 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1310 VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i)); 1471 VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i));
1311 } 1472 }
1312 return 1; 1473 return 1;
1313 } 1474 }
1314 1475
1315 static int 1476 static int
1316 compiler_visit_kwonlydefaults(struct compiler *c, asdl_seq *kwonlyargs, 1477 compiler_visit_kwonlydefaults(struct compiler *c, asdl_seq *kwonlyargs,
1317 asdl_seq *kw_defaults) 1478 asdl_seq *kw_defaults)
1318 { 1479 {
1319 int i, default_count = 0; 1480 int i, default_count = 0;
1320 for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) { 1481 for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) {
1321 arg_ty arg = asdl_seq_GET(kwonlyargs, i); 1482 arg_ty arg = asdl_seq_GET(kwonlyargs, i);
1322 expr_ty default_ = asdl_seq_GET(kw_defaults, i); 1483 expr_ty default_ = asdl_seq_GET(kw_defaults, i);
1323 if (default_) { 1484 if (default_) {
1324 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);
1325 if (!compiler_visit_expr(c, default_)) { 1490 if (!compiler_visit_expr(c, default_)) {
1326 return -1; 1491 return -1;
1327 } 1492 }
1328 default_count++; 1493 default_count++;
1329 } 1494 }
1330 } 1495 }
1331 return default_count; 1496 return default_count;
1332 } 1497 }
1333 1498
1334 static int 1499 static int
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
1373 */ 1538 */
1374 static identifier return_str; 1539 static identifier return_str;
1375 PyObject *names; 1540 PyObject *names;
1376 int len; 1541 int len;
1377 names = PyList_New(0); 1542 names = PyList_New(0);
1378 if (!names) 1543 if (!names)
1379 return -1; 1544 return -1;
1380 1545
1381 if (compiler_visit_argannotations(c, args->args, names)) 1546 if (compiler_visit_argannotations(c, args->args, names))
1382 goto error; 1547 goto error;
1383 if (args->varargannotation && 1548 if (args->vararg && args->vararg->annotation &&
1384 compiler_visit_argannotation(c, args->vararg, 1549 compiler_visit_argannotation(c, args->vararg->arg,
1385 args->varargannotation, names)) 1550 args->vararg->annotation, names))
1386 goto error; 1551 goto error;
1387 if (compiler_visit_argannotations(c, args->kwonlyargs, names)) 1552 if (compiler_visit_argannotations(c, args->kwonlyargs, names))
1388 goto error; 1553 goto error;
1389 if (args->kwargannotation && 1554 if (args->kwarg && args->kwarg->annotation &&
1390 compiler_visit_argannotation(c, args->kwarg, 1555 compiler_visit_argannotation(c, args->kwarg->arg,
1391 args->kwargannotation, names)) 1556 args->kwarg->annotation, names))
1392 goto error; 1557 goto error;
1393 1558
1394 if (!return_str) { 1559 if (!return_str) {
1395 return_str = PyUnicode_InternFromString("return"); 1560 return_str = PyUnicode_InternFromString("return");
1396 if (!return_str) 1561 if (!return_str)
1397 goto error; 1562 goto error;
1398 } 1563 }
1399 if (compiler_visit_argannotation(c, return_str, returns, names)) { 1564 if (compiler_visit_argannotation(c, return_str, returns, names)) {
1400 goto error; 1565 goto error;
1401 } 1566 }
(...skipping 26 matching lines...) Expand all
1428 1593
1429 error: 1594 error:
1430 Py_DECREF(names); 1595 Py_DECREF(names);
1431 return -1; 1596 return -1;
1432 } 1597 }
1433 1598
1434 static int 1599 static int
1435 compiler_function(struct compiler *c, stmt_ty s) 1600 compiler_function(struct compiler *c, stmt_ty s)
1436 { 1601 {
1437 PyCodeObject *co; 1602 PyCodeObject *co;
1438 PyObject *first_const = Py_None; 1603 PyObject *qualname, *first_const = Py_None;
1439 arguments_ty args = s->v.FunctionDef.args; 1604 arguments_ty args = s->v.FunctionDef.args;
1440 expr_ty returns = s->v.FunctionDef.returns; 1605 expr_ty returns = s->v.FunctionDef.returns;
1441 asdl_seq* decos = s->v.FunctionDef.decorator_list; 1606 asdl_seq* decos = s->v.FunctionDef.decorator_list;
1442 stmt_ty st; 1607 stmt_ty st;
1443 int i, n, docstring, kw_default_count = 0, arglength; 1608 int i, n, docstring, kw_default_count = 0, arglength;
1444 int num_annotations; 1609 int num_annotations;
1445 1610
1446 assert(s->kind == FunctionDef_kind); 1611 assert(s->kind == FunctionDef_kind);
1447 1612
1448 if (!compiler_decorators(c, decos)) 1613 if (!compiler_decorators(c, decos))
1449 return 0; 1614 return 0;
1615 if (args->defaults)
1616 VISIT_SEQ(c, expr, args->defaults);
1450 if (args->kwonlyargs) { 1617 if (args->kwonlyargs) {
1451 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs, 1618 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
1452 args->kw_defaults); 1619 args->kw_defaults);
1453 if (res < 0) 1620 if (res < 0)
1454 return 0; 1621 return 0;
1455 kw_default_count = res; 1622 kw_default_count = res;
1456 } 1623 }
1457 if (args->defaults)
1458 VISIT_SEQ(c, expr, args->defaults);
1459 num_annotations = compiler_visit_annotations(c, args, returns); 1624 num_annotations = compiler_visit_annotations(c, args, returns);
1460 if (num_annotations < 0) 1625 if (num_annotations < 0)
1461 return 0; 1626 return 0;
1462 assert((num_annotations & 0xFFFF) == num_annotations); 1627 assert((num_annotations & 0xFFFF) == num_annotations);
1463 1628
1464 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,
1465 s->lineno)) 1631 s->lineno))
1466 return 0; 1632 return 0;
1467 1633
1468 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, 0); 1634 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, 0);
1469 docstring = compiler_isdocstring(st); 1635 docstring = compiler_isdocstring(st);
1470 if (docstring && c->c_optimize < 2) 1636 if (docstring && c->c_optimize < 2)
1471 first_const = st->v.Expr.value->v.Str.s; 1637 first_const = st->v.Expr.value->v.Str.s;
1472 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) {
1473 compiler_exit_scope(c); 1639 compiler_exit_scope(c);
1474 return 0; 1640 return 0;
1475 } 1641 }
1476 1642
1477 c->u->u_argcount = asdl_seq_LEN(args->args); 1643 c->u->u_argcount = asdl_seq_LEN(args->args);
1478 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs); 1644 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
1479 n = asdl_seq_LEN(s->v.FunctionDef.body); 1645 n = asdl_seq_LEN(s->v.FunctionDef.body);
1480 /* 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 */
1481 for (i = docstring; i < n; i++) { 1647 for (i = docstring; i < n; i++) {
1482 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, i); 1648 st = (stmt_ty)asdl_seq_GET(s->v.FunctionDef.body, i);
1483 VISIT_IN_SCOPE(c, stmt, st); 1649 VISIT_IN_SCOPE(c, stmt, st);
1484 } 1650 }
1485 co = assemble(c, 1); 1651 co = assemble(c, 1);
1652 qualname = compiler_scope_qualname(c);
1486 compiler_exit_scope(c); 1653 compiler_exit_scope(c);
1487 if (co == NULL) 1654 if (qualname == NULL || co == NULL) {
1488 return 0; 1655 Py_XDECREF(qualname);
1656 Py_XDECREF(co);
1657 return 0;
1658 }
1489 1659
1490 arglength = asdl_seq_LEN(args->defaults); 1660 arglength = asdl_seq_LEN(args->defaults);
1491 arglength |= kw_default_count << 8; 1661 arglength |= kw_default_count << 8;
1492 arglength |= num_annotations << 16; 1662 arglength |= num_annotations << 16;
1493 compiler_make_closure(c, co, arglength); 1663 compiler_make_closure(c, co, arglength, qualname);
1664 Py_DECREF(qualname);
1494 Py_DECREF(co); 1665 Py_DECREF(co);
1495 1666
1496 /* decorators */ 1667 /* decorators */
1497 for (i = 0; i < asdl_seq_LEN(decos); i++) { 1668 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1498 ADDOP_I(c, CALL_FUNCTION, 1); 1669 ADDOP_I(c, CALL_FUNCTION, 1);
1499 } 1670 }
1500 1671
1501 return compiler_nameop(c, s->v.FunctionDef.name, Store); 1672 return compiler_nameop(c, s->v.FunctionDef.name, Store);
1502 } 1673 }
1503 1674
(...skipping 14 matching lines...) Expand all
1518 <func> is a function/closure created from the class body; 1689 <func> is a function/closure created from the class body;
1519 it has a single argument (__locals__) where the dict 1690 it has a single argument (__locals__) where the dict
1520 (or MutableSequence) representing the locals is passed 1691 (or MutableSequence) representing the locals is passed
1521 <name> is the class name 1692 <name> is the class name
1522 <bases> is the positional arguments and *varargs argument 1693 <bases> is the positional arguments and *varargs argument
1523 <keywords> is the keyword arguments and **kwds argument 1694 <keywords> is the keyword arguments and **kwds argument
1524 This borrows from compiler_call. 1695 This borrows from compiler_call.
1525 */ 1696 */
1526 1697
1527 /* 1. compile the class body into a code object */ 1698 /* 1. compile the class body into a code object */
1528 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))
1529 return 0; 1701 return 0;
1530 /* this block represents what we do in the new scope */ 1702 /* this block represents what we do in the new scope */
1531 { 1703 {
1532 /* use the class name for name mangling */ 1704 /* use the class name for name mangling */
1533 Py_INCREF(s->v.ClassDef.name); 1705 Py_INCREF(s->v.ClassDef.name);
1534 Py_XDECREF(c->u->u_private); 1706 Py_XDECREF(c->u->u_private);
1535 c->u->u_private = s->v.ClassDef.name; 1707 c->u->u_private = s->v.ClassDef.name;
1536 /* force it to have one mandatory argument */
1537 c->u->u_argcount = 1;
1538 /* load the first argument (__locals__) ... */
1539 ADDOP_I(c, LOAD_FAST, 0);
1540 /* ... and store it into f_locals */
1541 ADDOP_IN_SCOPE(c, STORE_LOCALS);
1542 /* load (global) __name__ ... */ 1708 /* load (global) __name__ ... */
1543 str = PyUnicode_InternFromString("__name__"); 1709 str = PyUnicode_InternFromString("__name__");
1544 if (!str || !compiler_nameop(c, str, Load)) { 1710 if (!str || !compiler_nameop(c, str, Load)) {
1545 Py_XDECREF(str); 1711 Py_XDECREF(str);
1546 compiler_exit_scope(c); 1712 compiler_exit_scope(c);
1547 return 0; 1713 return 0;
1548 } 1714 }
1549 Py_DECREF(str); 1715 Py_DECREF(str);
1550 /* ... and store it as __module__ */ 1716 /* ... and store it as __module__ */
1551 str = PyUnicode_InternFromString("__module__"); 1717 str = PyUnicode_InternFromString("__module__");
1552 if (!str || !compiler_nameop(c, str, Store)) { 1718 if (!str || !compiler_nameop(c, str, Store)) {
1553 Py_XDECREF(str); 1719 Py_XDECREF(str);
1554 compiler_exit_scope(c); 1720 compiler_exit_scope(c);
1555 return 0; 1721 return 0;
1556 } 1722 }
1557 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);
1558 /* compile the body proper */ 1739 /* compile the body proper */
1559 if (!compiler_body(c, s->v.ClassDef.body)) { 1740 if (!compiler_body(c, s->v.ClassDef.body)) {
1560 compiler_exit_scope(c); 1741 compiler_exit_scope(c);
1561 return 0; 1742 return 0;
1562 } 1743 }
1563 /* return the (empty) __class__ cell */ 1744 if (c->u->u_ste->ste_needs_class_closure) {
1564 str = PyUnicode_InternFromString("__class__"); 1745 /* return the (empty) __class__ cell */
1565 if (str == NULL) { 1746 str = PyUnicode_InternFromString("__class__");
1566 compiler_exit_scope(c); 1747 if (str == NULL) {
1567 return 0; 1748 compiler_exit_scope(c);
1568 } 1749 return 0;
1569 i = compiler_lookup_arg(c->u->u_cellvars, str); 1750 }
1570 Py_DECREF(str); 1751 i = compiler_lookup_arg(c->u->u_cellvars, str);
1571 if (i == -1) { 1752 Py_DECREF(str);
1572 /* This happens when nobody references the cell */ 1753 assert(i == 0);
1573 PyErr_Clear();
1574 /* Return None */
1575 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1576 }
1577 else {
1578 /* Return the cell where to store __class__ */ 1754 /* Return the cell where to store __class__ */
1579 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);
1580 } 1761 }
1581 ADDOP_IN_SCOPE(c, RETURN_VALUE); 1762 ADDOP_IN_SCOPE(c, RETURN_VALUE);
1582 /* create the code object */ 1763 /* create the code object */
1583 co = assemble(c, 1); 1764 co = assemble(c, 1);
1584 } 1765 }
1585 /* leave the new scope */ 1766 /* leave the new scope */
1586 compiler_exit_scope(c); 1767 compiler_exit_scope(c);
1587 if (co == NULL) 1768 if (co == NULL)
1588 return 0; 1769 return 0;
1589 1770
1590 /* 2. load the 'build_class' function */ 1771 /* 2. load the 'build_class' function */
1591 ADDOP(c, LOAD_BUILD_CLASS); 1772 ADDOP(c, LOAD_BUILD_CLASS);
1592 1773
1593 /* 3. load a function (or closure) made from the code object */ 1774 /* 3. load a function (or closure) made from the code object */
1594 compiler_make_closure(c, co, 0); 1775 compiler_make_closure(c, co, 0, NULL);
1595 Py_DECREF(co); 1776 Py_DECREF(co);
1596 1777
1597 /* 4. load class name */ 1778 /* 4. load class name */
1598 ADDOP_O(c, LOAD_CONST, s->v.ClassDef.name, consts); 1779 ADDOP_O(c, LOAD_CONST, s->v.ClassDef.name, consts);
1599 1780
1600 /* 5. generate the rest of the code for the call */ 1781 /* 5. generate the rest of the code for the call */
1601 if (!compiler_call_helper(c, 2, 1782 if (!compiler_call_helper(c, 2,
1602 s->v.ClassDef.bases, 1783 s->v.ClassDef.bases,
1603 s->v.ClassDef.keywords, 1784 s->v.ClassDef.keywords,
1604 s->v.ClassDef.starargs, 1785 s->v.ClassDef.starargs,
(...skipping 30 matching lines...) Expand all
1635 compiler_use_next_block(c, next); 1816 compiler_use_next_block(c, next);
1636 VISIT(c, expr, e->v.IfExp.orelse); 1817 VISIT(c, expr, e->v.IfExp.orelse);
1637 compiler_use_next_block(c, end); 1818 compiler_use_next_block(c, end);
1638 return 1; 1819 return 1;
1639 } 1820 }
1640 1821
1641 static int 1822 static int
1642 compiler_lambda(struct compiler *c, expr_ty e) 1823 compiler_lambda(struct compiler *c, expr_ty e)
1643 { 1824 {
1644 PyCodeObject *co; 1825 PyCodeObject *co;
1826 PyObject *qualname;
1645 static identifier name; 1827 static identifier name;
1646 int kw_default_count = 0, arglength; 1828 int kw_default_count = 0, arglength;
1647 arguments_ty args = e->v.Lambda.args; 1829 arguments_ty args = e->v.Lambda.args;
1648 assert(e->kind == Lambda_kind); 1830 assert(e->kind == Lambda_kind);
1649 1831
1650 if (!name) { 1832 if (!name) {
1651 name = PyUnicode_InternFromString("<lambda>"); 1833 name = PyUnicode_InternFromString("<lambda>");
1652 if (!name) 1834 if (!name)
1653 return 0; 1835 return 0;
1654 } 1836 }
1655 1837
1838 if (args->defaults)
1839 VISIT_SEQ(c, expr, args->defaults);
1656 if (args->kwonlyargs) { 1840 if (args->kwonlyargs) {
1657 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs, 1841 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
1658 args->kw_defaults); 1842 args->kw_defaults);
1659 if (res < 0) return 0; 1843 if (res < 0) return 0;
1660 kw_default_count = res; 1844 kw_default_count = res;
1661 } 1845 }
1662 if (args->defaults) 1846 if (!compiler_enter_scope(c, name, COMPILER_SCOPE_FUNCTION,
1663 VISIT_SEQ(c, expr, args->defaults); 1847 (void *)e, e->lineno))
1664 if (!compiler_enter_scope(c, name, (void *)e, e->lineno))
1665 return 0; 1848 return 0;
1666 1849
1667 /* 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
1668 docstring. */ 1851 docstring. */
1669 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)
1670 return 0; 1853 return 0;
1671 1854
1672 c->u->u_argcount = asdl_seq_LEN(args->args); 1855 c->u->u_argcount = asdl_seq_LEN(args->args);
1673 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs); 1856 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
1674 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body); 1857 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
1675 if (c->u->u_ste->ste_generator) { 1858 if (c->u->u_ste->ste_generator) {
1676 ADDOP_IN_SCOPE(c, POP_TOP); 1859 ADDOP_IN_SCOPE(c, POP_TOP);
1677 } 1860 }
1678 else { 1861 else {
1679 ADDOP_IN_SCOPE(c, RETURN_VALUE); 1862 ADDOP_IN_SCOPE(c, RETURN_VALUE);
1680 } 1863 }
1681 co = assemble(c, 1); 1864 co = assemble(c, 1);
1865 qualname = compiler_scope_qualname(c);
1682 compiler_exit_scope(c); 1866 compiler_exit_scope(c);
1683 if (co == NULL) 1867 if (qualname == NULL || co == NULL)
1684 return 0; 1868 return 0;
1685 1869
1686 arglength = asdl_seq_LEN(args->defaults); 1870 arglength = asdl_seq_LEN(args->defaults);
1687 arglength |= kw_default_count << 8; 1871 arglength |= kw_default_count << 8;
1688 compiler_make_closure(c, co, arglength); 1872 compiler_make_closure(c, co, arglength, qualname);
1873 Py_DECREF(qualname);
1689 Py_DECREF(co); 1874 Py_DECREF(co);
1690 1875
1691 return 1; 1876 return 1;
1692 } 1877 }
1693 1878
1694 static int 1879 static int
1695 compiler_if(struct compiler *c, stmt_ty s) 1880 compiler_if(struct compiler *c, stmt_ty s)
1696 { 1881 {
1697 basicblock *end, *next; 1882 basicblock *end, *next;
1698 int constant; 1883 int constant;
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after
1887 basicblock *body, *end; 2072 basicblock *body, *end;
1888 body = compiler_new_block(c); 2073 body = compiler_new_block(c);
1889 end = compiler_new_block(c); 2074 end = compiler_new_block(c);
1890 if (body == NULL || end == NULL) 2075 if (body == NULL || end == NULL)
1891 return 0; 2076 return 0;
1892 2077
1893 ADDOP_JREL(c, SETUP_FINALLY, end); 2078 ADDOP_JREL(c, SETUP_FINALLY, end);
1894 compiler_use_next_block(c, body); 2079 compiler_use_next_block(c, body);
1895 if (!compiler_push_fblock(c, FINALLY_TRY, body)) 2080 if (!compiler_push_fblock(c, FINALLY_TRY, body))
1896 return 0; 2081 return 0;
1897 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 }
1898 ADDOP(c, POP_BLOCK); 2089 ADDOP(c, POP_BLOCK);
1899 compiler_pop_fblock(c, FINALLY_TRY, body); 2090 compiler_pop_fblock(c, FINALLY_TRY, body);
1900 2091
1901 ADDOP_O(c, LOAD_CONST, Py_None, consts); 2092 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1902 compiler_use_next_block(c, end); 2093 compiler_use_next_block(c, end);
1903 if (!compiler_push_fblock(c, FINALLY_END, end)) 2094 if (!compiler_push_fblock(c, FINALLY_END, end))
1904 return 0; 2095 return 0;
1905 VISIT_SEQ(c, stmt, s->v.TryFinally.finalbody); 2096 VISIT_SEQ(c, stmt, s->v.Try.finalbody);
1906 ADDOP(c, END_FINALLY); 2097 ADDOP(c, END_FINALLY);
1907 compiler_pop_fblock(c, FINALLY_END, end); 2098 compiler_pop_fblock(c, FINALLY_END, end);
1908 2099
1909 return 1; 2100 return 1;
1910 } 2101 }
1911 2102
1912 /* 2103 /*
1913 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 ...":
1914 (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
1915 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
1949 body = compiler_new_block(c); 2140 body = compiler_new_block(c);
1950 except = compiler_new_block(c); 2141 except = compiler_new_block(c);
1951 orelse = compiler_new_block(c); 2142 orelse = compiler_new_block(c);
1952 end = compiler_new_block(c); 2143 end = compiler_new_block(c);
1953 if (body == NULL || except == NULL || orelse == NULL || end == NULL) 2144 if (body == NULL || except == NULL || orelse == NULL || end == NULL)
1954 return 0; 2145 return 0;
1955 ADDOP_JREL(c, SETUP_EXCEPT, except); 2146 ADDOP_JREL(c, SETUP_EXCEPT, except);
1956 compiler_use_next_block(c, body); 2147 compiler_use_next_block(c, body);
1957 if (!compiler_push_fblock(c, EXCEPT, body)) 2148 if (!compiler_push_fblock(c, EXCEPT, body))
1958 return 0; 2149 return 0;
1959 VISIT_SEQ(c, stmt, s->v.TryExcept.body); 2150 VISIT_SEQ(c, stmt, s->v.Try.body);
1960 ADDOP(c, POP_BLOCK); 2151 ADDOP(c, POP_BLOCK);
1961 compiler_pop_fblock(c, EXCEPT, body); 2152 compiler_pop_fblock(c, EXCEPT, body);
1962 ADDOP_JREL(c, JUMP_FORWARD, orelse); 2153 ADDOP_JREL(c, JUMP_FORWARD, orelse);
1963 n = asdl_seq_LEN(s->v.TryExcept.handlers); 2154 n = asdl_seq_LEN(s->v.Try.handlers);
1964 compiler_use_next_block(c, except); 2155 compiler_use_next_block(c, except);
1965 for (i = 0; i < n; i++) { 2156 for (i = 0; i < n; i++) {
1966 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET( 2157 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
1967 s->v.TryExcept.handlers, i); 2158 s->v.Try.handlers, i);
1968 if (!handler->v.ExceptHandler.type && i < n-1) 2159 if (!handler->v.ExceptHandler.type && i < n-1)
1969 return compiler_error(c, "default 'except:' must be last"); 2160 return compiler_error(c, "default 'except:' must be last");
1970 c->u->u_lineno_set = 0; 2161 c->u->u_lineno_set = 0;
1971 c->u->u_lineno = handler->lineno; 2162 c->u->u_lineno = handler->lineno;
1972 c->u->u_col_offset = handler->col_offset; 2163 c->u->u_col_offset = handler->col_offset;
1973 except = compiler_new_block(c); 2164 except = compiler_new_block(c);
1974 if (except == NULL) 2165 if (except == NULL)
1975 return 0; 2166 return 0;
1976 if (handler->v.ExceptHandler.type) { 2167 if (handler->v.ExceptHandler.type) {
1977 ADDOP(c, DUP_TOP); 2168 ADDOP(c, DUP_TOP);
1978 VISIT(c, expr, handler->v.ExceptHandler.type); 2169 VISIT(c, expr, handler->v.ExceptHandler.type);
1979 ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH); 2170 ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH);
1980 ADDOP_JABS(c, POP_JUMP_IF_FALSE, except); 2171 ADDOP_JABS(c, POP_JUMP_IF_FALSE, except);
1981 } 2172 }
1982 ADDOP(c, POP_TOP); 2173 ADDOP(c, POP_TOP);
1983 if (handler->v.ExceptHandler.name) { 2174 if (handler->v.ExceptHandler.name) {
1984 basicblock *cleanup_end, *cleanup_body; 2175 basicblock *cleanup_end, *cleanup_body;
1985 2176
1986 cleanup_end = compiler_new_block(c); 2177 cleanup_end = compiler_new_block(c);
1987 cleanup_body = compiler_new_block(c); 2178 cleanup_body = compiler_new_block(c);
1988 if(!(cleanup_end || cleanup_body)) 2179 if (!(cleanup_end || cleanup_body))
1989 return 0; 2180 return 0;
1990 2181
1991 compiler_nameop(c, handler->v.ExceptHandler.name, Store); 2182 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
1992 ADDOP(c, POP_TOP); 2183 ADDOP(c, POP_TOP);
1993 2184
1994 /* 2185 /*
1995 try: 2186 try:
1996 # body 2187 # body
1997 except type as name: 2188 except type as name:
1998 try: 2189 try:
1999 # body 2190 # body
2000 finally: 2191 finally:
2001 name = None 2192 name = None
2002 del name 2193 del name
2003 */ 2194 */
2004 2195
2005 /* second try: */ 2196 /* second try: */
2006 ADDOP_JREL(c, SETUP_FINALLY, cleanup_end); 2197 ADDOP_JREL(c, SETUP_FINALLY, cleanup_end);
2007 compiler_use_next_block(c, cleanup_body); 2198 compiler_use_next_block(c, cleanup_body);
2008 if (!compiler_push_fblock(c, FINALLY_TRY, cleanup_body)) 2199 if (!compiler_push_fblock(c, FINALLY_TRY, cleanup_body))
2009 return 0; 2200 return 0;
2010 2201
2011 /* second # body */ 2202 /* second # body */
2012 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body); 2203 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
2013 ADDOP(c, POP_BLOCK); 2204 ADDOP(c, POP_BLOCK);
2014 ADDOP(c, POP_EXCEPT); 2205 ADDOP(c, POP_EXCEPT);
2015 compiler_pop_fblock(c, FINALLY_TRY, cleanup_body); 2206 compiler_pop_fblock(c, FINALLY_TRY, cleanup_body);
2016 2207
2017 /* finally: */ 2208 /* finally: */
2018 ADDOP_O(c, LOAD_CONST, Py_None, consts); 2209 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2019 compiler_use_next_block(c, cleanup_end); 2210 compiler_use_next_block(c, cleanup_end);
2020 if (!compiler_push_fblock(c, FINALLY_END, cleanup_end)) 2211 if (!compiler_push_fblock(c, FINALLY_END, cleanup_end))
2021 return 0; 2212 return 0;
2022 2213
2023 /* name = None */ 2214 /* name = None */
2024 ADDOP_O(c, LOAD_CONST, Py_None, consts); 2215 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2025 compiler_nameop(c, handler->v.ExceptHandler.name, Store); 2216 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
2026 2217
2027 /* del name */ 2218 /* del name */
2028 compiler_nameop(c, handler->v.ExceptHandler.name, Del); 2219 compiler_nameop(c, handler->v.ExceptHandler.name, Del);
2029 2220
2030 ADDOP(c, END_FINALLY); 2221 ADDOP(c, END_FINALLY);
2031 compiler_pop_fblock(c, FINALLY_END, cleanup_end); 2222 compiler_pop_fblock(c, FINALLY_END, cleanup_end);
2032 } 2223 }
2033 else { 2224 else {
2034 basicblock *cleanup_body; 2225 basicblock *cleanup_body;
2035 2226
2036 cleanup_body = compiler_new_block(c); 2227 cleanup_body = compiler_new_block(c);
2037 if(!cleanup_body) 2228 if (!cleanup_body)
2038 return 0; 2229 return 0;
2039 2230
2040 ADDOP(c, POP_TOP); 2231 ADDOP(c, POP_TOP);
2041 ADDOP(c, POP_TOP); 2232 ADDOP(c, POP_TOP);
2042 compiler_use_next_block(c, cleanup_body); 2233 compiler_use_next_block(c, cleanup_body);
2043 if (!compiler_push_fblock(c, FINALLY_TRY, cleanup_body)) 2234 if (!compiler_push_fblock(c, FINALLY_TRY, cleanup_body))
2044 return 0; 2235 return 0;
2045 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body); 2236 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
2046 ADDOP(c, POP_EXCEPT); 2237 ADDOP(c, POP_EXCEPT);
2047 compiler_pop_fblock(c, FINALLY_TRY, cleanup_body); 2238 compiler_pop_fblock(c, FINALLY_TRY, cleanup_body);
2048 } 2239 }
2049 ADDOP_JREL(c, JUMP_FORWARD, end); 2240 ADDOP_JREL(c, JUMP_FORWARD, end);
2050 compiler_use_next_block(c, except); 2241 compiler_use_next_block(c, except);
2051 } 2242 }
2052 ADDOP(c, END_FINALLY); 2243 ADDOP(c, END_FINALLY);
2053 compiler_use_next_block(c, orelse); 2244 compiler_use_next_block(c, orelse);
2054 VISIT_SEQ(c, stmt, s->v.TryExcept.orelse); 2245 VISIT_SEQ(c, stmt, s->v.Try.orelse);
2055 compiler_use_next_block(c, end); 2246 compiler_use_next_block(c, end);
2056 return 1; 2247 return 1;
2057 } 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
2058 2258
2059 static int 2259 static int
2060 compiler_import_as(struct compiler *c, identifier name, identifier asname) 2260 compiler_import_as(struct compiler *c, identifier name, identifier asname)
2061 { 2261 {
2062 /* The IMPORT_NAME opcode was already generated. This function 2262 /* The IMPORT_NAME opcode was already generated. This function
2063 merely needs to bind the result to a name. 2263 merely needs to bind the result to a name.
2064 2264
2065 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
2066 LOAD_ATTR for each name. 2266 LOAD_ATTR for each name.
2067 */ 2267 */
2068 const Py_UNICODE *src = PyUnicode_AS_UNICODE(name); 2268 Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0,
2069 const Py_UNICODE *dot = Py_UNICODE_strchr(src, '.'); 2269 PyUnicode_GET_LENGTH(name), 1);
2070 if (dot) { 2270 if (dot == -2)
2271 return -1;
2272 if (dot != -1) {
2071 /* Consume the base module name to get the first attribute */ 2273 /* Consume the base module name to get the first attribute */
2072 src = dot + 1; 2274 Py_ssize_t pos = dot + 1;
2073 while (dot) { 2275 while (dot != -1) {
2074 /* NB src is only defined when dot != NULL */
2075 PyObject *attr; 2276 PyObject *attr;
2076 dot = Py_UNICODE_strchr(src, '.'); 2277 dot = PyUnicode_FindChar(name, '.', pos,
2077 attr = PyUnicode_FromUnicode(src, 2278 PyUnicode_GET_LENGTH(name), 1);
2078 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));
2079 if (!attr) 2284 if (!attr)
2080 return -1; 2285 return -1;
2081 ADDOP_O(c, LOAD_ATTR, attr, names); 2286 ADDOP_O(c, LOAD_ATTR, attr, names);
2082 Py_DECREF(attr); 2287 Py_DECREF(attr);
2083 src = dot + 1; 2288 pos = dot + 1;
2084 } 2289 }
2085 } 2290 }
2086 return compiler_nameop(c, asname, Store); 2291 return compiler_nameop(c, asname, Store);
2087 } 2292 }
2088 2293
2089 static int 2294 static int
2090 compiler_import(struct compiler *c, stmt_ty s) 2295 compiler_import(struct compiler *c, stmt_ty s)
2091 { 2296 {
2092 /* 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
2093 string. This is convenient for all cases except 2298 string. This is convenient for all cases except
(...skipping 18 matching lines...) Expand all
2112 ADDOP_O(c, LOAD_CONST, Py_None, consts); 2317 ADDOP_O(c, LOAD_CONST, Py_None, consts);
2113 ADDOP_NAME(c, IMPORT_NAME, alias->name, names); 2318 ADDOP_NAME(c, IMPORT_NAME, alias->name, names);
2114 2319
2115 if (alias->asname) { 2320 if (alias->asname) {
2116 r = compiler_import_as(c, alias->name, alias->asname); 2321 r = compiler_import_as(c, alias->name, alias->asname);
2117 if (!r) 2322 if (!r)
2118 return r; 2323 return r;
2119 } 2324 }
2120 else { 2325 else {
2121 identifier tmp = alias->name; 2326 identifier tmp = alias->name;
2122 const Py_UNICODE *base = PyUnicode_AS_UNICODE(alias->name); 2327 Py_ssize_t dot = PyUnicode_FindChar(
2123 Py_UNICODE *dot = Py_UNICODE_strchr(base, '.'); 2328 alias->name, '.', 0, PyUnicode_GET_LENGTH(alias->name), 1);
2124 if (dot) 2329 if (dot != -1) {
2125 tmp = PyUnicode_FromUnicode(base, 2330 tmp = PyUnicode_Substring(alias->name, 0, dot);
2126 dot - base); 2331 if (tmp == NULL)
2332 return 0;
2333 }
2127 r = compiler_nameop(c, tmp, Store); 2334 r = compiler_nameop(c, tmp, Store);
2128 if (dot) { 2335 if (dot != -1) {
2129 Py_DECREF(tmp); 2336 Py_DECREF(tmp);
2130 } 2337 }
2131 if (!r) 2338 if (!r)
2132 return r; 2339 return r;
2133 } 2340 }
2134 } 2341 }
2135 return 1; 2342 return 1;
2136 } 2343 }
2137 2344
2138 static int 2345 static int
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
2181 if (s->v.ImportFrom.module) { 2388 if (s->v.ImportFrom.module) {
2182 ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names); 2389 ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
2183 } 2390 }
2184 else { 2391 else {
2185 ADDOP_NAME(c, IMPORT_NAME, empty_string, names); 2392 ADDOP_NAME(c, IMPORT_NAME, empty_string, names);
2186 } 2393 }
2187 for (i = 0; i < n; i++) { 2394 for (i = 0; i < n; i++) {
2188 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);
2189 identifier store_name; 2396 identifier store_name;
2190 2397
2191 if (i == 0 && *PyUnicode_AS_UNICODE(alias->name) == '*') { 2398 if (i == 0 && PyUnicode_READ_CHAR(alias->name, 0) == '*') {
2192 assert(n == 1); 2399 assert(n == 1);
2193 ADDOP(c, IMPORT_STAR); 2400 ADDOP(c, IMPORT_STAR);
2194 return 1; 2401 return 1;
2195 } 2402 }
2196 2403
2197 ADDOP_NAME(c, IMPORT_FROM, alias->name, names); 2404 ADDOP_NAME(c, IMPORT_FROM, alias->name, names);
2198 store_name = alias->name; 2405 store_name = alias->name;
2199 if (alias->asname) 2406 if (alias->asname)
2200 store_name = alias->asname; 2407 store_name = alias->asname;
2201 2408
(...skipping 21 matching lines...) Expand all
2223 if (assertion_error == NULL) 2430 if (assertion_error == NULL)
2224 return 0; 2431 return 0;
2225 } 2432 }
2226 if (s->v.Assert.test->kind == Tuple_kind && 2433 if (s->v.Assert.test->kind == Tuple_kind &&
2227 asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) { 2434 asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) {
2228 msg = PyUnicode_FromString("assertion is always true, " 2435 msg = PyUnicode_FromString("assertion is always true, "
2229 "perhaps remove parentheses?"); 2436 "perhaps remove parentheses?");
2230 if (msg == NULL) 2437 if (msg == NULL)
2231 return 0; 2438 return 0;
2232 if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg, 2439 if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg,
2233 c->filename, c->u->u_lineno, 2440 c->c_filename, c->u->u_lineno,
2234 NULL, NULL) == -1) { 2441 NULL, NULL) == -1) {
2235 Py_DECREF(msg); 2442 Py_DECREF(msg);
2236 return 0; 2443 return 0;
2237 } 2444 }
2238 Py_DECREF(msg); 2445 Py_DECREF(msg);
2239 } 2446 }
2240 VISIT(c, expr, s->v.Assert.test); 2447 VISIT(c, expr, s->v.Assert.test);
2241 end = compiler_new_block(c); 2448 end = compiler_new_block(c);
2242 if (end == NULL) 2449 if (end == NULL)
2243 return 0; 2450 return 0;
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
2303 if (s->v.Raise.exc) { 2510 if (s->v.Raise.exc) {
2304 VISIT(c, expr, s->v.Raise.exc); 2511 VISIT(c, expr, s->v.Raise.exc);
2305 n++; 2512 n++;
2306 if (s->v.Raise.cause) { 2513 if (s->v.Raise.cause) {
2307 VISIT(c, expr, s->v.Raise.cause); 2514 VISIT(c, expr, s->v.Raise.cause);
2308 n++; 2515 n++;
2309 } 2516 }
2310 } 2517 }
2311 ADDOP_I(c, RAISE_VARARGS, n); 2518 ADDOP_I(c, RAISE_VARARGS, n);
2312 break; 2519 break;
2313 case TryExcept_kind: 2520 case Try_kind:
2314 return compiler_try_except(c, s); 2521 return compiler_try(c, s);
2315 case TryFinally_kind:
2316 return compiler_try_finally(c, s);
2317 case Assert_kind: 2522 case Assert_kind:
2318 return compiler_assert(c, s); 2523 return compiler_assert(c, s);
2319 case Import_kind: 2524 case Import_kind:
2320 return compiler_import(c, s); 2525 return compiler_import(c, s);
2321 case ImportFrom_kind: 2526 case ImportFrom_kind:
2322 return compiler_from_import(c, s); 2527 return compiler_from_import(c, s);
2323 case Global_kind: 2528 case Global_kind:
2324 case Nonlocal_kind: 2529 case Nonlocal_kind:
2325 break; 2530 break;
2326 case Expr_kind: 2531 case Expr_kind:
(...skipping 10 matching lines...) Expand all
2337 case Pass_kind: 2542 case Pass_kind:
2338 break; 2543 break;
2339 case Break_kind: 2544 case Break_kind:
2340 if (!compiler_in_loop(c)) 2545 if (!compiler_in_loop(c))
2341 return compiler_error(c, "'break' outside loop"); 2546 return compiler_error(c, "'break' outside loop");
2342 ADDOP(c, BREAK_LOOP); 2547 ADDOP(c, BREAK_LOOP);
2343 break; 2548 break;
2344 case Continue_kind: 2549 case Continue_kind:
2345 return compiler_continue(c); 2550 return compiler_continue(c);
2346 case With_kind: 2551 case With_kind:
2347 return compiler_with(c, s); 2552 return compiler_with(c, s, 0);
2348 } 2553 }
2349 return 1; 2554 return 1;
2350 } 2555 }
2351 2556
2352 static int 2557 static int
2353 unaryop(unaryop_ty op) 2558 unaryop(unaryop_ty op)
2354 { 2559 {
2355 switch (op) { 2560 switch (op) {
2356 case Invert: 2561 case Invert:
2357 return UNARY_INVERT; 2562 return UNARY_INVERT;
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
2474 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype; 2679 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
2475 2680
2476 PyObject *dict = c->u->u_names; 2681 PyObject *dict = c->u->u_names;
2477 PyObject *mangled; 2682 PyObject *mangled;
2478 /* XXX AugStore isn't used anywhere! */ 2683 /* XXX AugStore isn't used anywhere! */
2479 2684
2480 mangled = _Py_Mangle(c->u->u_private, name); 2685 mangled = _Py_Mangle(c->u->u_private, name);
2481 if (!mangled) 2686 if (!mangled)
2482 return 0; 2687 return 0;
2483 2688
2689 assert(PyUnicode_CompareWithASCIIString(name, "None") &&
2690 PyUnicode_CompareWithASCIIString(name, "True") &&
2691 PyUnicode_CompareWithASCIIString(name, "False"));
2692
2484 op = 0; 2693 op = 0;
2485 optype = OP_NAME; 2694 optype = OP_NAME;
2486 scope = PyST_GetScope(c->u->u_ste, mangled); 2695 scope = PyST_GetScope(c->u->u_ste, mangled);
2487 switch (scope) { 2696 switch (scope) {
2488 case FREE: 2697 case FREE:
2489 dict = c->u->u_freevars; 2698 dict = c->u->u_freevars;
2490 optype = OP_DEREF; 2699 optype = OP_DEREF;
2491 break; 2700 break;
2492 case CELL: 2701 case CELL:
2493 dict = c->u->u_cellvars; 2702 dict = c->u->u_cellvars;
(...skipping 10 matching lines...) Expand all
2504 break; 2713 break;
2505 case GLOBAL_EXPLICIT: 2714 case GLOBAL_EXPLICIT:
2506 optype = OP_GLOBAL; 2715 optype = OP_GLOBAL;
2507 break; 2716 break;
2508 default: 2717 default:
2509 /* scope can be 0 */ 2718 /* scope can be 0 */
2510 break; 2719 break;
2511 } 2720 }
2512 2721
2513 /* XXX Leave assert here, but handle __doc__ and the like better */ 2722 /* XXX Leave assert here, but handle __doc__ and the like better */
2514 assert(scope || PyUnicode_AS_UNICODE(name)[0] == '_'); 2723 assert(scope || PyUnicode_READ_CHAR(name, 0) == '_');
2515 2724
2516 switch (optype) { 2725 switch (optype) {
2517 case OP_DEREF: 2726 case OP_DEREF:
2518 switch (ctx) { 2727 switch (ctx) {
2519 case Load: op = LOAD_DEREF; break; 2728 case Load:
2729 op = (c->u->u_ste->ste_type == ClassBlock) ? LOAD_CLASSDEREF : LOAD_ DEREF;
2730 break;
2520 case Store: op = STORE_DEREF; break; 2731 case Store: op = STORE_DEREF; break;
2521 case AugLoad: 2732 case AugLoad:
2522 case AugStore: 2733 case AugStore:
2523 break; 2734 break;
2524 case Del: op = DELETE_DEREF; break; 2735 case Del: op = DELETE_DEREF; break;
2525 case Param: 2736 case Param:
2526 default: 2737 default:
2527 PyErr_SetString(PyExc_SystemError, 2738 PyErr_SetString(PyExc_SystemError,
2528 "param invalid for deref variable"); 2739 "param invalid for deref variable");
2529 return 0; 2740 return 0;
(...skipping 352 matching lines...) Expand 10 before | Expand all | Expand 10 after
2882 3093
2883 return 1; 3094 return 1;
2884 } 3095 }
2885 3096
2886 static int 3097 static int
2887 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,
2888 asdl_seq *generators, expr_ty elt, expr_ty val) 3099 asdl_seq *generators, expr_ty elt, expr_ty val)
2889 { 3100 {
2890 PyCodeObject *co = NULL; 3101 PyCodeObject *co = NULL;
2891 expr_ty outermost_iter; 3102 expr_ty outermost_iter;
3103 PyObject *qualname = NULL;
2892 3104
2893 outermost_iter = ((comprehension_ty) 3105 outermost_iter = ((comprehension_ty)
2894 asdl_seq_GET(generators, 0))->iter; 3106 asdl_seq_GET(generators, 0))->iter;
2895 3107
2896 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))
2897 goto error; 3110 goto error;
2898 3111
2899 if (type != COMP_GENEXP) { 3112 if (type != COMP_GENEXP) {
2900 int op; 3113 int op;
2901 switch (type) { 3114 switch (type) {
2902 case COMP_LISTCOMP: 3115 case COMP_LISTCOMP:
2903 op = BUILD_LIST; 3116 op = BUILD_LIST;
2904 break; 3117 break;
2905 case COMP_SETCOMP: 3118 case COMP_SETCOMP:
2906 op = BUILD_SET; 3119 op = BUILD_SET;
(...skipping 12 matching lines...) Expand all
2919 3132
2920 if (!compiler_comprehension_generator(c, generators, 0, elt, 3133 if (!compiler_comprehension_generator(c, generators, 0, elt,
2921 val, type)) 3134 val, type))
2922 goto error_in_scope; 3135 goto error_in_scope;
2923 3136
2924 if (type != COMP_GENEXP) { 3137 if (type != COMP_GENEXP) {
2925 ADDOP(c, RETURN_VALUE); 3138 ADDOP(c, RETURN_VALUE);
2926 } 3139 }
2927 3140
2928 co = assemble(c, 1); 3141 co = assemble(c, 1);
3142 qualname = compiler_scope_qualname(c);
2929 compiler_exit_scope(c); 3143 compiler_exit_scope(c);
2930 if (co == NULL) 3144 if (qualname == NULL || co == NULL)
2931 goto error; 3145 goto error;
2932 3146
2933 if (!compiler_make_closure(c, co, 0)) 3147 if (!compiler_make_closure(c, co, 0, qualname))
2934 goto error; 3148 goto error;
3149 Py_DECREF(qualname);
2935 Py_DECREF(co); 3150 Py_DECREF(co);
2936 3151
2937 VISIT(c, expr, outermost_iter); 3152 VISIT(c, expr, outermost_iter);
2938 ADDOP(c, GET_ITER); 3153 ADDOP(c, GET_ITER);
2939 ADDOP_I(c, CALL_FUNCTION, 1); 3154 ADDOP_I(c, CALL_FUNCTION, 1);
2940 return 1; 3155 return 1;
2941 error_in_scope: 3156 error_in_scope:
2942 compiler_exit_scope(c); 3157 compiler_exit_scope(c);
2943 error: 3158 error:
3159 Py_XDECREF(qualname);
2944 Py_XDECREF(co); 3160 Py_XDECREF(co);
2945 return 0; 3161 return 0;
2946 } 3162 }
2947 3163
2948 static int 3164 static int
2949 compiler_genexp(struct compiler *c, expr_ty e) 3165 compiler_genexp(struct compiler *c, expr_ty e)
2950 { 3166 {
2951 static identifier name; 3167 static identifier name;
2952 if (!name) { 3168 if (!name) {
2953 name = PyUnicode_FromString("<genexpr>"); 3169 name = PyUnicode_FromString("<genexpr>");
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
3027 char *id; 3243 char *id;
3028 switch (e->kind) { 3244 switch (e->kind) {
3029 case Ellipsis_kind: 3245 case Ellipsis_kind:
3030 return 1; 3246 return 1;
3031 case Num_kind: 3247 case Num_kind:
3032 return PyObject_IsTrue(e->v.Num.n); 3248 return PyObject_IsTrue(e->v.Num.n);
3033 case Str_kind: 3249 case Str_kind:
3034 return PyObject_IsTrue(e->v.Str.s); 3250 return PyObject_IsTrue(e->v.Str.s);
3035 case Name_kind: 3251 case Name_kind:
3036 /* optimize away names that can't be reassigned */ 3252 /* optimize away names that can't be reassigned */
3037 id = PyBytes_AS_STRING( 3253 id = PyUnicode_AsUTF8(e->v.Name.id);
3038 _PyUnicode_AsDefaultEncodedString(e->v.Name.id)); 3254 if (id && strcmp(id, "__debug__") == 0)
3039 if (strcmp(id, "True") == 0) return 1; 3255 return !c->c_optimize;
3040 if (strcmp(id, "False") == 0) return 0; 3256 return -1;
3041 if (strcmp(id, "None") == 0) return 0; 3257 case NameConstant_kind: {
3042 if (strcmp(id, "__debug__") == 0) 3258 PyObject *o = e->v.NameConstant.value;
3043 return ! c->c_optimize; 3259 if (o == Py_None)
3044 /* fall through */ 3260 return 0;
3261 else if (o == Py_True)
3262 return 1;
3263 else if (o == Py_False)
3264 return 0;
3265 }
3045 default: 3266 default:
3046 return -1; 3267 return -1;
3047 } 3268 }
3048 } 3269 }
3049 3270
3050 /* 3271 /*
3051 Implements the with statement from PEP 343. 3272 Implements the with statement from PEP 343.
3052 3273
3053 The semantics outlined in that PEP are as follows: 3274 The semantics outlined in that PEP are as follows:
3054 3275
3055 with EXPR as VAR: 3276 with EXPR as VAR:
3056 BLOCK 3277 BLOCK
3057 3278
3058 It is implemented roughly as: 3279 It is implemented roughly as:
3059 3280
3060 context = EXPR 3281 context = EXPR
3061 exit = context.__exit__ # not calling it 3282 exit = context.__exit__ # not calling it
3062 value = context.__enter__() 3283 value = context.__enter__()
3063 try: 3284 try:
3064 VAR = value # if VAR present in the syntax 3285 VAR = value # if VAR present in the syntax
3065 BLOCK 3286 BLOCK
3066 finally: 3287 finally:
3067 if an exception was raised: 3288 if an exception was raised:
3068 exc = copy of (exception, instance, traceback) 3289 exc = copy of (exception, instance, traceback)
3069 else: 3290 else:
3070 exc = (None, None, None) 3291 exc = (None, None, None)
3071 exit(*exc) 3292 exit(*exc)
3072 */ 3293 */
3073 static int 3294 static int
3074 compiler_with(struct compiler *c, stmt_ty s) 3295 compiler_with(struct compiler *c, stmt_ty s, int pos)
3075 { 3296 {
3076 basicblock *block, *finally; 3297 basicblock *block, *finally;
3298 withitem_ty item = asdl_seq_GET(s->v.With.items, pos);
3077 3299
3078 assert(s->kind == With_kind); 3300 assert(s->kind == With_kind);
3079 3301
3080 block = compiler_new_block(c); 3302 block = compiler_new_block(c);
3081 finally = compiler_new_block(c); 3303 finally = compiler_new_block(c);
3082 if (!block || !finally) 3304 if (!block || !finally)
3083 return 0; 3305 return 0;
3084 3306
3085 /* Evaluate EXPR */ 3307 /* Evaluate EXPR */
3086 VISIT(c, expr, s->v.With.context_expr); 3308 VISIT(c, expr, item->context_expr);
3087 ADDOP_JREL(c, SETUP_WITH, finally); 3309 ADDOP_JREL(c, SETUP_WITH, finally);
3088 3310
3089 /* SETUP_WITH pushes a finally block. */ 3311 /* SETUP_WITH pushes a finally block. */
3090 compiler_use_next_block(c, block); 3312 compiler_use_next_block(c, block);
3091 if (!compiler_push_fblock(c, FINALLY_TRY, block)) { 3313 if (!compiler_push_fblock(c, FINALLY_TRY, block)) {
3092 return 0; 3314 return 0;
3093 } 3315 }
3094 3316
3095 if (s->v.With.optional_vars) { 3317 if (item->optional_vars) {
3096 VISIT(c, expr, s->v.With.optional_vars); 3318 VISIT(c, expr, item->optional_vars);
3097 } 3319 }
3098 else { 3320 else {
3099 /* Discard result from context.__enter__() */ 3321 /* Discard result from context.__enter__() */
3100 ADDOP(c, POP_TOP); 3322 ADDOP(c, POP_TOP);
3101 } 3323 }
3102 3324
3103 /* BLOCK code */ 3325 pos++;
3104 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;
3105 3331
3106 /* End of try block; start the finally block */ 3332 /* End of try block; start the finally block */
3107 ADDOP(c, POP_BLOCK); 3333 ADDOP(c, POP_BLOCK);
3108 compiler_pop_fblock(c, FINALLY_TRY, block); 3334 compiler_pop_fblock(c, FINALLY_TRY, block);
3109 3335
3110 ADDOP_O(c, LOAD_CONST, Py_None, consts); 3336 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3111 compiler_use_next_block(c, finally); 3337 compiler_use_next_block(c, finally);
3112 if (!compiler_push_fblock(c, FINALLY_END, finally)) 3338 if (!compiler_push_fblock(c, FINALLY_END, finally))
3113 return 0; 3339 return 0;
3114 3340
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
3181 if (c->u->u_ste->ste_type != FunctionBlock) 3407 if (c->u->u_ste->ste_type != FunctionBlock)
3182 return compiler_error(c, "'yield' outside function"); 3408 return compiler_error(c, "'yield' outside function");
3183 if (e->v.Yield.value) { 3409 if (e->v.Yield.value) {
3184 VISIT(c, expr, e->v.Yield.value); 3410 VISIT(c, expr, e->v.Yield.value);
3185 } 3411 }
3186 else { 3412 else {
3187 ADDOP_O(c, LOAD_CONST, Py_None, consts); 3413 ADDOP_O(c, LOAD_CONST, Py_None, consts);
3188 } 3414 }
3189 ADDOP(c, YIELD_VALUE); 3415 ADDOP(c, YIELD_VALUE);
3190 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;
3191 case Compare_kind: 3425 case Compare_kind:
3192 return compiler_compare(c, e); 3426 return compiler_compare(c, e);
3193 case Call_kind: 3427 case Call_kind:
3194 return compiler_call(c, e); 3428 return compiler_call(c, e);
3195 case Num_kind: 3429 case Num_kind:
3196 ADDOP_O(c, LOAD_CONST, e->v.Num.n, consts); 3430 ADDOP_O(c, LOAD_CONST, e->v.Num.n, consts);
3197 break; 3431 break;
3198 case Str_kind: 3432 case Str_kind:
3199 ADDOP_O(c, LOAD_CONST, e->v.Str.s, consts); 3433 ADDOP_O(c, LOAD_CONST, e->v.Str.s, consts);
3200 break; 3434 break;
3201 case Bytes_kind: 3435 case Bytes_kind:
3202 ADDOP_O(c, LOAD_CONST, e->v.Bytes.s, consts); 3436 ADDOP_O(c, LOAD_CONST, e->v.Bytes.s, consts);
3203 break; 3437 break;
3204 case Ellipsis_kind: 3438 case Ellipsis_kind:
3205 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);
3206 break; 3443 break;
3207 /* The following exprs can be assignment targets. */ 3444 /* The following exprs can be assignment targets. */
3208 case Attribute_kind: 3445 case Attribute_kind:
3209 if (e->v.Attribute.ctx != AugStore) 3446 if (e->v.Attribute.ctx != AugStore)
3210 VISIT(c, expr, e->v.Attribute.value); 3447 VISIT(c, expr, e->v.Attribute.value);
3211 switch (e->v.Attribute.ctx) { 3448 switch (e->v.Attribute.ctx) {
3212 case AugLoad: 3449 case AugLoad:
3213 ADDOP(c, DUP_TOP); 3450 ADDOP(c, DUP_TOP);
3214 /* Fall through to load */ 3451 /* Fall through to load */
3215 case Load: 3452 case Load:
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after
3366 /* Raises a SyntaxError and returns 0. 3603 /* Raises a SyntaxError and returns 0.
3367 If something goes wrong, a different exception may be raised. 3604 If something goes wrong, a different exception may be raised.
3368 */ 3605 */
3369 3606
3370 static int 3607 static int
3371 compiler_error(struct compiler *c, const char *errstr) 3608 compiler_error(struct compiler *c, const char *errstr)
3372 { 3609 {
3373 PyObject *loc; 3610 PyObject *loc;
3374 PyObject *u = NULL, *v = NULL; 3611 PyObject *u = NULL, *v = NULL;
3375 3612
3376 loc = PyErr_ProgramTextObject(c->filename, c->u->u_lineno); 3613 loc = PyErr_ProgramTextObject(c->c_filename, c->u->u_lineno);
3377 if (!loc) { 3614 if (!loc) {
3378 Py_INCREF(Py_None); 3615 Py_INCREF(Py_None);
3379 loc = Py_None; 3616 loc = Py_None;
3380 } 3617 }
3381 u = Py_BuildValue("(OiiO)", c->filename, c->u->u_lineno, 3618 u = Py_BuildValue("(OiiO)", c->c_filename, c->u->u_lineno,
3382 c->u->u_col_offset, loc); 3619 c->u->u_col_offset, loc);
3383 if (!u) 3620 if (!u)
3384 goto exit; 3621 goto exit;
3385 v = Py_BuildValue("(zO)", errstr, u); 3622 v = Py_BuildValue("(zO)", errstr, u);
3386 if (!v) 3623 if (!v)
3387 goto exit; 3624 goto exit;
3388 PyErr_SetObject(PyExc_SyntaxError, v); 3625 PyErr_SetObject(PyExc_SyntaxError, v);
3389 exit: 3626 exit:
3390 Py_DECREF(loc); 3627 Py_DECREF(loc);
3391 Py_XDECREF(u); 3628 Py_XDECREF(u);
(...skipping 487 matching lines...) Expand 10 before | Expand all | Expand 10 after
3879 PyTuple_SET_ITEM(tuple, i - offset, k); 4116 PyTuple_SET_ITEM(tuple, i - offset, k);
3880 } 4117 }
3881 return tuple; 4118 return tuple;
3882 } 4119 }
3883 4120
3884 static int 4121 static int
3885 compute_code_flags(struct compiler *c) 4122 compute_code_flags(struct compiler *c)
3886 { 4123 {
3887 PySTEntryObject *ste = c->u->u_ste; 4124 PySTEntryObject *ste = c->u->u_ste;
3888 int flags = 0, n; 4125 int flags = 0, n;
3889 if (ste->ste_type != ModuleBlock) 4126 if (ste->ste_type == FunctionBlock) {
3890 flags |= CO_NEWLOCALS; 4127 flags |= CO_NEWLOCALS;
3891 if (ste->ste_type == FunctionBlock) {
3892 if (!ste->ste_unoptimized) 4128 if (!ste->ste_unoptimized)
3893 flags |= CO_OPTIMIZED; 4129 flags |= CO_OPTIMIZED;
3894 if (ste->ste_nested) 4130 if (ste->ste_nested)
3895 flags |= CO_NESTED; 4131 flags |= CO_NESTED;
3896 if (ste->ste_generator) 4132 if (ste->ste_generator)
3897 flags |= CO_GENERATOR; 4133 flags |= CO_GENERATOR;
3898 if (ste->ste_varargs) 4134 if (ste->ste_varargs)
3899 flags |= CO_VARARGS; 4135 flags |= CO_VARARGS;
3900 if (ste->ste_varkeywords) 4136 if (ste->ste_varkeywords)
3901 flags |= CO_VARKEYWORDS; 4137 flags |= CO_VARKEYWORDS;
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
3943 varnames = dict_keys_inorder(c->u->u_varnames, 0); 4179 varnames = dict_keys_inorder(c->u->u_varnames, 0);
3944 if (!consts || !names || !varnames) 4180 if (!consts || !names || !varnames)
3945 goto error; 4181 goto error;
3946 4182
3947 cellvars = dict_keys_inorder(c->u->u_cellvars, 0); 4183 cellvars = dict_keys_inorder(c->u->u_cellvars, 0);
3948 if (!cellvars) 4184 if (!cellvars)
3949 goto error; 4185 goto error;
3950 freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_Size(cellvars)); 4186 freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_Size(cellvars));
3951 if (!freevars) 4187 if (!freevars)
3952 goto error; 4188 goto error;
3953
3954 nlocals = PyDict_Size(c->u->u_varnames); 4189 nlocals = PyDict_Size(c->u->u_varnames);
3955 flags = compute_code_flags(c); 4190 flags = compute_code_flags(c);
3956 if (flags < 0) 4191 if (flags < 0)
3957 goto error; 4192 goto error;
3958 4193
3959 bytecode = PyCode_Optimize(a->a_bytecode, consts, names, a->a_lnotab); 4194 bytecode = PyCode_Optimize(a->a_bytecode, consts, names, a->a_lnotab);
3960 if (!bytecode) 4195 if (!bytecode)
3961 goto error; 4196 goto error;
3962 4197
3963 tmp = PyList_AsTuple(consts); /* PyCode_New requires a tuple */ 4198 tmp = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
3964 if (!tmp) 4199 if (!tmp)
3965 goto error; 4200 goto error;
3966 Py_DECREF(consts); 4201 Py_DECREF(consts);
3967 consts = tmp; 4202 consts = tmp;
3968 4203
3969 co = PyCode_New(c->u->u_argcount, c->u->u_kwonlyargcount, 4204 co = PyCode_New(c->u->u_argcount, c->u->u_kwonlyargcount,
3970 nlocals, stackdepth(c), flags, 4205 nlocals, stackdepth(c), flags,
3971 bytecode, consts, names, varnames, 4206 bytecode, consts, names, varnames,
3972 freevars, cellvars, 4207 freevars, cellvars,
3973 c->filename, c->u->u_name, 4208 c->c_filename, c->u->u_name,
3974 c->u->u_firstlineno, 4209 c->u->u_firstlineno,
3975 a->a_lnotab); 4210 a->a_lnotab);
3976 error: 4211 error:
3977 Py_XDECREF(consts); 4212 Py_XDECREF(consts);
3978 Py_XDECREF(names); 4213 Py_XDECREF(names);
3979 Py_XDECREF(varnames); 4214 Py_XDECREF(varnames);
3980 Py_XDECREF(name); 4215 Py_XDECREF(name);
3981 Py_XDECREF(freevars); 4216 Py_XDECREF(freevars);
3982 Py_XDECREF(cellvars); 4217 Py_XDECREF(cellvars);
3983 Py_XDECREF(bytecode); 4218 Py_XDECREF(bytecode);
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
4080 4315
4081 #undef PyAST_Compile 4316 #undef PyAST_Compile
4082 PyAPI_FUNC(PyCodeObject *) 4317 PyAPI_FUNC(PyCodeObject *)
4083 PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags, 4318 PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags,
4084 PyArena *arena) 4319 PyArena *arena)
4085 { 4320 {
4086 return PyAST_CompileEx(mod, filename, flags, -1, arena); 4321 return PyAST_CompileEx(mod, filename, flags, -1, arena);
4087 } 4322 }
4088 4323
4089 4324
LEFTRIGHT

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