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

Side by Side Diff: Python/compile.c

Issue 11619: On Windows, don't encode filenames in the import machinery
Patch Set: Created 8 years, 5 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 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
127 }; 127 };
128 128
129 /* This struct captures the global state of a compilation. 129 /* This struct captures the global state of a compilation.
130 130
131 The u pointer points to the current compilation unit, while units 131 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 132 for enclosing blocks are stored in c_stack. The u and c_stack are
133 managed by compiler_enter_scope() and compiler_exit_scope(). 133 managed by compiler_enter_scope() and compiler_exit_scope().
134 */ 134 */
135 135
136 struct compiler { 136 struct compiler {
137 const char *c_filename; 137 PyObject *filename;
138 struct symtable *c_st; 138 struct symtable *c_st;
139 PyFutureFeatures *c_future; /* pointer to module's __future__ */ 139 PyFutureFeatures *c_future; /* pointer to module's __future__ */
140 PyCompilerFlags *c_flags; 140 PyCompilerFlags *c_flags;
141 141
142 int c_optimize; /* optimization level */ 142 int c_optimize; /* optimization level */
143 int c_interactive; /* true if in interactive mode */ 143 int c_interactive; /* true if in interactive mode */
144 int c_nestlevel; 144 int c_nestlevel;
145 145
146 struct compiler_unit *u; /* compiler state for current block */ 146 struct compiler_unit *u; /* compiler state for current block */
147 PyObject *c_stack; /* Python list holding compiler_unit ptrs */ 147 PyObject *c_stack; /* Python list holding compiler_unit ptrs */
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
248 memset(c, 0, sizeof(struct compiler)); 248 memset(c, 0, sizeof(struct compiler));
249 249
250 c->c_stack = PyList_New(0); 250 c->c_stack = PyList_New(0);
251 if (!c->c_stack) 251 if (!c->c_stack)
252 return 0; 252 return 0;
253 253
254 return 1; 254 return 1;
255 } 255 }
256 256
257 PyCodeObject * 257 PyCodeObject *
258 PyAST_CompileEx(mod_ty mod, const char *filename, PyCompilerFlags *flags, 258 PyAST_CompileObject(mod_ty mod, PyObject *filename, PyCompilerFlags *flags,
259 int optimize, PyArena *arena) 259 int optimize, PyArena *arena)
260 { 260 {
261 struct compiler c; 261 struct compiler c;
262 PyCodeObject *co = NULL; 262 PyCodeObject *co = NULL;
263 PyCompilerFlags local_flags; 263 PyCompilerFlags local_flags;
264 int merged; 264 int merged;
265 265
266 if (!__doc__) { 266 if (!__doc__) {
267 __doc__ = PyUnicode_InternFromString("__doc__"); 267 __doc__ = PyUnicode_InternFromString("__doc__");
268 if (!__doc__) 268 if (!__doc__)
269 return NULL; 269 return NULL;
270 } 270 }
271 271
272 if (!compiler_init(&c)) 272 if (!compiler_init(&c))
273 return NULL; 273 return NULL;
274 c.c_filename = filename; 274 Py_INCREF(filename);
275 c.filename = filename;
275 c.c_arena = arena; 276 c.c_arena = arena;
276 c.c_future = PyFuture_FromAST(mod, filename); 277 c.c_future = PyFuture_FromASTObject(mod, filename);
277 if (c.c_future == NULL) 278 if (c.c_future == NULL)
278 goto finally; 279 goto finally;
279 if (!flags) { 280 if (!flags) {
280 local_flags.cf_flags = 0; 281 local_flags.cf_flags = 0;
281 flags = &local_flags; 282 flags = &local_flags;
282 } 283 }
283 merged = c.c_future->ff_features | flags->cf_flags; 284 merged = c.c_future->ff_features | flags->cf_flags;
284 c.c_future->ff_features = merged; 285 c.c_future->ff_features = merged;
285 flags->cf_flags = merged; 286 flags->cf_flags = merged;
286 c.c_flags = flags; 287 c.c_flags = flags;
287 c.c_optimize = (optimize == -1) ? Py_OptimizeFlag : optimize; 288 c.c_optimize = (optimize == -1) ? Py_OptimizeFlag : optimize;
288 c.c_nestlevel = 0; 289 c.c_nestlevel = 0;
289 290
290 c.c_st = PySymtable_Build(mod, filename, c.c_future); 291 c.c_st = PySymtable_BuildObject(mod, filename, c.c_future);
291 if (c.c_st == NULL) { 292 if (c.c_st == NULL) {
292 if (!PyErr_Occurred()) 293 if (!PyErr_Occurred())
293 PyErr_SetString(PyExc_SystemError, "no symtable"); 294 PyErr_SetString(PyExc_SystemError, "no symtable");
294 goto finally; 295 goto finally;
295 } 296 }
296 297
297 co = compiler_mod(&c, mod); 298 co = compiler_mod(&c, mod);
298 299
299 finally: 300 finally:
300 compiler_free(&c); 301 compiler_free(&c);
301 assert(co || PyErr_Occurred()); 302 assert(co || PyErr_Occurred());
302 return co; 303 return co;
304 }
305
306 PyCodeObject *
307 PyAST_CompileEx(mod_ty mod, const char *filename_str, PyCompilerFlags *flags,
308 int optimize, PyArena *arena)
309 {
310 PyObject *filename;
311 PyCodeObject *co;
312 filename = PyUnicode_DecodeFSDefault(filename_str);
313 if (filename == NULL)
314 return NULL;
315 co = PyAST_CompileObject(mod, filename, flags, optimize, arena);
316 Py_DECREF(filename);
317 return co;
318
303 } 319 }
304 320
305 PyCodeObject * 321 PyCodeObject *
306 PyNode_Compile(struct _node *n, const char *filename) 322 PyNode_Compile(struct _node *n, const char *filename)
307 { 323 {
308 PyCodeObject *co = NULL; 324 PyCodeObject *co = NULL;
309 mod_ty mod; 325 mod_ty mod;
310 PyArena *arena = PyArena_New(); 326 PyArena *arena = PyArena_New();
311 if (!arena) 327 if (!arena)
312 return NULL; 328 return NULL;
313 mod = PyAST_FromNode(n, NULL, filename, arena); 329 mod = PyAST_FromNode(n, NULL, filename, arena);
314 if (mod) 330 if (mod)
315 co = PyAST_Compile(mod, filename, NULL, arena); 331 co = PyAST_Compile(mod, filename, NULL, arena);
316 PyArena_Free(arena); 332 PyArena_Free(arena);
317 return co; 333 return co;
318 } 334 }
319 335
320 static void 336 static void
321 compiler_free(struct compiler *c) 337 compiler_free(struct compiler *c)
322 { 338 {
323 if (c->c_st) 339 if (c->c_st)
324 PySymtable_Free(c->c_st); 340 PySymtable_Free(c->c_st);
325 if (c->c_future) 341 if (c->c_future)
326 PyObject_Free(c->c_future); 342 PyObject_Free(c->c_future);
327 Py_DECREF(c->c_stack); 343 Py_DECREF(c->c_stack);
344 Py_XDECREF(c->filename);
328 } 345 }
329 346
330 static PyObject * 347 static PyObject *
331 list2dict(PyObject *list) 348 list2dict(PyObject *list)
332 { 349 {
333 Py_ssize_t i, n; 350 Py_ssize_t i, n;
334 PyObject *v, *k; 351 PyObject *v, *k;
335 PyObject *dict = PyDict_New(); 352 PyObject *dict = PyDict_New();
336 if (!dict) return NULL; 353 if (!dict) return NULL;
337 354
(...skipping 875 matching lines...) Expand 10 before | Expand all | Expand 10 after
1213 a method and the free var is a free var referenced within a method. 1230 a method and the free var is a free var referenced within a method.
1214 */ 1231 */
1215 1232
1216 static int 1233 static int
1217 get_ref_type(struct compiler *c, PyObject *name) 1234 get_ref_type(struct compiler *c, PyObject *name)
1218 { 1235 {
1219 int scope = PyST_GetScope(c->u->u_ste, name); 1236 int scope = PyST_GetScope(c->u->u_ste, name);
1220 if (scope == 0) { 1237 if (scope == 0) {
1221 char buf[350]; 1238 char buf[350];
1222 PyOS_snprintf(buf, sizeof(buf), 1239 PyOS_snprintf(buf, sizeof(buf),
1223 "unknown scope for %.100s in %.100s(%s) in %s\n" 1240 "unknown scope for %.100s in %.100s(%s)\n"
1224 "symbols: %s\nlocals: %s\nglobals: %s", 1241 "symbols: %s\nlocals: %s\nglobals: %s",
1225 PyBytes_AS_STRING(name), 1242 PyBytes_AS_STRING(name),
1226 PyBytes_AS_STRING(c->u->u_name), 1243 PyBytes_AS_STRING(c->u->u_name),
1227 PyObject_REPR(c->u->u_ste->ste_id), 1244 PyObject_REPR(c->u->u_ste->ste_id),
1228 c->c_filename,
1229 PyObject_REPR(c->u->u_ste->ste_symbols), 1245 PyObject_REPR(c->u->u_ste->ste_symbols),
1230 PyObject_REPR(c->u->u_varnames), 1246 PyObject_REPR(c->u->u_varnames),
1231 PyObject_REPR(c->u->u_names) 1247 PyObject_REPR(c->u->u_names)
1232 ); 1248 );
1233 Py_FatalError(buf); 1249 Py_FatalError(buf);
1234 } 1250 }
1235 1251
1236 return scope; 1252 return scope;
1237 } 1253 }
1238 1254
(...skipping 966 matching lines...) Expand 10 before | Expand all | Expand 10 after
2205 /* remove imported module */ 2221 /* remove imported module */
2206 ADDOP(c, POP_TOP); 2222 ADDOP(c, POP_TOP);
2207 return 1; 2223 return 1;
2208 } 2224 }
2209 2225
2210 static int 2226 static int
2211 compiler_assert(struct compiler *c, stmt_ty s) 2227 compiler_assert(struct compiler *c, stmt_ty s)
2212 { 2228 {
2213 static PyObject *assertion_error = NULL; 2229 static PyObject *assertion_error = NULL;
2214 basicblock *end; 2230 basicblock *end;
2231 PyObject* msg;
2215 2232
2216 if (c->c_optimize) 2233 if (c->c_optimize)
2217 return 1; 2234 return 1;
2218 if (assertion_error == NULL) { 2235 if (assertion_error == NULL) {
2219 assertion_error = PyUnicode_InternFromString("AssertionError"); 2236 assertion_error = PyUnicode_InternFromString("AssertionError");
2220 if (assertion_error == NULL) 2237 if (assertion_error == NULL)
2221 return 0; 2238 return 0;
2222 } 2239 }
2223 if (s->v.Assert.test->kind == Tuple_kind && 2240 if (s->v.Assert.test->kind == Tuple_kind &&
2224 asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) { 2241 asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) {
2225 const char* msg = 2242 msg = PyUnicode_FromString("assertion is always true, "
2226 "assertion is always true, perhaps remove parentheses?"; 2243 "perhaps remove parentheses?");
2227 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, c->c_filename, 2244 if (msg == NULL)
2228 c->u->u_lineno, NULL, NULL) == -1)
2229 return 0; 2245 return 0;
2246 if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg,
2247 c->filename, c->u->u_lineno,
2248 NULL, NULL) == -1) {
2249 Py_DECREF(msg);
2250 return 0;
2251 }
2252 Py_DECREF(msg);
2230 } 2253 }
2231 VISIT(c, expr, s->v.Assert.test); 2254 VISIT(c, expr, s->v.Assert.test);
2232 end = compiler_new_block(c); 2255 end = compiler_new_block(c);
2233 if (end == NULL) 2256 if (end == NULL)
2234 return 0; 2257 return 0;
2235 ADDOP_JABS(c, POP_JUMP_IF_TRUE, end); 2258 ADDOP_JABS(c, POP_JUMP_IF_TRUE, end);
2236 ADDOP_O(c, LOAD_GLOBAL, assertion_error, names); 2259 ADDOP_O(c, LOAD_GLOBAL, assertion_error, names);
2237 if (s->v.Assert.msg) { 2260 if (s->v.Assert.msg) {
2238 VISIT(c, expr, s->v.Assert.msg); 2261 VISIT(c, expr, s->v.Assert.msg);
2239 ADDOP_I(c, CALL_FUNCTION, 1); 2262 ADDOP_I(c, CALL_FUNCTION, 1);
(...skipping 1114 matching lines...) Expand 10 before | Expand all | Expand 10 after
3354 } 3377 }
3355 return 0; 3378 return 0;
3356 } 3379 }
3357 /* Raises a SyntaxError and returns 0. 3380 /* Raises a SyntaxError and returns 0.
3358 If something goes wrong, a different exception may be raised. 3381 If something goes wrong, a different exception may be raised.
3359 */ 3382 */
3360 3383
3361 static int 3384 static int
3362 compiler_error(struct compiler *c, const char *errstr) 3385 compiler_error(struct compiler *c, const char *errstr)
3363 { 3386 {
3364 PyObject *loc, *filename; 3387 PyObject *loc;
3365 PyObject *u = NULL, *v = NULL; 3388 PyObject *u = NULL, *v = NULL;
3366 3389
3367 loc = PyErr_ProgramText(c->c_filename, c->u->u_lineno); 3390 loc = PyErr_ProgramTextObject(c->filename, c->u->u_lineno);
3368 if (!loc) { 3391 if (!loc) {
3369 Py_INCREF(Py_None); 3392 Py_INCREF(Py_None);
3370 loc = Py_None; 3393 loc = Py_None;
3371 } 3394 }
3372 if (c->c_filename != NULL) { 3395 u = Py_BuildValue("(OiiO)", c->filename, c->u->u_lineno,
3373 filename = PyUnicode_DecodeFSDefault(c->c_filename);
3374 if (!filename)
3375 goto exit;
3376 }
3377 else {
3378 Py_INCREF(Py_None);
3379 filename = Py_None;
3380 }
3381 u = Py_BuildValue("(NiiO)", filename, c->u->u_lineno,
3382 c->u->u_col_offset, loc); 3396 c->u->u_col_offset, loc);
3383 if (!u) 3397 if (!u)
3384 goto exit; 3398 goto exit;
3385 v = Py_BuildValue("(zO)", errstr, u); 3399 v = Py_BuildValue("(zO)", errstr, u);
3386 if (!v) 3400 if (!v)
3387 goto exit; 3401 goto exit;
3388 PyErr_SetObject(PyExc_SyntaxError, v); 3402 PyErr_SetObject(PyExc_SyntaxError, v);
3389 exit: 3403 exit:
3390 Py_DECREF(loc); 3404 Py_DECREF(loc);
3391 Py_XDECREF(u); 3405 Py_XDECREF(u);
(...skipping 528 matching lines...) Expand 10 before | Expand all | Expand 10 after
3920 } 3934 }
3921 3935
3922 static PyCodeObject * 3936 static PyCodeObject *
3923 makecode(struct compiler *c, struct assembler *a) 3937 makecode(struct compiler *c, struct assembler *a)
3924 { 3938 {
3925 PyObject *tmp; 3939 PyObject *tmp;
3926 PyCodeObject *co = NULL; 3940 PyCodeObject *co = NULL;
3927 PyObject *consts = NULL; 3941 PyObject *consts = NULL;
3928 PyObject *names = NULL; 3942 PyObject *names = NULL;
3929 PyObject *varnames = NULL; 3943 PyObject *varnames = NULL;
3930 PyObject *filename = NULL;
3931 PyObject *name = NULL; 3944 PyObject *name = NULL;
3932 PyObject *freevars = NULL; 3945 PyObject *freevars = NULL;
3933 PyObject *cellvars = NULL; 3946 PyObject *cellvars = NULL;
3934 PyObject *bytecode = NULL; 3947 PyObject *bytecode = NULL;
3935 int nlocals, flags; 3948 int nlocals, flags;
3936 3949
3937 tmp = dict_keys_inorder(c->u->u_consts, 0); 3950 tmp = dict_keys_inorder(c->u->u_consts, 0);
3938 if (!tmp) 3951 if (!tmp)
3939 goto error; 3952 goto error;
3940 consts = PySequence_List(tmp); /* optimize_code requires a list */ 3953 consts = PySequence_List(tmp); /* optimize_code requires a list */
3941 Py_DECREF(tmp); 3954 Py_DECREF(tmp);
3942 3955
3943 names = dict_keys_inorder(c->u->u_names, 0); 3956 names = dict_keys_inorder(c->u->u_names, 0);
3944 varnames = dict_keys_inorder(c->u->u_varnames, 0); 3957 varnames = dict_keys_inorder(c->u->u_varnames, 0);
3945 if (!consts || !names || !varnames) 3958 if (!consts || !names || !varnames)
3946 goto error; 3959 goto error;
3947 3960
3948 cellvars = dict_keys_inorder(c->u->u_cellvars, 0); 3961 cellvars = dict_keys_inorder(c->u->u_cellvars, 0);
3949 if (!cellvars) 3962 if (!cellvars)
3950 goto error; 3963 goto error;
3951 freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_Size(cellvars)); 3964 freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_Size(cellvars));
3952 if (!freevars) 3965 if (!freevars)
3953 goto error;
3954 filename = PyUnicode_DecodeFSDefault(c->c_filename);
3955 if (!filename)
3956 goto error; 3966 goto error;
3957 3967
3958 nlocals = PyDict_Size(c->u->u_varnames); 3968 nlocals = PyDict_Size(c->u->u_varnames);
3959 flags = compute_code_flags(c); 3969 flags = compute_code_flags(c);
3960 if (flags < 0) 3970 if (flags < 0)
3961 goto error; 3971 goto error;
3962 3972
3963 bytecode = PyCode_Optimize(a->a_bytecode, consts, names, a->a_lnotab); 3973 bytecode = PyCode_Optimize(a->a_bytecode, consts, names, a->a_lnotab);
3964 if (!bytecode) 3974 if (!bytecode)
3965 goto error; 3975 goto error;
3966 3976
3967 tmp = PyList_AsTuple(consts); /* PyCode_New requires a tuple */ 3977 tmp = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
3968 if (!tmp) 3978 if (!tmp)
3969 goto error; 3979 goto error;
3970 Py_DECREF(consts); 3980 Py_DECREF(consts);
3971 consts = tmp; 3981 consts = tmp;
3972 3982
3973 co = PyCode_New(c->u->u_argcount, c->u->u_kwonlyargcount, 3983 co = PyCode_New(c->u->u_argcount, c->u->u_kwonlyargcount,
3974 nlocals, stackdepth(c), flags, 3984 nlocals, stackdepth(c), flags,
3975 bytecode, consts, names, varnames, 3985 bytecode, consts, names, varnames,
3976 freevars, cellvars, 3986 freevars, cellvars,
3977 filename, c->u->u_name, 3987 c->filename, c->u->u_name,
3978 c->u->u_firstlineno, 3988 c->u->u_firstlineno,
3979 a->a_lnotab); 3989 a->a_lnotab);
3980 error: 3990 error:
3981 Py_XDECREF(consts); 3991 Py_XDECREF(consts);
3982 Py_XDECREF(names); 3992 Py_XDECREF(names);
3983 Py_XDECREF(varnames); 3993 Py_XDECREF(varnames);
3984 Py_XDECREF(filename);
3985 Py_XDECREF(name); 3994 Py_XDECREF(name);
3986 Py_XDECREF(freevars); 3995 Py_XDECREF(freevars);
3987 Py_XDECREF(cellvars); 3996 Py_XDECREF(cellvars);
3988 Py_XDECREF(bytecode); 3997 Py_XDECREF(bytecode);
3989 return co; 3998 return co;
3990 } 3999 }
3991 4000
3992 4001
3993 /* For debugging purposes only */ 4002 /* For debugging purposes only */
3994 #if 0 4003 #if 0
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
4085 4094
4086 #undef PyAST_Compile 4095 #undef PyAST_Compile
4087 PyAPI_FUNC(PyCodeObject *) 4096 PyAPI_FUNC(PyCodeObject *)
4088 PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags, 4097 PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags,
4089 PyArena *arena) 4098 PyArena *arena)
4090 { 4099 {
4091 return PyAST_CompileEx(mod, filename, flags, -1, arena); 4100 return PyAST_CompileEx(mod, filename, flags, -1, arena);
4092 } 4101 }
4093 4102
4094 4103
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+