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

Side by Side Diff: Python/compile.c

Issue 11619: On Windows, don't encode filenames in the import machinery
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:
View unified diff | Download patch
« no previous file with comments | « Python/ast.c ('k') | Python/errors.c » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
142 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
143 managed by compiler_enter_scope() and compiler_exit_scope(). 143 managed by compiler_enter_scope() and compiler_exit_scope().
144 144
145 Note that we don't track recursion levels during compilation - the 145 Note that we don't track recursion levels during compilation - the
146 task of detecting and rejecting excessive levels of nesting is 146 task of detecting and rejecting excessive levels of nesting is
147 handled by the symbol analysis pass. 147 handled by the symbol analysis pass.
148 148
149 */ 149 */
150 150
151 struct compiler { 151 struct compiler {
152 const char *c_filename; 152 PyObject *c_filename;
153 PyObject *c_filename_obj;
154 struct symtable *c_st; 153 struct symtable *c_st;
155 PyFutureFeatures *c_future; /* pointer to module's __future__ */ 154 PyFutureFeatures *c_future; /* pointer to module's __future__ */
156 PyCompilerFlags *c_flags; 155 PyCompilerFlags *c_flags;
157 156
158 int c_optimize; /* optimization level */ 157 int c_optimize; /* optimization level */
159 int c_interactive; /* true if in interactive mode */ 158 int c_interactive; /* true if in interactive mode */
160 int c_nestlevel; 159 int c_nestlevel;
161 160
162 struct compiler_unit *u; /* compiler state for current block */ 161 struct compiler_unit *u; /* compiler state for current block */
163 PyObject *c_stack; /* Python list holding compiler_unit ptrs */ 162 PyObject *c_stack; /* Python list holding compiler_unit ptrs */
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
281 memset(c, 0, sizeof(struct compiler)); 280 memset(c, 0, sizeof(struct compiler));
282 281
283 c->c_stack = PyList_New(0); 282 c->c_stack = PyList_New(0);
284 if (!c->c_stack) 283 if (!c->c_stack)
285 return 0; 284 return 0;
286 285
287 return 1; 286 return 1;
288 } 287 }
289 288
290 PyCodeObject * 289 PyCodeObject *
291 PyAST_CompileEx(mod_ty mod, const char *filename, PyCompilerFlags *flags, 290 PyAST_CompileObject(mod_ty mod, PyObject *filename, PyCompilerFlags *flags,
292 int optimize, PyArena *arena) 291 int optimize, PyArena *arena)
293 { 292 {
294 struct compiler c; 293 struct compiler c;
295 PyCodeObject *co = NULL; 294 PyCodeObject *co = NULL;
296 PyCompilerFlags local_flags; 295 PyCompilerFlags local_flags;
297 int merged; 296 int merged;
298 297
299 if (!__doc__) { 298 if (!__doc__) {
300 __doc__ = PyUnicode_InternFromString("__doc__"); 299 __doc__ = PyUnicode_InternFromString("__doc__");
301 if (!__doc__) 300 if (!__doc__)
302 return NULL; 301 return NULL;
303 } 302 }
304 303
305 if (!compiler_init(&c)) 304 if (!compiler_init(&c))
306 return NULL; 305 return NULL;
306 Py_INCREF(filename);
307 c.c_filename = filename; 307 c.c_filename = filename;
308 c.c_filename_obj = PyUnicode_DecodeFSDefault(filename);
309 if (!c.c_filename_obj)
310 goto finally;
311 c.c_arena = arena; 308 c.c_arena = arena;
312 c.c_future = PyFuture_FromAST(mod, filename); 309 c.c_future = PyFuture_FromASTObject(mod, filename);
313 if (c.c_future == NULL) 310 if (c.c_future == NULL)
314 goto finally; 311 goto finally;
315 if (!flags) { 312 if (!flags) {
316 local_flags.cf_flags = 0; 313 local_flags.cf_flags = 0;
317 flags = &local_flags; 314 flags = &local_flags;
318 } 315 }
319 merged = c.c_future->ff_features | flags->cf_flags; 316 merged = c.c_future->ff_features | flags->cf_flags;
320 c.c_future->ff_features = merged; 317 c.c_future->ff_features = merged;
321 flags->cf_flags = merged; 318 flags->cf_flags = merged;
322 c.c_flags = flags; 319 c.c_flags = flags;
323 c.c_optimize = (optimize == -1) ? Py_OptimizeFlag : optimize; 320 c.c_optimize = (optimize == -1) ? Py_OptimizeFlag : optimize;
324 c.c_nestlevel = 0; 321 c.c_nestlevel = 0;
325 322
326 c.c_st = PySymtable_Build(mod, filename, c.c_future); 323 c.c_st = PySymtable_BuildObject(mod, filename, c.c_future);
327 if (c.c_st == NULL) { 324 if (c.c_st == NULL) {
328 if (!PyErr_Occurred()) 325 if (!PyErr_Occurred())
329 PyErr_SetString(PyExc_SystemError, "no symtable"); 326 PyErr_SetString(PyExc_SystemError, "no symtable");
330 goto finally; 327 goto finally;
331 } 328 }
332 329
333 co = compiler_mod(&c, mod); 330 co = compiler_mod(&c, mod);
334 331
335 finally: 332 finally:
336 compiler_free(&c); 333 compiler_free(&c);
337 assert(co || PyErr_Occurred()); 334 assert(co || PyErr_Occurred());
338 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
339 } 351 }
340 352
341 PyCodeObject * 353 PyCodeObject *
342 PyNode_Compile(struct _node *n, const char *filename) 354 PyNode_Compile(struct _node *n, const char *filename)
343 { 355 {
344 PyCodeObject *co = NULL; 356 PyCodeObject *co = NULL;
345 mod_ty mod; 357 mod_ty mod;
346 PyArena *arena = PyArena_New(); 358 PyArena *arena = PyArena_New();
347 if (!arena) 359 if (!arena)
348 return NULL; 360 return NULL;
349 mod = PyAST_FromNode(n, NULL, filename, arena); 361 mod = PyAST_FromNode(n, NULL, filename, arena);
350 if (mod) 362 if (mod)
351 co = PyAST_Compile(mod, filename, NULL, arena); 363 co = PyAST_Compile(mod, filename, NULL, arena);
352 PyArena_Free(arena); 364 PyArena_Free(arena);
353 return co; 365 return co;
354 } 366 }
355 367
356 static void 368 static void
357 compiler_free(struct compiler *c) 369 compiler_free(struct compiler *c)
358 { 370 {
359 if (c->c_st) 371 if (c->c_st)
360 PySymtable_Free(c->c_st); 372 PySymtable_Free(c->c_st);
361 if (c->c_future) 373 if (c->c_future)
362 PyObject_Free(c->c_future); 374 PyObject_Free(c->c_future);
363 if (c->c_filename_obj)
364 Py_DECREF(c->c_filename_obj);
365 Py_DECREF(c->c_stack); 375 Py_DECREF(c->c_stack);
376 Py_XDECREF(c->c_filename);
366 } 377 }
367 378
368 static PyObject * 379 static PyObject *
369 list2dict(PyObject *list) 380 list2dict(PyObject *list)
370 { 381 {
371 Py_ssize_t i, n; 382 Py_ssize_t i, n;
372 PyObject *v, *k; 383 PyObject *v, *k;
373 PyObject *dict = PyDict_New(); 384 PyObject *dict = PyDict_New();
374 if (!dict) return NULL; 385 if (!dict) return NULL;
375 386
(...skipping 985 matching lines...) Expand 10 before | Expand all | Expand 10 after
1361 get_ref_type(struct compiler *c, PyObject *name) 1372 get_ref_type(struct compiler *c, PyObject *name)
1362 { 1373 {
1363 int scope; 1374 int scope;
1364 if (c->u->u_scope_type == COMPILER_SCOPE_CLASS && 1375 if (c->u->u_scope_type == COMPILER_SCOPE_CLASS &&
1365 !PyUnicode_CompareWithASCIIString(name, "__class__")) 1376 !PyUnicode_CompareWithASCIIString(name, "__class__"))
1366 return CELL; 1377 return CELL;
1367 scope = PyST_GetScope(c->u->u_ste, name); 1378 scope = PyST_GetScope(c->u->u_ste, name);
1368 if (scope == 0) { 1379 if (scope == 0) {
1369 char buf[350]; 1380 char buf[350];
1370 PyOS_snprintf(buf, sizeof(buf), 1381 PyOS_snprintf(buf, sizeof(buf),
1371 "unknown scope for %.100s in %.100s(%s) in %s\n" 1382 "unknown scope for %.100s in %.100s(%s)\n"
1372 "symbols: %s\nlocals: %s\nglobals: %s", 1383 "symbols: %s\nlocals: %s\nglobals: %s",
1373 PyBytes_AS_STRING(name), 1384 PyBytes_AS_STRING(name),
1374 PyBytes_AS_STRING(c->u->u_name), 1385 PyBytes_AS_STRING(c->u->u_name),
1375 PyObject_REPR(c->u->u_ste->ste_id), 1386 PyObject_REPR(c->u->u_ste->ste_id),
1376 c->c_filename,
1377 PyObject_REPR(c->u->u_ste->ste_symbols), 1387 PyObject_REPR(c->u->u_ste->ste_symbols),
1378 PyObject_REPR(c->u->u_varnames), 1388 PyObject_REPR(c->u->u_varnames),
1379 PyObject_REPR(c->u->u_names) 1389 PyObject_REPR(c->u->u_names)
1380 ); 1390 );
1381 Py_FatalError(buf); 1391 Py_FatalError(buf);
1382 } 1392 }
1383 1393
1384 return scope; 1394 return scope;
1385 } 1395 }
1386 1396
(...skipping 1017 matching lines...) Expand 10 before | Expand all | Expand 10 after
2404 /* remove imported module */ 2414 /* remove imported module */
2405 ADDOP(c, POP_TOP); 2415 ADDOP(c, POP_TOP);
2406 return 1; 2416 return 1;
2407 } 2417 }
2408 2418
2409 static int 2419 static int
2410 compiler_assert(struct compiler *c, stmt_ty s) 2420 compiler_assert(struct compiler *c, stmt_ty s)
2411 { 2421 {
2412 static PyObject *assertion_error = NULL; 2422 static PyObject *assertion_error = NULL;
2413 basicblock *end; 2423 basicblock *end;
2424 PyObject* msg;
2414 2425
2415 if (c->c_optimize) 2426 if (c->c_optimize)
2416 return 1; 2427 return 1;
2417 if (assertion_error == NULL) { 2428 if (assertion_error == NULL) {
2418 assertion_error = PyUnicode_InternFromString("AssertionError"); 2429 assertion_error = PyUnicode_InternFromString("AssertionError");
2419 if (assertion_error == NULL) 2430 if (assertion_error == NULL)
2420 return 0; 2431 return 0;
2421 } 2432 }
2422 if (s->v.Assert.test->kind == Tuple_kind && 2433 if (s->v.Assert.test->kind == Tuple_kind &&
2423 asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) { 2434 asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) {
2424 const char* msg = 2435 msg = PyUnicode_FromString("assertion is always true, "
2425 "assertion is always true, perhaps remove parentheses?"; 2436 "perhaps remove parentheses?");
2426 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, c->c_filename, 2437 if (msg == NULL)
2427 c->u->u_lineno, NULL, NULL) == -1)
2428 return 0; 2438 return 0;
2439 if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg,
2440 c->c_filename, c->u->u_lineno,
2441 NULL, NULL) == -1) {
2442 Py_DECREF(msg);
2443 return 0;
2444 }
2445 Py_DECREF(msg);
2429 } 2446 }
2430 VISIT(c, expr, s->v.Assert.test); 2447 VISIT(c, expr, s->v.Assert.test);
2431 end = compiler_new_block(c); 2448 end = compiler_new_block(c);
2432 if (end == NULL) 2449 if (end == NULL)
2433 return 0; 2450 return 0;
2434 ADDOP_JABS(c, POP_JUMP_IF_TRUE, end); 2451 ADDOP_JABS(c, POP_JUMP_IF_TRUE, end);
2435 ADDOP_O(c, LOAD_GLOBAL, assertion_error, names); 2452 ADDOP_O(c, LOAD_GLOBAL, assertion_error, names);
2436 if (s->v.Assert.msg) { 2453 if (s->v.Assert.msg) {
2437 VISIT(c, expr, s->v.Assert.msg); 2454 VISIT(c, expr, s->v.Assert.msg);
2438 ADDOP_I(c, CALL_FUNCTION, 1); 2455 ADDOP_I(c, CALL_FUNCTION, 1);
(...skipping 1147 matching lines...) Expand 10 before | Expand all | Expand 10 after
3586 /* Raises a SyntaxError and returns 0. 3603 /* Raises a SyntaxError and returns 0.
3587 If something goes wrong, a different exception may be raised. 3604 If something goes wrong, a different exception may be raised.
3588 */ 3605 */
3589 3606
3590 static int 3607 static int
3591 compiler_error(struct compiler *c, const char *errstr) 3608 compiler_error(struct compiler *c, const char *errstr)
3592 { 3609 {
3593 PyObject *loc; 3610 PyObject *loc;
3594 PyObject *u = NULL, *v = NULL; 3611 PyObject *u = NULL, *v = NULL;
3595 3612
3596 loc = PyErr_ProgramText(c->c_filename, c->u->u_lineno); 3613 loc = PyErr_ProgramTextObject(c->c_filename, c->u->u_lineno);
3597 if (!loc) { 3614 if (!loc) {
3598 Py_INCREF(Py_None); 3615 Py_INCREF(Py_None);
3599 loc = Py_None; 3616 loc = Py_None;
3600 } 3617 }
3601 u = Py_BuildValue("(OiiO)", c->c_filename_obj, c->u->u_lineno, 3618 u = Py_BuildValue("(OiiO)", c->c_filename, c->u->u_lineno,
3602 c->u->u_col_offset, loc); 3619 c->u->u_col_offset, loc);
3603 if (!u) 3620 if (!u)
3604 goto exit; 3621 goto exit;
3605 v = Py_BuildValue("(zO)", errstr, u); 3622 v = Py_BuildValue("(zO)", errstr, u);
3606 if (!v) 3623 if (!v)
3607 goto exit; 3624 goto exit;
3608 PyErr_SetObject(PyExc_SyntaxError, v); 3625 PyErr_SetObject(PyExc_SyntaxError, v);
3609 exit: 3626 exit:
3610 Py_DECREF(loc); 3627 Py_DECREF(loc);
3611 Py_XDECREF(u); 3628 Py_XDECREF(u);
(...skipping 569 matching lines...) Expand 10 before | Expand all | Expand 10 after
4181 tmp = PyList_AsTuple(consts); /* PyCode_New requires a tuple */ 4198 tmp = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
4182 if (!tmp) 4199 if (!tmp)
4183 goto error; 4200 goto error;
4184 Py_DECREF(consts); 4201 Py_DECREF(consts);
4185 consts = tmp; 4202 consts = tmp;
4186 4203
4187 co = PyCode_New(c->u->u_argcount, c->u->u_kwonlyargcount, 4204 co = PyCode_New(c->u->u_argcount, c->u->u_kwonlyargcount,
4188 nlocals, stackdepth(c), flags, 4205 nlocals, stackdepth(c), flags,
4189 bytecode, consts, names, varnames, 4206 bytecode, consts, names, varnames,
4190 freevars, cellvars, 4207 freevars, cellvars,
4191 c->c_filename_obj, c->u->u_name, 4208 c->c_filename, c->u->u_name,
4192 c->u->u_firstlineno, 4209 c->u->u_firstlineno,
4193 a->a_lnotab); 4210 a->a_lnotab);
4194 error: 4211 error:
4195 Py_XDECREF(consts); 4212 Py_XDECREF(consts);
4196 Py_XDECREF(names); 4213 Py_XDECREF(names);
4197 Py_XDECREF(varnames); 4214 Py_XDECREF(varnames);
4198 Py_XDECREF(name); 4215 Py_XDECREF(name);
4199 Py_XDECREF(freevars); 4216 Py_XDECREF(freevars);
4200 Py_XDECREF(cellvars); 4217 Py_XDECREF(cellvars);
4201 Py_XDECREF(bytecode); 4218 Py_XDECREF(bytecode);
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
4298 4315
4299 #undef PyAST_Compile 4316 #undef PyAST_Compile
4300 PyAPI_FUNC(PyCodeObject *) 4317 PyAPI_FUNC(PyCodeObject *)
4301 PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags, 4318 PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags,
4302 PyArena *arena) 4319 PyArena *arena)
4303 { 4320 {
4304 return PyAST_CompileEx(mod, filename, flags, -1, arena); 4321 return PyAST_CompileEx(mod, filename, flags, -1, arena);
4305 } 4322 }
4306 4323
4307 4324
OLDNEW
« no previous file with comments | « Python/ast.c ('k') | Python/errors.c » ('j') | no next file with comments »

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