comparison Python/symtable.c @ 85417:df2fdd42b375

Close #11619: The parser and the import machinery do not encode Unicode filenames anymore on Windows.
author Victor Stinner <victor.stinner@gmail.com>
date Mon, 26 Aug 2013 22:28:21 +0200
parents 39eb1ce5f377
children 9a7ec433bf59
comparison
equal deleted inserted replaced
85416:8fb3a6f9b0a4 85417:df2fdd42b375
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();
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;
303 } 308 }
304 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;
321 }
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
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.
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 *
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
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;
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);