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

Side by Side Diff: Python/symtable.c

Issue 11619: On Windows, don't encode filenames in the import machinery
Patch Set: Created 6 years ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Python/pythonrun.c ('k') | Python/_warnings.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 #include "Python.h" 1 #include "Python.h"
2 #include "Python-ast.h" 2 #include "Python-ast.h"
3 #include "code.h" 3 #include "code.h"
4 #include "symtable.h" 4 #include "symtable.h"
5 #include "structmember.h" 5 #include "structmember.h"
6 6
7 /* error strings used for warnings */ 7 /* error strings used for warnings */
8 #define GLOBAL_AFTER_ASSIGN \ 8 #define GLOBAL_AFTER_ASSIGN \
9 "name '%.400s' is assigned to before global declaration" 9 "name '%.400s' is assigned to before global declaration"
10 10
(...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after
226 Using the current recursion limit for the compiler seems too 226 Using the current recursion limit for the compiler seems too
227 restrictive (it caused at least one test to fail) so a factor is 227 restrictive (it caused at least one test to fail) so a factor is
228 used to allow deeper recursion when compiling an expression. 228 used to allow deeper recursion when compiling an expression.
229 229
230 Using a scaling factor means this should automatically adjust when 230 Using a scaling factor means this should automatically adjust when
231 the recursion limit is adjusted for small or large C stack allocations. 231 the recursion limit is adjusted for small or large C stack allocations.
232 */ 232 */
233 #define COMPILER_STACK_FRAME_SCALE 3 233 #define COMPILER_STACK_FRAME_SCALE 3
234 234
235 struct symtable * 235 struct symtable *
236 PySymtable_Build(mod_ty mod, const char *filename, PyFutureFeatures *future) 236 PySymtable_BuildObject(mod_ty mod, PyObject *filename, PyFutureFeatures *future)
237 { 237 {
238 struct symtable *st = symtable_new(); 238 struct symtable *st = symtable_new();
239 asdl_seq *seq; 239 asdl_seq *seq;
240 int i; 240 int i;
241 PyThreadState *tstate; 241 PyThreadState *tstate;
242 242
243 if (st == NULL) 243 if (st == NULL)
244 return st; 244 return NULL;
245 if (filename == NULL) {
246 PySymtable_Free(st);
247 return NULL;
248 }
249 Py_INCREF(filename);
245 st->st_filename = filename; 250 st->st_filename = filename;
246 st->st_future = future; 251 st->st_future = future;
247 252
248 /* Setup recursion depth check counters */ 253 /* Setup recursion depth check counters */
249 tstate = PyThreadState_GET(); 254 tstate = PyThreadState_GET();
250 if (!tstate) { 255 if (!tstate) {
251 PySymtable_Free(st); 256 PySymtable_Free(st);
252 return NULL; 257 return NULL;
253 } 258 }
254 st->recursion_depth = tstate->recursion_depth * COMPILER_STACK_FRAME_SCALE; 259 st->recursion_depth = tstate->recursion_depth * COMPILER_STACK_FRAME_SCALE;
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
293 } 298 }
294 /* Make the second symbol analysis pass */ 299 /* Make the second symbol analysis pass */
295 if (symtable_analyze(st)) 300 if (symtable_analyze(st))
296 return st; 301 return st;
297 PySymtable_Free(st); 302 PySymtable_Free(st);
298 return NULL; 303 return NULL;
299 error: 304 error:
300 (void) symtable_exit_block(st, (void *)mod); 305 (void) symtable_exit_block(st, (void *)mod);
301 PySymtable_Free(st); 306 PySymtable_Free(st);
302 return NULL; 307 return NULL;
308 }
309
310 struct symtable *
311 PySymtable_Build(mod_ty mod, const char *filename_str, PyFutureFeatures *future)
312 {
313 PyObject *filename;
314 struct symtable *st;
315 filename = PyUnicode_DecodeFSDefault(filename_str);
316 if (filename == NULL)
317 return NULL;
318 st = PySymtable_BuildObject(mod, filename, future);
319 Py_DECREF(filename);
320 return st;
303 } 321 }
304 322
305 void 323 void
306 PySymtable_Free(struct symtable *st) 324 PySymtable_Free(struct symtable *st)
307 { 325 {
326 Py_XDECREF(st->st_filename);
308 Py_XDECREF(st->st_blocks); 327 Py_XDECREF(st->st_blocks);
309 Py_XDECREF(st->st_stack); 328 Py_XDECREF(st->st_stack);
310 PyMem_Free((void *)st); 329 PyMem_Free((void *)st);
311 } 330 }
312 331
313 PySTEntryObject * 332 PySTEntryObject *
314 PySymtable_Lookup(struct symtable *st, void *key) 333 PySymtable_Lookup(struct symtable *st, void *key)
315 { 334 {
316 PyObject *k, *v; 335 PyObject *k, *v;
317 336
(...skipping 29 matching lines...) Expand all
347 { 366 {
348 Py_ssize_t i; 367 Py_ssize_t i;
349 PyObject *data; 368 PyObject *data;
350 assert(ste->ste_directives); 369 assert(ste->ste_directives);
351 for (i = 0; ; i++) { 370 for (i = 0; ; i++) {
352 data = PyList_GET_ITEM(ste->ste_directives, i); 371 data = PyList_GET_ITEM(ste->ste_directives, i);
353 assert(PyTuple_CheckExact(data)); 372 assert(PyTuple_CheckExact(data));
354 if (PyTuple_GET_ITEM(data, 0) == name) 373 if (PyTuple_GET_ITEM(data, 0) == name)
355 break; 374 break;
356 } 375 }
357 PyErr_SyntaxLocationEx(ste->ste_table->st_filename, 376 PyErr_SyntaxLocationObject(ste->ste_table->st_filename,
358 PyLong_AsLong(PyTuple_GET_ITEM(data, 1)), 377 PyLong_AsLong(PyTuple_GET_ITEM(data, 1)),
359 PyLong_AsLong(PyTuple_GET_ITEM(data, 2))); 378 PyLong_AsLong(PyTuple_GET_ITEM(data, 2)));
360 return 0; 379 return 0;
361 } 380 }
362 381
363 382
364 /* Analyze raw symbol information to determine scope of each name. 383 /* Analyze raw symbol information to determine scope of each name.
365 384
366 The next several functions are helpers for symtable_analyze(), 385 The next several functions are helpers for symtable_analyze(),
367 which determines whether a name is local, global, or free. In addition, 386 which determines whether a name is local, global, or free. In addition,
368 it determines which local variables are cell variables; they provide 387 it determines which local variables are cell variables; they provide
369 bindings that are used for free variables in enclosed blocks. 388 bindings that are used for free variables in enclosed blocks.
(...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after
576 switch (ste->ste_unoptimized) { 595 switch (ste->ste_unoptimized) {
577 case OPT_TOPLEVEL: /* import * at top-level is fine */ 596 case OPT_TOPLEVEL: /* import * at top-level is fine */
578 return 1; 597 return 1;
579 case OPT_IMPORT_STAR: 598 case OPT_IMPORT_STAR:
580 PyErr_Format(PyExc_SyntaxError, 599 PyErr_Format(PyExc_SyntaxError,
581 "import * is not allowed in function '%U' because it %s", 600 "import * is not allowed in function '%U' because it %s",
582 ste->ste_name, trailer); 601 ste->ste_name, trailer);
583 break; 602 break;
584 } 603 }
585 604
586 PyErr_SyntaxLocationEx(ste->ste_table->st_filename, ste->ste_opt_lineno, 605 PyErr_SyntaxLocationObject(ste->ste_table->st_filename,
587 ste->ste_opt_col_offset); 606 ste->ste_opt_lineno,
607 ste->ste_opt_col_offset);
588 return 0; 608 return 0;
589 } 609 }
590 610
591 /* Enter the final scope information into the ste_symbols dict. 611 /* Enter the final scope information into the ste_symbols dict.
592 * 612 *
593 * All arguments are dicts. Modifies symbols, others are read-only. 613 * All arguments are dicts. Modifies symbols, others are read-only.
594 */ 614 */
595 static int 615 static int
596 update_symbols(PyObject *symbols, PyObject *scopes, 616 update_symbols(PyObject *symbols, PyObject *scopes,
597 PyObject *bound, PyObject *free, int classflag) 617 PyObject *bound, PyObject *free, int classflag)
(...skipping 310 matching lines...) Expand 10 before | Expand all | Expand 10 after
908 r = analyze_block(st->st_top, NULL, free, global); 928 r = analyze_block(st->st_top, NULL, free, global);
909 Py_DECREF(free); 929 Py_DECREF(free);
910 Py_DECREF(global); 930 Py_DECREF(global);
911 return r; 931 return r;
912 } 932 }
913 933
914 934
915 static int 935 static int
916 symtable_warn(struct symtable *st, char *msg, int lineno) 936 symtable_warn(struct symtable *st, char *msg, int lineno)
917 { 937 {
918 if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, st->st_filename, 938 PyObject *message = PyUnicode_FromString(msg);
919 lineno, NULL, NULL) < 0) { 939 if (message == NULL)
940 return 0;
941 if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, message, st->st_filename,
942 lineno, NULL, NULL) < 0) {
943 Py_DECREF(message);
920 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) { 944 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
921 PyErr_SetString(PyExc_SyntaxError, msg); 945 PyErr_SetString(PyExc_SyntaxError, msg);
922 PyErr_SyntaxLocationEx(st->st_filename, st->st_cur->ste_lineno, 946 PyErr_SyntaxLocationObject(st->st_filename, st->st_cur->ste_lineno,
923 st->st_cur->ste_col_offset); 947 st->st_cur->ste_col_offset);
924 } 948 }
925 return 0; 949 return 0;
926 } 950 }
951 Py_DECREF(message);
927 return 1; 952 return 1;
928 } 953 }
929 954
930 /* symtable_enter_block() gets a reference via ste_new. 955 /* symtable_enter_block() gets a reference via ste_new.
931 This reference is released when the block is exited, via the DECREF 956 This reference is released when the block is exited, via the DECREF
932 in symtable_exit_block(). 957 in symtable_exit_block().
933 */ 958 */
934 959
935 static int 960 static int
936 symtable_exit_block(struct symtable *st, void *ast) 961 symtable_exit_block(struct symtable *st, void *ast)
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
999 1024
1000 1025
1001 if (!mangled) 1026 if (!mangled)
1002 return 0; 1027 return 0;
1003 dict = st->st_cur->ste_symbols; 1028 dict = st->st_cur->ste_symbols;
1004 if ((o = PyDict_GetItem(dict, mangled))) { 1029 if ((o = PyDict_GetItem(dict, mangled))) {
1005 val = PyLong_AS_LONG(o); 1030 val = PyLong_AS_LONG(o);
1006 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) { 1031 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
1007 /* Is it better to use 'mangled' or 'name' here? */ 1032 /* Is it better to use 'mangled' or 'name' here? */
1008 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT, name); 1033 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT, name);
1009 PyErr_SyntaxLocationEx(st->st_filename, 1034 PyErr_SyntaxLocationObject(st->st_filename,
1010 st->st_cur->ste_lineno, 1035 st->st_cur->ste_lineno,
1011 st->st_cur->ste_col_offset); 1036 st->st_cur->ste_col_offset);
1012 goto error; 1037 goto error;
1013 } 1038 }
1014 val |= flag; 1039 val |= flag;
1015 } else 1040 } else
1016 val = flag; 1041 val = flag;
1017 o = PyLong_FromLong(val); 1042 o = PyLong_FromLong(val);
1018 if (o == NULL) 1043 if (o == NULL)
1019 goto error; 1044 goto error;
1020 if (PyDict_SetItem(dict, mangled, o) < 0) { 1045 if (PyDict_SetItem(dict, mangled, o) < 0) {
1021 Py_DECREF(o); 1046 Py_DECREF(o);
(...skipping 584 matching lines...) Expand 10 before | Expand all | Expand 10 after
1606 if (PyUnicode_CompareWithASCIIString(name, "*")) { 1631 if (PyUnicode_CompareWithASCIIString(name, "*")) {
1607 int r = symtable_add_def(st, store_name, DEF_IMPORT); 1632 int r = symtable_add_def(st, store_name, DEF_IMPORT);
1608 Py_DECREF(store_name); 1633 Py_DECREF(store_name);
1609 return r; 1634 return r;
1610 } 1635 }
1611 else { 1636 else {
1612 if (st->st_cur->ste_type != ModuleBlock) { 1637 if (st->st_cur->ste_type != ModuleBlock) {
1613 int lineno = st->st_cur->ste_lineno; 1638 int lineno = st->st_cur->ste_lineno;
1614 int col_offset = st->st_cur->ste_col_offset; 1639 int col_offset = st->st_cur->ste_col_offset;
1615 PyErr_SetString(PyExc_SyntaxError, IMPORT_STAR_WARNING); 1640 PyErr_SetString(PyExc_SyntaxError, IMPORT_STAR_WARNING);
1616 PyErr_SyntaxLocationEx(st->st_filename, lineno, col_offset); 1641 PyErr_SyntaxLocationObject(st->st_filename, lineno, col_offset);
1617 Py_DECREF(store_name); 1642 Py_DECREF(store_name);
1618 return 0; 1643 return 0;
1619 } 1644 }
1620 st->st_cur->ste_unoptimized |= OPT_IMPORT_STAR; 1645 st->st_cur->ste_unoptimized |= OPT_IMPORT_STAR;
1621 Py_DECREF(store_name); 1646 Py_DECREF(store_name);
1622 return 1; 1647 return 1;
1623 } 1648 }
1624 } 1649 }
1625 1650
1626 1651
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
1726 } 1751 }
1727 1752
1728 static int 1753 static int
1729 symtable_visit_dictcomp(struct symtable *st, expr_ty e) 1754 symtable_visit_dictcomp(struct symtable *st, expr_ty e)
1730 { 1755 {
1731 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(dictcomp), 1756 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(dictcomp),
1732 e->v.DictComp.generators, 1757 e->v.DictComp.generators,
1733 e->v.DictComp.key, 1758 e->v.DictComp.key,
1734 e->v.DictComp.value); 1759 e->v.DictComp.value);
1735 } 1760 }
OLDNEW
« no previous file with comments | « Python/pythonrun.c ('k') | Python/_warnings.c » ('j') | no next file with comments »

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