| 1 |
#include "Python.h" |
| 2 |
#include "Python-ast.h" |
| 3 |
#include "code.h" |
| 4 |
#include "symtable.h" |
| 5 |
#include "structmember.h" |
| 6 |
|
| 7 |
|
| 8 |
#define GLOBAL_AFTER_ASSIGN \ |
| 9 |
"name '%.400s' is assigned to before global declaration" |
| 10 |
|
| 11 |
#define GLOBAL_AFTER_USE \ |
| 12 |
"name '%.400s' is used prior to global declaration" |
| 13 |
|
| 14 |
#define IMPORT_STAR_WARNING "import * only allowed at module level" |
| 15 |
|
| 16 |
#define RETURN_VAL_IN_GENERATOR \ |
| 17 |
"'return' with argument inside generator" |
| 18 |
|
| 19 |
|
| 20 |
static PySTEntryObject * |
| 21 |
ste_new(struct symtable *st, identifier name, _Py_block_ty block, |
| 22 |
void *key, int lineno) |
| 23 |
{ |
| 24 |
PySTEntryObject *ste = NULL; |
| 25 |
PyObject *k = NULL; |
| 26 |
|
| 27 |
k = PyLong_FromVoidPtr(key); |
| 28 |
if (k == NULL) |
| 29 |
goto fail; |
| 30 |
ste = PyObject_New(PySTEntryObject, &PySTEntry_Type); |
| 31 |
if (ste == NULL) { |
| 32 |
Py_DECREF(k); |
| 33 |
goto fail; |
| 34 |
} |
| 35 |
ste->ste_table = st; |
| 36 |
ste->ste_id = k; |
| 37 |
|
| 38 |
ste->ste_name = name; |
| 39 |
Py_INCREF(name); |
| 40 |
|
| 41 |
ste->ste_symbols = NULL; |
| 42 |
ste->ste_varnames = NULL; |
| 43 |
ste->ste_children = NULL; |
| 44 |
|
| 45 |
ste->ste_symbols = PyDict_New(); |
| 46 |
if (ste->ste_symbols == NULL) |
| 47 |
goto fail; |
| 48 |
|
| 49 |
ste->ste_varnames = PyList_New(0); |
| 50 |
if (ste->ste_varnames == NULL) |
| 51 |
goto fail; |
| 52 |
|
| 53 |
ste->ste_children = PyList_New(0); |
| 54 |
if (ste->ste_children == NULL) |
| 55 |
goto fail; |
| 56 |
|
| 57 |
ste->ste_type = block; |
| 58 |
ste->ste_unoptimized = 0; |
| 59 |
ste->ste_nested = 0; |
| 60 |
ste->ste_free = 0; |
| 61 |
ste->ste_varargs = 0; |
| 62 |
ste->ste_varkeywords = 0; |
| 63 |
ste->ste_opt_lineno = 0; |
| 64 |
ste->ste_tmpname = 0; |
| 65 |
ste->ste_lineno = lineno; |
| 66 |
|
| 67 |
if (st->st_cur != NULL && |
| 68 |
(st->st_cur->ste_nested || |
| 69 |
st->st_cur->ste_type == FunctionBlock)) |
| 70 |
ste->ste_nested = 1; |
| 71 |
ste->ste_child_free = 0; |
| 72 |
ste->ste_generator = 0; |
| 73 |
ste->ste_returns_value = 0; |
| 74 |
|
| 75 |
if (PyDict_SetItem(st->st_symbols, ste->ste_id, (PyObject *)ste) < 0) |
| 76 |
goto fail; |
| 77 |
|
| 78 |
return ste; |
| 79 |
fail: |
| 80 |
Py_XDECREF(ste); |
| 81 |
return NULL; |
| 82 |
} |
| 83 |
|
| 84 |
static PyObject * |
| 85 |
ste_repr(PySTEntryObject *ste) |
| 86 |
{ |
| 87 |
char buf[256]; |
| 88 |
|
| 89 |
PyOS_snprintf(buf, sizeof(buf), |
| 90 |
"<symtable entry %.100s(%ld), line %d>", |
| 91 |
PyString_AS_STRING(ste->ste_name), |
| 92 |
PyInt_AS_LONG(ste->ste_id), ste->ste_lineno); |
| 93 |
return PyString_FromString(buf); |
| 94 |
} |
| 95 |
|
| 96 |
static void |
| 97 |
ste_dealloc(PySTEntryObject *ste) |
| 98 |
{ |
| 99 |
ste->ste_table = NULL; |
| 100 |
Py_XDECREF(ste->ste_id); |
| 101 |
Py_XDECREF(ste->ste_name); |
| 102 |
Py_XDECREF(ste->ste_symbols); |
| 103 |
Py_XDECREF(ste->ste_varnames); |
| 104 |
Py_XDECREF(ste->ste_children); |
| 105 |
PyObject_Del(ste); |
| 106 |
} |
| 107 |
|
| 108 |
#define OFF(x) offsetof(PySTEntryObject, x) |
| 109 |
|
| 110 |
static PyMemberDef ste_memberlist[] = { |
| 111 |
{"id", T_OBJECT, OFF(ste_id), READONLY}, |
| 112 |
{"name", T_OBJECT, OFF(ste_name), READONLY}, |
| 113 |
{"symbols", T_OBJECT, OFF(ste_symbols), READONLY}, |
| 114 |
{"varnames", T_OBJECT, OFF(ste_varnames), READONLY}, |
| 115 |
{"children", T_OBJECT, OFF(ste_children), READONLY}, |
| 116 |
{"optimized",T_INT, OFF(ste_unoptimized), READONLY}, |
| 117 |
{"nested", T_INT, OFF(ste_nested), READONLY}, |
| 118 |
{"type", T_INT, OFF(ste_type), READONLY}, |
| 119 |
{"lineno", T_INT, OFF(ste_lineno), READONLY}, |
| 120 |
{NULL} |
| 121 |
}; |
| 122 |
|
| 123 |
PyTypeObject PySTEntry_Type = { |
| 124 |
PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 125 |
"symtable entry", |
| 126 |
sizeof(PySTEntryObject), |
| 127 |
0, |
| 128 |
(destructor)ste_dealloc, |
| 129 |
0, |
| 130 |
0, |
| 131 |
0, |
| 132 |
0, |
| 133 |
(reprfunc)ste_repr, |
| 134 |
0, |
| 135 |
0, |
| 136 |
0, |
| 137 |
0, |
| 138 |
0, |
| 139 |
0, |
| 140 |
PyObject_GenericGetAttr, |
| 141 |
0, |
| 142 |
0, |
| 143 |
Py_TPFLAGS_DEFAULT, |
| 144 |
0, |
| 145 |
0, |
| 146 |
0, |
| 147 |
0, |
| 148 |
0, |
| 149 |
0, |
| 150 |
0, |
| 151 |
0, |
| 152 |
ste_memberlist, |
| 153 |
0, |
| 154 |
0, |
| 155 |
0, |
| 156 |
0, |
| 157 |
0, |
| 158 |
0, |
| 159 |
0, |
| 160 |
0, |
| 161 |
0, |
| 162 |
}; |
| 163 |
|
| 164 |
static int symtable_analyze(struct symtable *st); |
| 165 |
static int symtable_warn(struct symtable *st, |
| 166 |
PyObject *warn, const char *msg, int lineno); |
| 167 |
static int symtable_enter_block(struct symtable *st, identifier name, |
| 168 |
_Py_block_ty block, void *ast, int lineno); |
| 169 |
static int symtable_exit_block(struct symtable *st, void *ast); |
| 170 |
static int symtable_visit_stmt(struct symtable *st, stmt_ty s); |
| 171 |
static int symtable_visit_expr(struct symtable *st, expr_ty s); |
| 172 |
static int symtable_visit_listcomp(struct symtable *st, expr_ty e); |
| 173 |
static int symtable_visit_genexp(struct symtable *st, expr_ty s); |
| 174 |
static int symtable_visit_setcomp(struct symtable *st, expr_ty e); |
| 175 |
static int symtable_visit_dictcomp(struct symtable *st, expr_ty e); |
| 176 |
static int symtable_visit_arguments(struct symtable *st, arguments_ty); |
| 177 |
static int symtable_visit_excepthandler(struct symtable *st, excepthandler_ty); |
| 178 |
static int symtable_visit_alias(struct symtable *st, alias_ty); |
| 179 |
static int symtable_visit_comprehension(struct symtable *st, comprehension_ty); |
| 180 |
static int symtable_visit_keyword(struct symtable *st, keyword_ty); |
| 181 |
static int symtable_visit_slice(struct symtable *st, slice_ty); |
| 182 |
static int symtable_visit_params(struct symtable *st, asdl_seq *args, int top); |
| 183 |
static int symtable_visit_params_nested(struct symtable *st, asdl_seq *args); |
| 184 |
static int symtable_implicit_arg(struct symtable *st, int pos); |
| 185 |
|
| 186 |
|
| 187 |
static identifier top = NULL, lambda = NULL, genexpr = NULL, setcomp = NULL, |
| 188 |
dictcomp = NULL; |
| 189 |
|
| 190 |
#define GET_IDENTIFIER(VAR) \ |
| 191 |
((VAR) ? (VAR) : ((VAR) = PyString_InternFromString(# VAR))) |
| 192 |
|
| 193 |
#define DUPLICATE_ARGUMENT \ |
| 194 |
"duplicate argument '%s' in function definition" |
| 195 |
|
| 196 |
static struct symtable * |
| 197 |
symtable_new(void) |
| 198 |
{ |
| 199 |
struct symtable *st; |
| 200 |
|
| 201 |
st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable)); |
| 202 |
if (st == NULL) { |
| 203 |
PyErr_NoMemory(); |
| 204 |
return NULL; |
| 205 |
} |
| 206 |
|
| 207 |
st->st_filename = NULL; |
| 208 |
st->st_symbols = NULL; |
| 209 |
|
| 210 |
if ((st->st_stack = PyList_New(0)) == NULL) |
| 211 |
goto fail; |
| 212 |
if ((st->st_symbols = PyDict_New()) == NULL) |
| 213 |
goto fail; |
| 214 |
st->st_cur = NULL; |
| 215 |
st->st_private = NULL; |
| 216 |
return st; |
| 217 |
fail: |
| 218 |
PySymtable_Free(st); |
| 219 |
return NULL; |
| 220 |
} |
| 221 |
|
| 222 |
struct symtable * |
| 223 |
PySymtable_Build(mod_ty mod, const char *filename, PyFutureFeatures *future) |
| 224 |
{ |
| 225 |
struct symtable *st = symtable_new(); |
| 226 |
asdl_seq *seq; |
| 227 |
int i; |
| 228 |
|
| 229 |
if (st == NULL) |
| 230 |
return st; |
| 231 |
st->st_filename = filename; |
| 232 |
st->st_future = future; |
| 233 |
if (!GET_IDENTIFIER(top) || |
| 234 |
!symtable_enter_block(st, top, ModuleBlock, (void *)mod, 0)) { |
| 235 |
PySymtable_Free(st); |
| 236 |
return NULL; |
| 237 |
} |
| 238 |
|
| 239 |
st->st_top = st->st_cur; |
| 240 |
st->st_cur->ste_unoptimized = OPT_TOPLEVEL; |
| 241 |
|
| 242 |
switch (mod->kind) { |
| 243 |
case Module_kind: |
| 244 |
seq = mod->v.Module.body; |
| 245 |
for (i = 0; i < asdl_seq_LEN(seq); i++) |
| 246 |
if (!symtable_visit_stmt(st, |
| 247 |
(stmt_ty)asdl_seq_GET(seq, i))) |
| 248 |
goto error; |
| 249 |
break; |
| 250 |
case Expression_kind: |
| 251 |
if (!symtable_visit_expr(st, mod->v.Expression.body)) |
| 252 |
goto error; |
| 253 |
break; |
| 254 |
case Interactive_kind: |
| 255 |
seq = mod->v.Interactive.body; |
| 256 |
for (i = 0; i < asdl_seq_LEN(seq); i++) |
| 257 |
if (!symtable_visit_stmt(st, |
| 258 |
(stmt_ty)asdl_seq_GET(seq, i))) |
| 259 |
goto error; |
| 260 |
break; |
| 261 |
case Suite_kind: |
| 262 |
PyErr_SetString(PyExc_RuntimeError, |
| 263 |
"this compiler does not handle Suites"); |
| 264 |
goto error; |
| 265 |
} |
| 266 |
if (!symtable_exit_block(st, (void *)mod)) { |
| 267 |
PySymtable_Free(st); |
| 268 |
return NULL; |
| 269 |
} |
| 270 |
if (symtable_analyze(st)) |
| 271 |
return st; |
| 272 |
PySymtable_Free(st); |
| 273 |
return NULL; |
| 274 |
error: |
| 275 |
(void) symtable_exit_block(st, (void *)mod); |
| 276 |
PySymtable_Free(st); |
| 277 |
return NULL; |
| 278 |
} |
| 279 |
|
| 280 |
void |
| 281 |
PySymtable_Free(struct symtable *st) |
| 282 |
{ |
| 283 |
Py_XDECREF(st->st_symbols); |
| 284 |
Py_XDECREF(st->st_stack); |
| 285 |
PyMem_Free((void *)st); |
| 286 |
} |
| 287 |
|
| 288 |
PySTEntryObject * |
| 289 |
PySymtable_Lookup(struct symtable *st, void *key) |
| 290 |
{ |
| 291 |
PyObject *k, *v; |
| 292 |
|
| 293 |
k = PyLong_FromVoidPtr(key); |
| 294 |
if (k == NULL) |
| 295 |
return NULL; |
| 296 |
v = PyDict_GetItem(st->st_symbols, k); |
| 297 |
if (v) { |
| 298 |
assert(PySTEntry_Check(v)); |
| 299 |
Py_INCREF(v); |
| 300 |
} |
| 301 |
else { |
| 302 |
PyErr_SetString(PyExc_KeyError, |
| 303 |
"unknown symbol table entry"); |
| 304 |
} |
| 305 |
|
| 306 |
Py_DECREF(k); |
| 307 |
return (PySTEntryObject *)v; |
| 308 |
} |
| 309 |
|
| 310 |
int |
| 311 |
PyST_GetScope(PySTEntryObject *ste, PyObject *name) |
| 312 |
{ |
| 313 |
PyObject *v = PyDict_GetItem(ste->ste_symbols, name); |
| 314 |
if (!v) |
| 315 |
return 0; |
| 316 |
assert(PyInt_Check(v)); |
| 317 |
return (PyInt_AS_LONG(v) >> SCOPE_OFF) & SCOPE_MASK; |
| 318 |
} |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
#define SET_SCOPE(DICT, NAME, I) { \ |
| 359 |
PyObject *o = PyInt_FromLong(I); \ |
| 360 |
if (!o) \ |
| 361 |
return 0; \ |
| 362 |
if (PyDict_SetItem((DICT), (NAME), o) < 0) { \ |
| 363 |
Py_DECREF(o); \ |
| 364 |
return 0; \ |
| 365 |
} \ |
| 366 |
Py_DECREF(o); \ |
| 367 |
} |
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
static int |
| 377 |
analyze_name(PySTEntryObject *ste, PyObject *dict, PyObject *name, long flags, |
| 378 |
PyObject *bound, PyObject *local, PyObject *free, |
| 379 |
PyObject *global) |
| 380 |
{ |
| 381 |
if (flags & DEF_GLOBAL) { |
| 382 |
if (flags & DEF_PARAM) { |
| 383 |
PyErr_Format(PyExc_SyntaxError, |
| 384 |
"name '%s' is local and global", |
| 385 |
PyString_AS_STRING(name)); |
| 386 |
PyErr_SyntaxLocation(ste->ste_table->st_filename, |
| 387 |
ste->ste_lineno); |
| 388 |
|
| 389 |
return 0; |
| 390 |
} |
| 391 |
SET_SCOPE(dict, name, GLOBAL_EXPLICIT); |
| 392 |
if (PyDict_SetItem(global, name, Py_None) < 0) |
| 393 |
return 0; |
| 394 |
if (bound && PyDict_GetItem(bound, name)) { |
| 395 |
if (PyDict_DelItem(bound, name) < 0) |
| 396 |
return 0; |
| 397 |
} |
| 398 |
return 1; |
| 399 |
} |
| 400 |
if (flags & DEF_BOUND) { |
| 401 |
SET_SCOPE(dict, name, LOCAL); |
| 402 |
if (PyDict_SetItem(local, name, Py_None) < 0) |
| 403 |
return 0; |
| 404 |
if (PyDict_GetItem(global, name)) { |
| 405 |
if (PyDict_DelItem(global, name) < 0) |
| 406 |
return 0; |
| 407 |
} |
| 408 |
return 1; |
| 409 |
} |
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
if (bound && PyDict_GetItem(bound, name)) { |
| 416 |
SET_SCOPE(dict, name, FREE); |
| 417 |
ste->ste_free = 1; |
| 418 |
if (PyDict_SetItem(free, name, Py_None) < 0) |
| 419 |
return 0; |
| 420 |
return 1; |
| 421 |
} |
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
else if (global && PyDict_GetItem(global, name)) { |
| 426 |
SET_SCOPE(dict, name, GLOBAL_IMPLICIT); |
| 427 |
return 1; |
| 428 |
} |
| 429 |
else { |
| 430 |
if (ste->ste_nested) |
| 431 |
ste->ste_free = 1; |
| 432 |
SET_SCOPE(dict, name, GLOBAL_IMPLICIT); |
| 433 |
return 1; |
| 434 |
} |
| 435 |
|
| 436 |
PyErr_Format(PyExc_SystemError, "failed to set scope for %s", |
| 437 |
PyString_AS_STRING(name)); |
| 438 |
return 0; |
| 439 |
} |
| 440 |
|
| 441 |
#undef SET_SCOPE |
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
static int |
| 452 |
analyze_cells(PyObject *scope, PyObject *free) |
| 453 |
{ |
| 454 |
PyObject *name, *v, *w; |
| 455 |
int success = 0; |
| 456 |
Py_ssize_t pos = 0; |
| 457 |
|
| 458 |
w = PyInt_FromLong(CELL); |
| 459 |
if (!w) |
| 460 |
return 0; |
| 461 |
while (PyDict_Next(scope, &pos, &name, &v)) { |
| 462 |
long flags; |
| 463 |
assert(PyInt_Check(v)); |
| 464 |
flags = PyInt_AS_LONG(v); |
| 465 |
if (flags != LOCAL) |
| 466 |
continue; |
| 467 |
if (!PyDict_GetItem(free, name)) |
| 468 |
continue; |
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
if (PyDict_SetItem(scope, name, w) < 0) |
| 474 |
goto error; |
| 475 |
if (PyDict_DelItem(free, name) < 0) |
| 476 |
goto error; |
| 477 |
} |
| 478 |
success = 1; |
| 479 |
error: |
| 480 |
Py_DECREF(w); |
| 481 |
return success; |
| 482 |
} |
| 483 |
|
| 484 |
|
| 485 |
static int |
| 486 |
check_unoptimized(const PySTEntryObject* ste) { |
| 487 |
char buf[300]; |
| 488 |
const char* trailer; |
| 489 |
|
| 490 |
if (ste->ste_type != FunctionBlock || !ste->ste_unoptimized |
| 491 |
|| !(ste->ste_free || ste->ste_child_free)) |
| 492 |
return 1; |
| 493 |
|
| 494 |
trailer = (ste->ste_child_free ? |
| 495 |
"contains a nested function with free variables" : |
| 496 |
"is a nested function"); |
| 497 |
|
| 498 |
switch (ste->ste_unoptimized) { |
| 499 |
case OPT_TOPLEVEL: |
| 500 |
case OPT_EXEC: |
| 501 |
return 1; |
| 502 |
case OPT_IMPORT_STAR: |
| 503 |
PyOS_snprintf(buf, sizeof(buf), |
| 504 |
"import * is not allowed in function '%.100s' " |
| 505 |
"because it %s", |
| 506 |
PyString_AS_STRING(ste->ste_name), trailer); |
| 507 |
break; |
| 508 |
case OPT_BARE_EXEC: |
| 509 |
PyOS_snprintf(buf, sizeof(buf), |
| 510 |
"unqualified exec is not allowed in function " |
| 511 |
"'%.100s' because it %s", |
| 512 |
PyString_AS_STRING(ste->ste_name), trailer); |
| 513 |
break; |
| 514 |
default: |
| 515 |
PyOS_snprintf(buf, sizeof(buf), |
| 516 |
"function '%.100s' uses import * and bare exec, " |
| 517 |
"which are illegal because it %s", |
| 518 |
PyString_AS_STRING(ste->ste_name), trailer); |
| 519 |
break; |
| 520 |
} |
| 521 |
|
| 522 |
PyErr_SetString(PyExc_SyntaxError, buf); |
| 523 |
PyErr_SyntaxLocation(ste->ste_table->st_filename, |
| 524 |
ste->ste_opt_lineno); |
| 525 |
return 0; |
| 526 |
} |
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
static int |
| 533 |
update_symbols(PyObject *symbols, PyObject *scope, |
| 534 |
PyObject *bound, PyObject *free, int classflag) |
| 535 |
{ |
| 536 |
PyObject *name, *v, *u, *w, *free_value = NULL; |
| 537 |
Py_ssize_t pos = 0; |
| 538 |
|
| 539 |
while (PyDict_Next(symbols, &pos, &name, &v)) { |
| 540 |
long i, flags; |
| 541 |
assert(PyInt_Check(v)); |
| 542 |
flags = PyInt_AS_LONG(v); |
| 543 |
w = PyDict_GetItem(scope, name); |
| 544 |
assert(w && PyInt_Check(w)); |
| 545 |
i = PyInt_AS_LONG(w); |
| 546 |
flags |= (i << SCOPE_OFF); |
| 547 |
u = PyInt_FromLong(flags); |
| 548 |
if (!u) |
| 549 |
return 0; |
| 550 |
if (PyDict_SetItem(symbols, name, u) < 0) { |
| 551 |
Py_DECREF(u); |
| 552 |
return 0; |
| 553 |
} |
| 554 |
Py_DECREF(u); |
| 555 |
} |
| 556 |
|
| 557 |
free_value = PyInt_FromLong(FREE << SCOPE_OFF); |
| 558 |
if (!free_value) |
| 559 |
return 0; |
| 560 |
|
| 561 |
|
| 562 |
pos = 0; |
| 563 |
while (PyDict_Next(free, &pos, &name, &v)) { |
| 564 |
PyObject *o = PyDict_GetItem(symbols, name); |
| 565 |
|
| 566 |
if (o) { |
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
if (classflag && |
| 572 |
PyInt_AS_LONG(o) & (DEF_BOUND | DEF_GLOBAL)) { |
| 573 |
long i = PyInt_AS_LONG(o) | DEF_FREE_CLASS; |
| 574 |
o = PyInt_FromLong(i); |
| 575 |
if (!o) { |
| 576 |
Py_DECREF(free_value); |
| 577 |
return 0; |
| 578 |
} |
| 579 |
if (PyDict_SetItem(symbols, name, o) < 0) { |
| 580 |
Py_DECREF(o); |
| 581 |
Py_DECREF(free_value); |
| 582 |
return 0; |
| 583 |
} |
| 584 |
Py_DECREF(o); |
| 585 |
} |
| 586 |
|
| 587 |
continue; |
| 588 |
} |
| 589 |
if (!PyDict_GetItem(bound, name)) |
| 590 |
continue; |
| 591 |
|
| 592 |
if (PyDict_SetItem(symbols, name, free_value) < 0) { |
| 593 |
Py_DECREF(free_value); |
| 594 |
return 0; |
| 595 |
} |
| 596 |
} |
| 597 |
Py_DECREF(free_value); |
| 598 |
return 1; |
| 599 |
} |
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
static int |
| 622 |
analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free, |
| 623 |
PyObject *global, PyObject* child_free); |
| 624 |
|
| 625 |
static int |
| 626 |
analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free, |
| 627 |
PyObject *global) |
| 628 |
{ |
| 629 |
PyObject *name, *v, *local = NULL, *scope = NULL; |
| 630 |
PyObject *newbound = NULL, *newglobal = NULL; |
| 631 |
PyObject *newfree = NULL, *allfree = NULL; |
| 632 |
int i, success = 0; |
| 633 |
Py_ssize_t pos = 0; |
| 634 |
|
| 635 |
local = PyDict_New(); |
| 636 |
if (!local) |
| 637 |
goto error; |
| 638 |
scope = PyDict_New(); |
| 639 |
if (!scope) |
| 640 |
goto error; |
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
newglobal = PyDict_New(); |
| 654 |
if (!newglobal) |
| 655 |
goto error; |
| 656 |
newbound = PyDict_New(); |
| 657 |
if (!newbound) |
| 658 |
goto error; |
| 659 |
newfree = PyDict_New(); |
| 660 |
if (!newfree) |
| 661 |
goto error; |
| 662 |
|
| 663 |
if (ste->ste_type == ClassBlock) { |
| 664 |
if (PyDict_Update(newglobal, global) < 0) |
| 665 |
goto error; |
| 666 |
if (bound) |
| 667 |
if (PyDict_Update(newbound, bound) < 0) |
| 668 |
goto error; |
| 669 |
} |
| 670 |
|
| 671 |
while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) { |
| 672 |
long flags = PyInt_AS_LONG(v); |
| 673 |
if (!analyze_name(ste, scope, name, flags, |
| 674 |
bound, local, free, global)) |
| 675 |
goto error; |
| 676 |
} |
| 677 |
|
| 678 |
if (ste->ste_type != ClassBlock) { |
| 679 |
if (ste->ste_type == FunctionBlock) { |
| 680 |
if (PyDict_Update(newbound, local) < 0) |
| 681 |
goto error; |
| 682 |
} |
| 683 |
if (bound) { |
| 684 |
if (PyDict_Update(newbound, bound) < 0) |
| 685 |
goto error; |
| 686 |
} |
| 687 |
if (PyDict_Update(newglobal, global) < 0) |
| 688 |
goto error; |
| 689 |
} |
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
allfree = PyDict_New(); |
| 698 |
if (!allfree) |
| 699 |
goto error; |
| 700 |
for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) { |
| 701 |
PyObject *c = PyList_GET_ITEM(ste->ste_children, i); |
| 702 |
PySTEntryObject* entry; |
| 703 |
assert(c && PySTEntry_Check(c)); |
| 704 |
entry = (PySTEntryObject*)c; |
| 705 |
if (!analyze_child_block(entry, newbound, newfree, newglobal, |
| 706 |
allfree)) |
| 707 |
goto error; |
| 708 |
if (entry->ste_free || entry->ste_child_free) |
| 709 |
ste->ste_child_free = 1; |
| 710 |
} |
| 711 |
|
| 712 |
if (PyDict_Update(newfree, allfree) < 0) |
| 713 |
goto error; |
| 714 |
if (ste->ste_type == FunctionBlock && !analyze_cells(scope, newfree)) |
| 715 |
goto error; |
| 716 |
if (!update_symbols(ste->ste_symbols, scope, bound, newfree, |
| 717 |
ste->ste_type == ClassBlock)) |
| 718 |
goto error; |
| 719 |
if (!check_unoptimized(ste)) |
| 720 |
goto error; |
| 721 |
|
| 722 |
if (PyDict_Update(free, newfree) < 0) |
| 723 |
goto error; |
| 724 |
success = 1; |
| 725 |
error: |
| 726 |
Py_XDECREF(local); |
| 727 |
Py_XDECREF(scope); |
| 728 |
Py_XDECREF(newbound); |
| 729 |
Py_XDECREF(newglobal); |
| 730 |
Py_XDECREF(newfree); |
| 731 |
Py_XDECREF(allfree); |
| 732 |
if (!success) |
| 733 |
assert(PyErr_Occurred()); |
| 734 |
return success; |
| 735 |
} |
| 736 |
|
| 737 |
static int |
| 738 |
analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free, |
| 739 |
PyObject *global, PyObject* child_free) |
| 740 |
{ |
| 741 |
PyObject *temp_bound = NULL, *temp_global = NULL, *temp_free = NULL; |
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
temp_bound = PyDict_New(); |
| 751 |
if (!temp_bound) |
| 752 |
goto error; |
| 753 |
if (PyDict_Update(temp_bound, bound) < 0) |
| 754 |
goto error; |
| 755 |
temp_free = PyDict_New(); |
| 756 |
if (!temp_free) |
| 757 |
goto error; |
| 758 |
if (PyDict_Update(temp_free, free) < 0) |
| 759 |
goto error; |
| 760 |
temp_global = PyDict_New(); |
| 761 |
if (!temp_global) |
| 762 |
goto error; |
| 763 |
if (PyDict_Update(temp_global, global) < 0) |
| 764 |
goto error; |
| 765 |
|
| 766 |
if (!analyze_block(entry, temp_bound, temp_free, temp_global)) |
| 767 |
goto error; |
| 768 |
if (PyDict_Update(child_free, temp_free) < 0) |
| 769 |
goto error; |
| 770 |
Py_DECREF(temp_bound); |
| 771 |
Py_DECREF(temp_free); |
| 772 |
Py_DECREF(temp_global); |
| 773 |
return 1; |
| 774 |
error: |
| 775 |
Py_XDECREF(temp_bound); |
| 776 |
Py_XDECREF(temp_free); |
| 777 |
Py_XDECREF(temp_global); |
| 778 |
return 0; |
| 779 |
} |
| 780 |
|
| 781 |
static int |
| 782 |
symtable_analyze(struct symtable *st) |
| 783 |
{ |
| 784 |
PyObject *free, *global; |
| 785 |
int r; |
| 786 |
|
| 787 |
free = PyDict_New(); |
| 788 |
if (!free) |
| 789 |
return 0; |
| 790 |
global = PyDict_New(); |
| 791 |
if (!global) { |
| 792 |
Py_DECREF(free); |
| 793 |
return 0; |
| 794 |
} |
| 795 |
r = analyze_block(st->st_top, NULL, free, global); |
| 796 |
Py_DECREF(free); |
| 797 |
Py_DECREF(global); |
| 798 |
return r; |
| 799 |
} |
| 800 |
|
| 801 |
|
| 802 |
static int |
| 803 |
symtable_warn(struct symtable *st, PyObject *warn, const char *msg, int lineno) |
| 804 |
{ |
| 805 |
if (lineno < 0) { |
| 806 |
lineno = st->st_cur->ste_lineno; |
| 807 |
} |
| 808 |
if (PyErr_WarnExplicit(warn, msg, st->st_filename, lineno, NULL, NULL) < 0) { |
| 809 |
if (PyErr_ExceptionMatches(warn)) { |
| 810 |
|
| 811 |
|
| 812 |
PyErr_Clear(); |
| 813 |
PyErr_SetString(PyExc_SyntaxError, msg); |
| 814 |
PyErr_SyntaxLocation(st->st_filename, lineno); |
| 815 |
} |
| 816 |
return 0; |
| 817 |
} |
| 818 |
return 1; |
| 819 |
} |
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
|
| 826 |
static int |
| 827 |
symtable_exit_block(struct symtable *st, void *ast) |
| 828 |
{ |
| 829 |
Py_ssize_t end; |
| 830 |
|
| 831 |
Py_CLEAR(st->st_cur); |
| 832 |
end = PyList_GET_SIZE(st->st_stack) - 1; |
| 833 |
if (end >= 0) { |
| 834 |
st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack, |
| 835 |
end); |
| 836 |
if (st->st_cur == NULL) |
| 837 |
return 0; |
| 838 |
Py_INCREF(st->st_cur); |
| 839 |
if (PySequence_DelItem(st->st_stack, end) < 0) |
| 840 |
return 0; |
| 841 |
} |
| 842 |
return 1; |
| 843 |
} |
| 844 |
|
| 845 |
static int |
| 846 |
symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block, |
| 847 |
void *ast, int lineno) |
| 848 |
{ |
| 849 |
PySTEntryObject *prev = NULL; |
| 850 |
|
| 851 |
if (st->st_cur) { |
| 852 |
prev = st->st_cur; |
| 853 |
if (PyList_Append(st->st_stack, (PyObject *)st->st_cur) < 0) { |
| 854 |
return 0; |
| 855 |
} |
| 856 |
Py_DECREF(st->st_cur); |
| 857 |
} |
| 858 |
st->st_cur = ste_new(st, name, block, ast, lineno); |
| 859 |
if (st->st_cur == NULL) |
| 860 |
return 0; |
| 861 |
if (block == ModuleBlock) |
| 862 |
st->st_global = st->st_cur->ste_symbols; |
| 863 |
if (prev) { |
| 864 |
if (PyList_Append(prev->ste_children, |
| 865 |
(PyObject *)st->st_cur) < 0) { |
| 866 |
return 0; |
| 867 |
} |
| 868 |
} |
| 869 |
return 1; |
| 870 |
} |
| 871 |
|
| 872 |
static long |
| 873 |
symtable_lookup(struct symtable *st, PyObject *name) |
| 874 |
{ |
| 875 |
PyObject *o; |
| 876 |
PyObject *mangled = _Py_Mangle(st->st_private, name); |
| 877 |
if (!mangled) |
| 878 |
return 0; |
| 879 |
o = PyDict_GetItem(st->st_cur->ste_symbols, mangled); |
| 880 |
Py_DECREF(mangled); |
| 881 |
if (!o) |
| 882 |
return 0; |
| 883 |
return PyInt_AsLong(o); |
| 884 |
} |
| 885 |
|
| 886 |
static int |
| 887 |
symtable_add_def(struct symtable *st, PyObject *name, int flag) |
| 888 |
{ |
| 889 |
PyObject *o; |
| 890 |
PyObject *dict; |
| 891 |
long val; |
| 892 |
PyObject *mangled = _Py_Mangle(st->st_private, name); |
| 893 |
|
| 894 |
if (!mangled) |
| 895 |
return 0; |
| 896 |
dict = st->st_cur->ste_symbols; |
| 897 |
if ((o = PyDict_GetItem(dict, mangled))) { |
| 898 |
val = PyInt_AS_LONG(o); |
| 899 |
if ((flag & DEF_PARAM) && (val & DEF_PARAM)) { |
| 900 |
|
| 901 |
PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT, |
| 902 |
PyString_AsString(name)); |
| 903 |
PyErr_SyntaxLocation(st->st_filename, |
| 904 |
st->st_cur->ste_lineno); |
| 905 |
goto error; |
| 906 |
} |
| 907 |
val |= flag; |
| 908 |
} else |
| 909 |
val = flag; |
| 910 |
o = PyInt_FromLong(val); |
| 911 |
if (o == NULL) |
| 912 |
goto error; |
| 913 |
if (PyDict_SetItem(dict, mangled, o) < 0) { |
| 914 |
Py_DECREF(o); |
| 915 |
goto error; |
| 916 |
} |
| 917 |
Py_DECREF(o); |
| 918 |
|
| 919 |
if (flag & DEF_PARAM) { |
| 920 |
if (PyList_Append(st->st_cur->ste_varnames, mangled) < 0) |
| 921 |
goto error; |
| 922 |
} else if (flag & DEF_GLOBAL) { |
| 923 |
|
| 924 |
|
| 925 |
val = flag; |
| 926 |
if ((o = PyDict_GetItem(st->st_global, mangled))) { |
| 927 |
val |= PyInt_AS_LONG(o); |
| 928 |
} |
| 929 |
o = PyInt_FromLong(val); |
| 930 |
if (o == NULL) |
| 931 |
goto error; |
| 932 |
if (PyDict_SetItem(st->st_global, mangled, o) < 0) { |
| 933 |
Py_DECREF(o); |
| 934 |
goto error; |
| 935 |
} |
| 936 |
Py_DECREF(o); |
| 937 |
} |
| 938 |
Py_DECREF(mangled); |
| 939 |
return 1; |
| 940 |
|
| 941 |
error: |
| 942 |
Py_DECREF(mangled); |
| 943 |
return 0; |
| 944 |
} |
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
|
| 954 |
#define VISIT(ST, TYPE, V) \ |
| 955 |
if (!symtable_visit_ ## TYPE((ST), (V))) \ |
| 956 |
return 0; |
| 957 |
|
| 958 |
#define VISIT_IN_BLOCK(ST, TYPE, V, S) \ |
| 959 |
if (!symtable_visit_ ## TYPE((ST), (V))) { \ |
| 960 |
symtable_exit_block((ST), (S)); \ |
| 961 |
return 0; \ |
| 962 |
} |
| 963 |
|
| 964 |
#define VISIT_SEQ(ST, TYPE, SEQ) { \ |
| 965 |
int i; \ |
| 966 |
asdl_seq *seq = (SEQ); \ |
| 967 |
for (i = 0; i < asdl_seq_LEN(seq); i++) { \ |
| 968 |
TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \ |
| 969 |
if (!symtable_visit_ ## TYPE((ST), elt)) \ |
| 970 |
return 0; \ |
| 971 |
} \ |
| 972 |
} |
| 973 |
|
| 974 |
#define VISIT_SEQ_IN_BLOCK(ST, TYPE, SEQ, S) { \ |
| 975 |
int i; \ |
| 976 |
asdl_seq *seq = (SEQ); \ |
| 977 |
for (i = 0; i < asdl_seq_LEN(seq); i++) { \ |
| 978 |
TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \ |
| 979 |
if (!symtable_visit_ ## TYPE((ST), elt)) { \ |
| 980 |
symtable_exit_block((ST), (S)); \ |
| 981 |
return 0; \ |
| 982 |
} \ |
| 983 |
} \ |
| 984 |
} |
| 985 |
|
| 986 |
#define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \ |
| 987 |
int i; \ |
| 988 |
asdl_seq *seq = (SEQ); \ |
| 989 |
for (i = (START); i < asdl_seq_LEN(seq); i++) { \ |
| 990 |
TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \ |
| 991 |
if (!symtable_visit_ ## TYPE((ST), elt)) \ |
| 992 |
return 0; \ |
| 993 |
} \ |
| 994 |
} |
| 995 |
|
| 996 |
#define VISIT_SEQ_TAIL_IN_BLOCK(ST, TYPE, SEQ, START, S) { \ |
| 997 |
int i; \ |
| 998 |
asdl_seq *seq = (SEQ); \ |
| 999 |
for (i = (START); i < asdl_seq_LEN(seq); i++) { \ |
| 1000 |
TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \ |
| 1001 |
if (!symtable_visit_ ## TYPE((ST), elt)) { \ |
| 1002 |
symtable_exit_block((ST), (S)); \ |
| 1003 |
return 0; \ |
| 1004 |
} \ |
| 1005 |
} \ |
| 1006 |
} |
| 1007 |
|
| 1008 |
static int |
| 1009 |
symtable_visit_stmt(struct symtable *st, stmt_ty s) |
| 1010 |
{ |
| 1011 |
switch (s->kind) { |
| 1012 |
case FunctionDef_kind: |
| 1013 |
if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL)) |
| 1014 |
return 0; |
| 1015 |
if (s->v.FunctionDef.args->defaults) |
| 1016 |
VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults); |
| 1017 |
if (s->v.FunctionDef.decorator_list) |
| 1018 |
VISIT_SEQ(st, expr, s->v.FunctionDef.decorator_list); |
| 1019 |
if (!symtable_enter_block(st, s->v.FunctionDef.name, |
| 1020 |
FunctionBlock, (void *)s, s->lineno)) |
| 1021 |
return 0; |
| 1022 |
VISIT_IN_BLOCK(st, arguments, s->v.FunctionDef.args, s); |
| 1023 |
VISIT_SEQ_IN_BLOCK(st, stmt, s->v.FunctionDef.body, s); |
| 1024 |
if (!symtable_exit_block(st, s)) |
| 1025 |
return 0; |
| 1026 |
break; |
| 1027 |
case ClassDef_kind: { |
| 1028 |
PyObject *tmp; |
| 1029 |
if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL)) |
| 1030 |
return 0; |
| 1031 |
VISIT_SEQ(st, expr, s->v.ClassDef.bases); |
| 1032 |
if (s->v.ClassDef.decorator_list) |
| 1033 |
VISIT_SEQ(st, expr, s->v.ClassDef.decorator_list); |
| 1034 |
if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock, |
| 1035 |
(void *)s, s->lineno)) |
| 1036 |
return 0; |
| 1037 |
tmp = st->st_private; |
| 1038 |
st->st_private = s->v.ClassDef.name; |
| 1039 |
VISIT_SEQ_IN_BLOCK(st, stmt, s->v.ClassDef.body, s); |
| 1040 |
st->st_private = tmp; |
| 1041 |
if (!symtable_exit_block(st, s)) |
| 1042 |
return 0; |
| 1043 |
break; |
| 1044 |
} |
| 1045 |
case Return_kind: |
| 1046 |
if (s->v.Return.value) { |
| 1047 |
VISIT(st, expr, s->v.Return.value); |
| 1048 |
st->st_cur->ste_returns_value = 1; |
| 1049 |
if (st->st_cur->ste_generator) { |
| 1050 |
PyErr_SetString(PyExc_SyntaxError, |
| 1051 |
RETURN_VAL_IN_GENERATOR); |
| 1052 |
PyErr_SyntaxLocation(st->st_filename, |
| 1053 |
s->lineno); |
| 1054 |
return 0; |
| 1055 |
} |
| 1056 |
} |
| 1057 |
break; |
| 1058 |
case Delete_kind: |
| 1059 |
VISIT_SEQ(st, expr, s->v.Delete.targets); |
| 1060 |
break; |
| 1061 |
case Assign_kind: |
| 1062 |
VISIT_SEQ(st, expr, s->v.Assign.targets); |
| 1063 |
VISIT(st, expr, s->v.Assign.value); |
| 1064 |
break; |
| 1065 |
case AugAssign_kind: |
| 1066 |
VISIT(st, expr, s->v.AugAssign.target); |
| 1067 |
VISIT(st, expr, s->v.AugAssign.value); |
| 1068 |
break; |
| 1069 |
case Print_kind: |
| 1070 |
if (s->v.Print.dest) |
| 1071 |
VISIT(st, expr, s->v.Print.dest); |
| 1072 |
VISIT_SEQ(st, expr, s->v.Print.values); |
| 1073 |
break; |
| 1074 |
case For_kind: |
| 1075 |
VISIT(st, expr, s->v.For.target); |
| 1076 |
VISIT(st, expr, s->v.For.iter); |
| 1077 |
VISIT_SEQ(st, stmt, s->v.For.body); |
| 1078 |
if (s->v.For.orelse) |
| 1079 |
VISIT_SEQ(st, stmt, s->v.For.orelse); |
| 1080 |
break; |
| 1081 |
case While_kind: |
| 1082 |
VISIT(st, expr, s->v.While.test); |
| 1083 |
VISIT_SEQ(st, stmt, s->v.While.body); |
| 1084 |
if (s->v.While.orelse) |
| 1085 |
VISIT_SEQ(st, stmt, s->v.While.orelse); |
| 1086 |
break; |
| 1087 |
case If_kind: |
| 1088 |
|
| 1089 |
VISIT(st, expr, s->v.If.test); |
| 1090 |
VISIT_SEQ(st, stmt, s->v.If.body); |
| 1091 |
if (s->v.If.orelse) |
| 1092 |
VISIT_SEQ(st, stmt, s->v.If.orelse); |
| 1093 |
break; |
| 1094 |
case Raise_kind: |
| 1095 |
if (s->v.Raise.type) { |
| 1096 |
VISIT(st, expr, s->v.Raise.type); |
| 1097 |
if (s->v.Raise.inst) { |
| 1098 |
VISIT(st, expr, s->v.Raise.inst); |
| 1099 |
if (s->v.Raise.tback) |
| 1100 |
VISIT(st, expr, s->v.Raise.tback); |
| 1101 |
} |
| 1102 |
} |
| 1103 |
break; |
| 1104 |
case TryExcept_kind: |
| 1105 |
VISIT_SEQ(st, stmt, s->v.TryExcept.body); |
| 1106 |
VISIT_SEQ(st, stmt, s->v.TryExcept.orelse); |
| 1107 |
VISIT_SEQ(st, excepthandler, s->v.TryExcept.handlers); |
| 1108 |
break; |
| 1109 |
case TryFinally_kind: |
| 1110 |
VISIT_SEQ(st, stmt, s->v.TryFinally.body); |
| 1111 |
VISIT_SEQ(st, stmt, s->v.TryFinally.finalbody); |
| 1112 |
break; |
| 1113 |
case Assert_kind: |
| 1114 |
VISIT(st, expr, s->v.Assert.test); |
| 1115 |
if (s->v.Assert.msg) |
| 1116 |
VISIT(st, expr, s->v.Assert.msg); |
| 1117 |
break; |
| 1118 |
case Import_kind: |
| 1119 |
VISIT_SEQ(st, alias, s->v.Import.names); |
| 1120 |
|
| 1121 |
|
| 1122 |
if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno) |
| 1123 |
st->st_cur->ste_opt_lineno = s->lineno; |
| 1124 |
break; |
| 1125 |
case ImportFrom_kind: |
| 1126 |
VISIT_SEQ(st, alias, s->v.ImportFrom.names); |
| 1127 |
|
| 1128 |
|
| 1129 |
if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno) |
| 1130 |
st->st_cur->ste_opt_lineno = s->lineno; |
| 1131 |
break; |
| 1132 |
case Exec_kind: |
| 1133 |
VISIT(st, expr, s->v.Exec.body); |
| 1134 |
if (!st->st_cur->ste_opt_lineno) |
| 1135 |
st->st_cur->ste_opt_lineno = s->lineno; |
| 1136 |
if (s->v.Exec.globals) { |
| 1137 |
st->st_cur->ste_unoptimized |= OPT_EXEC; |
| 1138 |
VISIT(st, expr, s->v.Exec.globals); |
| 1139 |
if (s->v.Exec.locals) |
| 1140 |
VISIT(st, expr, s->v.Exec.locals); |
| 1141 |
} else { |
| 1142 |
st->st_cur->ste_unoptimized |= OPT_BARE_EXEC; |
| 1143 |
} |
| 1144 |
break; |
| 1145 |
case Global_kind: { |
| 1146 |
int i; |
| 1147 |
asdl_seq *seq = s->v.Global.names; |
| 1148 |
for (i = 0; i < asdl_seq_LEN(seq); i++) { |
| 1149 |
identifier name = (identifier)asdl_seq_GET(seq, i); |
| 1150 |
char *c_name = PyString_AS_STRING(name); |
| 1151 |
long cur = symtable_lookup(st, name); |
| 1152 |
if (cur < 0) |
| 1153 |
return 0; |
| 1154 |
if (cur & (DEF_LOCAL | USE)) { |
| 1155 |
char buf[256]; |
| 1156 |
if (cur & DEF_LOCAL) |
| 1157 |
PyOS_snprintf(buf, sizeof(buf), |
| 1158 |
GLOBAL_AFTER_ASSIGN, |
| 1159 |
c_name); |
| 1160 |
else |
| 1161 |
PyOS_snprintf(buf, sizeof(buf), |
| 1162 |
GLOBAL_AFTER_USE, |
| 1163 |
c_name); |
| 1164 |
if (!symtable_warn(st, PyExc_SyntaxWarning, buf, s->lineno)) |
| 1165 |
return 0; |
| 1166 |
} |
| 1167 |
if (!symtable_add_def(st, name, DEF_GLOBAL)) |
| 1168 |
return 0; |
| 1169 |
} |
| 1170 |
break; |
| 1171 |
} |
| 1172 |
case Expr_kind: |
| 1173 |
VISIT(st, expr, s->v.Expr.value); |
| 1174 |
break; |
| 1175 |
case Pass_kind: |
| 1176 |
case Break_kind: |
| 1177 |
case Continue_kind: |
| 1178 |
|
| 1179 |
break; |
| 1180 |
case With_kind: |
| 1181 |
VISIT(st, expr, s->v.With.context_expr); |
| 1182 |
if (s->v.With.optional_vars) { |
| 1183 |
VISIT(st, expr, s->v.With.optional_vars); |
| 1184 |
} |
| 1185 |
VISIT_SEQ(st, stmt, s->v.With.body); |
| 1186 |
break; |
| 1187 |
} |
| 1188 |
return 1; |
| 1189 |
} |
| 1190 |
|
| 1191 |
static int |
| 1192 |
symtable_visit_expr(struct symtable *st, expr_ty e) |
| 1193 |
{ |
| 1194 |
switch (e->kind) { |
| 1195 |
case BoolOp_kind: |
| 1196 |
VISIT_SEQ(st, expr, e->v.BoolOp.values); |
| 1197 |
break; |
| 1198 |
case BinOp_kind: |
| 1199 |
VISIT(st, expr, e->v.BinOp.left); |
| 1200 |
VISIT(st, expr, e->v.BinOp.right); |
| 1201 |
break; |
| 1202 |
case UnaryOp_kind: |
| 1203 |
VISIT(st, expr, e->v.UnaryOp.operand); |
| 1204 |
break; |
| 1205 |
case Lambda_kind: { |
| 1206 |
if (!GET_IDENTIFIER(lambda)) |
| 1207 |
return 0; |
| 1208 |
if (e->v.Lambda.args->defaults) |
| 1209 |
VISIT_SEQ(st, expr, e->v.Lambda.args->defaults); |
| 1210 |
if (!symtable_enter_block(st, lambda, |
| 1211 |
FunctionBlock, (void *)e, e->lineno)) |
| 1212 |
return 0; |
| 1213 |
VISIT_IN_BLOCK(st, arguments, e->v.Lambda.args, (void*)e); |
| 1214 |
VISIT_IN_BLOCK(st, expr, e->v.Lambda.body, (void*)e); |
| 1215 |
if (!symtable_exit_block(st, (void *)e)) |
| 1216 |
return 0; |
| 1217 |
break; |
| 1218 |
} |
| 1219 |
case IfExp_kind: |
| 1220 |
VISIT(st, expr, e->v.IfExp.test); |
| 1221 |
VISIT(st, expr, e->v.IfExp.body); |
| 1222 |
VISIT(st, expr, e->v.IfExp.orelse); |
| 1223 |
break; |
| 1224 |
case Dict_kind: |
| 1225 |
VISIT_SEQ(st, expr, e->v.Dict.keys); |
| 1226 |
VISIT_SEQ(st, expr, e->v.Dict.values); |
| 1227 |
break; |
| 1228 |
case Set_kind: |
| 1229 |
VISIT_SEQ(st, expr, e->v.Set.elts); |
| 1230 |
break; |
| 1231 |
case ListComp_kind: |
| 1232 |
if (!symtable_visit_listcomp(st, e)) |
| 1233 |
return 0; |
| 1234 |
break; |
| 1235 |
case GeneratorExp_kind: |
| 1236 |
if (!symtable_visit_genexp(st, e)) |
| 1237 |
return 0; |
| 1238 |
break; |
| 1239 |
case SetComp_kind: |
| 1240 |
if (!symtable_visit_setcomp(st, e)) |
| 1241 |
return 0; |
| 1242 |
break; |
| 1243 |
case DictComp_kind: |
| 1244 |
if (!symtable_visit_dictcomp(st, e)) |
| 1245 |
return 0; |
| 1246 |
break; |
| 1247 |
case Yield_kind: |
| 1248 |
if (e->v.Yield.value) |
| 1249 |
VISIT(st, expr, e->v.Yield.value); |
| 1250 |
st->st_cur->ste_generator = 1; |
| 1251 |
if (st->st_cur->ste_returns_value) { |
| 1252 |
PyErr_SetString(PyExc_SyntaxError, |
| 1253 |
RETURN_VAL_IN_GENERATOR); |
| 1254 |
PyErr_SyntaxLocation(st->st_filename, |
| 1255 |
e->lineno); |
| 1256 |
return 0; |
| 1257 |
} |
| 1258 |
break; |
| 1259 |
case Compare_kind: |
| 1260 |
VISIT(st, expr, e->v.Compare.left); |
| 1261 |
VISIT_SEQ(st, expr, e->v.Compare.comparators); |
| 1262 |
break; |
| 1263 |
case Call_kind: |
| 1264 |
VISIT(st, expr, e->v.Call.func); |
| 1265 |
VISIT_SEQ(st, expr, e->v.Call.args); |
| 1266 |
VISIT_SEQ(st, keyword, e->v.Call.keywords); |
| 1267 |
if (e->v.Call.starargs) |
| 1268 |
VISIT(st, expr, e->v.Call.starargs); |
| 1269 |
if (e->v.Call.kwargs) |
| 1270 |
VISIT(st, expr, e->v.Call.kwargs); |
| 1271 |
break; |
| 1272 |
case Repr_kind: |
| 1273 |
VISIT(st, expr, e->v.Repr.value); |
| 1274 |
break; |
| 1275 |
case Num_kind: |
| 1276 |
case Str_kind: |
| 1277 |
|
| 1278 |
break; |
| 1279 |
|
| 1280 |
case Attribute_kind: |
| 1281 |
VISIT(st, expr, e->v.Attribute.value); |
| 1282 |
break; |
| 1283 |
case Subscript_kind: |
| 1284 |
VISIT(st, expr, e->v.Subscript.value); |
| 1285 |
VISIT(st, slice, e->v.Subscript.slice); |
| 1286 |
break; |
| 1287 |
case Name_kind: |
| 1288 |
if (!symtable_add_def(st, e->v.Name.id, |
| 1289 |
e->v.Name.ctx == Load ? USE : DEF_LOCAL)) |
| 1290 |
return 0; |
| 1291 |
break; |
| 1292 |
|
| 1293 |
case List_kind: |
| 1294 |
VISIT_SEQ(st, expr, e->v.List.elts); |
| 1295 |
break; |
| 1296 |
case Tuple_kind: |
| 1297 |
VISIT_SEQ(st, expr, e->v.Tuple.elts); |
| 1298 |
break; |
| 1299 |
} |
| 1300 |
return 1; |
| 1301 |
} |
| 1302 |
|
| 1303 |
static int |
| 1304 |
symtable_implicit_arg(struct symtable *st, int pos) |
| 1305 |
{ |
| 1306 |
PyObject *id = PyString_FromFormat(".%d", pos); |
| 1307 |
if (id == NULL) |
| 1308 |
return 0; |
| 1309 |
if (!symtable_add_def(st, id, DEF_PARAM)) { |
| 1310 |
Py_DECREF(id); |
| 1311 |
return 0; |
| 1312 |
} |
| 1313 |
Py_DECREF(id); |
| 1314 |
return 1; |
| 1315 |
} |
| 1316 |
|
| 1317 |
static int |
| 1318 |
symtable_visit_params(struct symtable *st, asdl_seq *args, int toplevel) |
| 1319 |
{ |
| 1320 |
int i; |
| 1321 |
|
| 1322 |
|
| 1323 |
for (i = 0; i < asdl_seq_LEN(args); i++) { |
| 1324 |
expr_ty arg = (expr_ty)asdl_seq_GET(args, i); |
| 1325 |
if (arg->kind == Name_kind) { |
| 1326 |
assert(arg->v.Name.ctx == Param || |
| 1327 |
(arg->v.Name.ctx == Store && !toplevel)); |
| 1328 |
if (!symtable_add_def(st, arg->v.Name.id, DEF_PARAM)) |
| 1329 |
return 0; |
| 1330 |
} |
| 1331 |
else if (arg->kind == Tuple_kind) { |
| 1332 |
assert(arg->v.Tuple.ctx == Store); |
| 1333 |
if (toplevel) { |
| 1334 |
if (!symtable_implicit_arg(st, i)) |
| 1335 |
return 0; |
| 1336 |
} |
| 1337 |
} |
| 1338 |
else { |
| 1339 |
PyErr_SetString(PyExc_SyntaxError, |
| 1340 |
"invalid expression in parameter list"); |
| 1341 |
PyErr_SyntaxLocation(st->st_filename, |
| 1342 |
st->st_cur->ste_lineno); |
| 1343 |
return 0; |
| 1344 |
} |
| 1345 |
} |
| 1346 |
|
| 1347 |
if (!toplevel) { |
| 1348 |
if (!symtable_visit_params_nested(st, args)) |
| 1349 |
return 0; |
| 1350 |
} |
| 1351 |
|
| 1352 |
return 1; |
| 1353 |
} |
| 1354 |
|
| 1355 |
static int |
| 1356 |
symtable_visit_params_nested(struct symtable *st, asdl_seq *args) |
| 1357 |
{ |
| 1358 |
int i; |
| 1359 |
for (i = 0; i < asdl_seq_LEN(args); i++) { |
| 1360 |
expr_ty arg = (expr_ty)asdl_seq_GET(args, i); |
| 1361 |
if (arg->kind == Tuple_kind && |
| 1362 |
!symtable_visit_params(st, arg->v.Tuple.elts, 0)) |
| 1363 |
return 0; |
| 1364 |
} |
| 1365 |
|
| 1366 |
return 1; |
| 1367 |
} |
| 1368 |
|
| 1369 |
static int |
| 1370 |
symtable_visit_arguments(struct symtable *st, arguments_ty a) |
| 1371 |
{ |
| 1372 |
|
| 1373 |
|
| 1374 |
|
| 1375 |
if (a->args && !symtable_visit_params(st, a->args, 1)) |
| 1376 |
return 0; |
| 1377 |
if (a->vararg) { |
| 1378 |
if (!symtable_add_def(st, a->vararg, DEF_PARAM)) |
| 1379 |
return 0; |
| 1380 |
st->st_cur->ste_varargs = 1; |
| 1381 |
} |
| 1382 |
if (a->kwarg) { |
| 1383 |
if (!symtable_add_def(st, a->kwarg, DEF_PARAM)) |
| 1384 |
return 0; |
| 1385 |
st->st_cur->ste_varkeywords = 1; |
| 1386 |
} |
| 1387 |
if (a->args && !symtable_visit_params_nested(st, a->args)) |
| 1388 |
return 0; |
| 1389 |
return 1; |
| 1390 |
} |
| 1391 |
|
| 1392 |
|
| 1393 |
static int |
| 1394 |
symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh) |
| 1395 |
{ |
| 1396 |
if (eh->v.ExceptHandler.type) |
| 1397 |
VISIT(st, expr, eh->v.ExceptHandler.type); |
| 1398 |
if (eh->v.ExceptHandler.name) |
| 1399 |
VISIT(st, expr, eh->v.ExceptHandler.name); |
| 1400 |
VISIT_SEQ(st, stmt, eh->v.ExceptHandler.body); |
| 1401 |
return 1; |
| 1402 |
} |
| 1403 |
|
| 1404 |
|
| 1405 |
static int |
| 1406 |
symtable_visit_alias(struct symtable *st, alias_ty a) |
| 1407 |
{ |
| 1408 |
|
| 1409 |
|
| 1410 |
|
| 1411 |
|
| 1412 |
PyObject *store_name; |
| 1413 |
PyObject *name = (a->asname == NULL) ? a->name : a->asname; |
| 1414 |
const char *base = PyString_AS_STRING(name); |
| 1415 |
char *dot = strchr(base, '.'); |
| 1416 |
if (dot) { |
| 1417 |
store_name = PyString_FromStringAndSize(base, dot - base); |
| 1418 |
if (!store_name) |
| 1419 |
return 0; |
| 1420 |
} |
| 1421 |
else { |
| 1422 |
store_name = name; |
| 1423 |
Py_INCREF(store_name); |
| 1424 |
} |
| 1425 |
if (strcmp(PyString_AS_STRING(name), "*")) { |
| 1426 |
int r = symtable_add_def(st, store_name, DEF_IMPORT); |
| 1427 |
Py_DECREF(store_name); |
| 1428 |
return r; |
| 1429 |
} |
| 1430 |
else { |
| 1431 |
if (st->st_cur->ste_type != ModuleBlock && |
| 1432 |
!symtable_warn(st, PyExc_SyntaxWarning, IMPORT_STAR_WARNING, -1)) |
| 1433 |
{ |
| 1434 |
Py_DECREF(store_name); |
| 1435 |
return 0; |
| 1436 |
} |
| 1437 |
st->st_cur->ste_unoptimized |= OPT_IMPORT_STAR; |
| 1438 |
Py_DECREF(store_name); |
| 1439 |
return 1; |
| 1440 |
} |
| 1441 |
} |
| 1442 |
|
| 1443 |
|
| 1444 |
static int |
| 1445 |
symtable_visit_comprehension(struct symtable *st, comprehension_ty lc) |
| 1446 |
{ |
| 1447 |
VISIT(st, expr, lc->target); |
| 1448 |
VISIT(st, expr, lc->iter); |
| 1449 |
VISIT_SEQ(st, expr, lc->ifs); |
| 1450 |
return 1; |
| 1451 |
} |
| 1452 |
|
| 1453 |
|
| 1454 |
static int |
| 1455 |
symtable_visit_keyword(struct symtable *st, keyword_ty k) |
| 1456 |
{ |
| 1457 |
VISIT(st, expr, k->value); |
| 1458 |
return 1; |
| 1459 |
} |
| 1460 |
|
| 1461 |
|
| 1462 |
static int |
| 1463 |
symtable_visit_slice(struct symtable *st, slice_ty s) |
| 1464 |
{ |
| 1465 |
switch (s->kind) { |
| 1466 |
case Slice_kind: |
| 1467 |
if (s->v.Slice.lower) |
| 1468 |
VISIT(st, expr, s->v.Slice.lower) |
| 1469 |
if (s->v.Slice.upper) |
| 1470 |
VISIT(st, expr, s->v.Slice.upper) |
| 1471 |
if (s->v.Slice.step) |
| 1472 |
VISIT(st, expr, s->v.Slice.step) |
| 1473 |
break; |
| 1474 |
case ExtSlice_kind: |
| 1475 |
VISIT_SEQ(st, slice, s->v.ExtSlice.dims) |
| 1476 |
break; |
| 1477 |
case Index_kind: |
| 1478 |
VISIT(st, expr, s->v.Index.value) |
| 1479 |
break; |
| 1480 |
case Ellipsis_kind: |
| 1481 |
break; |
| 1482 |
} |
| 1483 |
return 1; |
| 1484 |
} |
| 1485 |
|
| 1486 |
static int |
| 1487 |
symtable_new_tmpname(struct symtable *st) |
| 1488 |
{ |
| 1489 |
char tmpname[256]; |
| 1490 |
identifier tmp; |
| 1491 |
|
| 1492 |
PyOS_snprintf(tmpname, sizeof(tmpname), "_[%d]", |
| 1493 |
++st->st_cur->ste_tmpname); |
| 1494 |
tmp = PyString_InternFromString(tmpname); |
| 1495 |
if (!tmp) |
| 1496 |
return 0; |
| 1497 |
if (!symtable_add_def(st, tmp, DEF_LOCAL)) |
| 1498 |
return 0; |
| 1499 |
Py_DECREF(tmp); |
| 1500 |
return 1; |
| 1501 |
} |
| 1502 |
|
| 1503 |
static int |
| 1504 |
symtable_handle_comprehension(struct symtable *st, expr_ty e, |
| 1505 |
identifier scope_name, asdl_seq *generators, |
| 1506 |
expr_ty elt, expr_ty value) |
| 1507 |
{ |
| 1508 |
int is_generator = (e->kind == GeneratorExp_kind); |
| 1509 |
int needs_tmp = !is_generator; |
| 1510 |
comprehension_ty outermost = ((comprehension_ty) |
| 1511 |
asdl_seq_GET(generators, 0)); |
| 1512 |
|
| 1513 |
VISIT(st, expr, outermost->iter); |
| 1514 |
|
| 1515 |
if (!scope_name || |
| 1516 |
!symtable_enter_block(st, scope_name, FunctionBlock, (void *)e, 0)) { |
| 1517 |
return 0; |
| 1518 |
} |
| 1519 |
|
| 1520 |
|
| 1521 |
is_generator |= st->st_cur->ste_generator; |
| 1522 |
st->st_cur->ste_generator = 0; |
| 1523 |
|
| 1524 |
if (!symtable_implicit_arg(st, 0)) { |
| 1525 |
symtable_exit_block(st, (void *)e); |
| 1526 |
return 0; |
| 1527 |
} |
| 1528 |
|
| 1529 |
if (needs_tmp && !symtable_new_tmpname(st)) { |
| 1530 |
symtable_exit_block(st, (void *)e); |
| 1531 |
return 0; |
| 1532 |
} |
| 1533 |
VISIT_IN_BLOCK(st, expr, outermost->target, (void*)e); |
| 1534 |
VISIT_SEQ_IN_BLOCK(st, expr, outermost->ifs, (void*)e); |
| 1535 |
VISIT_SEQ_TAIL_IN_BLOCK(st, comprehension, |
| 1536 |
generators, 1, (void*)e); |
| 1537 |
if (value) |
| 1538 |
VISIT_IN_BLOCK(st, expr, value, (void*)e); |
| 1539 |
VISIT_IN_BLOCK(st, expr, elt, (void*)e); |
| 1540 |
if (Py_Py3kWarningFlag && st->st_cur->ste_generator) { |
| 1541 |
const char *msg = ( |
| 1542 |
(e->kind == SetComp_kind) ? "'yield' inside set comprehension" : |
| 1543 |
(e->kind == DictComp_kind) ? "'yield' inside dict comprehension" : |
| 1544 |
"'yield' inside generator expression"); |
| 1545 |
if (!symtable_warn(st, PyExc_DeprecationWarning, msg, -1)) { |
| 1546 |
symtable_exit_block(st, (void *)e); |
| 1547 |
return 0; |
| 1548 |
} |
| 1549 |
} |
| 1550 |
st->st_cur->ste_generator |= is_generator; |
| 1551 |
return symtable_exit_block(st, (void *)e); |
| 1552 |
} |
| 1553 |
|
| 1554 |
static int |
| 1555 |
symtable_visit_listcomp(struct symtable *st, expr_ty e) |
| 1556 |
{ |
| 1557 |
asdl_seq *generators = e->v.ListComp.generators; |
| 1558 |
int i, is_generator; |
| 1559 |
|
| 1560 |
|
| 1561 |
is_generator = st->st_cur->ste_generator; |
| 1562 |
st->st_cur->ste_generator = 0; |
| 1563 |
VISIT(st, expr, e->v.ListComp.elt); |
| 1564 |
for (i = 0; i < asdl_seq_LEN(generators); i++) { |
| 1565 |
comprehension_ty lc = (comprehension_ty)asdl_seq_GET(generators, i); |
| 1566 |
VISIT(st, expr, lc->target); |
| 1567 |
if (i == 0 && !st->st_cur->ste_generator) { |
| 1568 |
|
| 1569 |
VISIT(st, expr, lc->iter); |
| 1570 |
is_generator |= st->st_cur->ste_generator; |
| 1571 |
st->st_cur->ste_generator = 0; |
| 1572 |
} |
| 1573 |
else { |
| 1574 |
VISIT(st, expr, lc->iter); |
| 1575 |
} |
| 1576 |
VISIT_SEQ(st, expr, lc->ifs); |
| 1577 |
} |
| 1578 |
|
| 1579 |
if (Py_Py3kWarningFlag && st->st_cur->ste_generator) { |
| 1580 |
const char *msg = "'yield' inside list comprehension"; |
| 1581 |
if (!symtable_warn(st, PyExc_DeprecationWarning, msg, -1)) { |
| 1582 |
return 0; |
| 1583 |
} |
| 1584 |
} |
| 1585 |
st->st_cur->ste_generator |= is_generator; |
| 1586 |
return 1; |
| 1587 |
} |
| 1588 |
|
| 1589 |
static int |
| 1590 |
symtable_visit_genexp(struct symtable *st, expr_ty e) |
| 1591 |
{ |
| 1592 |
return symtable_handle_comprehension(st, e, GET_IDENTIFIER(genexpr), |
| 1593 |
e->v.GeneratorExp.generators, |
| 1594 |
e->v.GeneratorExp.elt, NULL); |
| 1595 |
} |
| 1596 |
|
| 1597 |
static int |
| 1598 |
symtable_visit_setcomp(struct symtable *st, expr_ty e) |
| 1599 |
{ |
| 1600 |
return symtable_handle_comprehension(st, e, GET_IDENTIFIER(setcomp), |
| 1601 |
e->v.SetComp.generators, |
| 1602 |
e->v.SetComp.elt, NULL); |
| 1603 |
} |
| 1604 |
|
| 1605 |
static int |
| 1606 |
symtable_visit_dictcomp(struct symtable *st, expr_ty e) |
| 1607 |
{ |
| 1608 |
return symtable_handle_comprehension(st, e, GET_IDENTIFIER(dictcomp), |
| 1609 |
e->v.DictComp.generators, |
| 1610 |
e->v.DictComp.key, |
| 1611 |
e->v.DictComp.value); |
| 1612 |
} |