diff options
Diffstat (limited to 'Python/symtable.c')
| -rw-r--r-- | Python/symtable.c | 97 | 
1 files changed, 54 insertions, 43 deletions
| diff --git a/Python/symtable.c b/Python/symtable.c index 951267342b..64910d8a55 100644 --- a/Python/symtable.c +++ b/Python/symtable.c @@ -47,7 +47,6 @@ ste_new(struct symtable *st, identifier name, _Py_block_ty block,      ste->ste_directives = NULL;      ste->ste_type = block; -    ste->ste_unoptimized = 0;      ste->ste_nested = 0;      ste->ste_free = 0;      ste->ste_varargs = 0; @@ -113,7 +112,6 @@ static PyMemberDef ste_memberlist[] = {      {"symbols",  T_OBJECT, OFF(ste_symbols), READONLY},      {"varnames", T_OBJECT, OFF(ste_varnames), READONLY},      {"children", T_OBJECT, OFF(ste_children), READONLY}, -    {"optimized",T_INT,    OFF(ste_unoptimized), READONLY},      {"nested",   T_INT,    OFF(ste_nested), READONLY},      {"type",     T_INT,    OFF(ste_type), READONLY},      {"lineno",   T_INT,    OFF(ste_lineno), READONLY}, @@ -182,7 +180,7 @@ static int symtable_visit_slice(struct symtable *st, slice_ty);  static int symtable_visit_params(struct symtable *st, asdl_seq *args);  static int symtable_visit_argannotations(struct symtable *st, asdl_seq *args);  static int symtable_implicit_arg(struct symtable *st, int pos); -static int symtable_visit_annotations(struct symtable *st, stmt_ty s); +static int symtable_visit_annotations(struct symtable *st, stmt_ty s, arguments_ty, expr_ty);  static int symtable_visit_withitem(struct symtable *st, withitem_ty item); @@ -271,7 +269,6 @@ PySymtable_BuildObject(mod_ty mod, PyObject *filename, PyFutureFeatures *future)      }      st->st_top = st->st_cur; -    st->st_cur->ste_unoptimized = OPT_TOPLEVEL;      switch (mod->kind) {      case Module_kind:          seq = mod->v.Module.body; @@ -1086,13 +1083,13 @@ error:      } \  } -#define VISIT_KWONLYDEFAULTS(ST, KW_DEFAULTS) { \ +#define VISIT_SEQ_WITH_NULL(ST, TYPE, SEQ) {     \      int i = 0; \ -    asdl_seq *seq = (KW_DEFAULTS); /* avoid variable capture */ \ +    asdl_seq *seq = (SEQ); /* avoid variable capture */ \      for (i = 0; i < asdl_seq_LEN(seq); i++) { \ -        expr_ty elt = (expr_ty)asdl_seq_GET(seq, i); \ +        TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \          if (!elt) continue; /* can be NULL */ \ -        if (!symtable_visit_expr((ST), elt)) \ +        if (!symtable_visit_ ## TYPE((ST), elt)) \              VISIT_QUIT((ST), 0);             \      } \  } @@ -1138,7 +1135,7 @@ static int  symtable_visit_stmt(struct symtable *st, stmt_ty s)  {      if (++st->recursion_depth > st->recursion_limit) { -        PyErr_SetString(PyExc_RuntimeError, +        PyErr_SetString(PyExc_RecursionError,                          "maximum recursion depth exceeded during compilation");          VISIT_QUIT(st, 0);      } @@ -1149,9 +1146,9 @@ symtable_visit_stmt(struct symtable *st, stmt_ty s)          if (s->v.FunctionDef.args->defaults)              VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults);          if (s->v.FunctionDef.args->kw_defaults) -            VISIT_KWONLYDEFAULTS(st, -                               s->v.FunctionDef.args->kw_defaults); -        if (!symtable_visit_annotations(st, s)) +            VISIT_SEQ_WITH_NULL(st, expr, s->v.FunctionDef.args->kw_defaults); +        if (!symtable_visit_annotations(st, s, s->v.FunctionDef.args, +                                        s->v.FunctionDef.returns))              VISIT_QUIT(st, 0);          if (s->v.FunctionDef.decorator_list)              VISIT_SEQ(st, expr, s->v.FunctionDef.decorator_list); @@ -1170,10 +1167,6 @@ symtable_visit_stmt(struct symtable *st, stmt_ty s)              VISIT_QUIT(st, 0);          VISIT_SEQ(st, expr, s->v.ClassDef.bases);          VISIT_SEQ(st, keyword, s->v.ClassDef.keywords); -        if (s->v.ClassDef.starargs) -            VISIT(st, expr, s->v.ClassDef.starargs); -        if (s->v.ClassDef.kwargs) -            VISIT(st, expr, s->v.ClassDef.kwargs);          if (s->v.ClassDef.decorator_list)              VISIT_SEQ(st, expr, s->v.ClassDef.decorator_list);          if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock, @@ -1245,21 +1238,9 @@ symtable_visit_stmt(struct symtable *st, stmt_ty s)          break;      case Import_kind:          VISIT_SEQ(st, alias, s->v.Import.names); -        /* XXX Don't have the lineno available inside -           visit_alias */ -        if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno) { -            st->st_cur->ste_opt_lineno = s->lineno; -            st->st_cur->ste_opt_col_offset = s->col_offset; -        }          break;      case ImportFrom_kind:          VISIT_SEQ(st, alias, s->v.ImportFrom.names); -        /* XXX Don't have the lineno available inside -           visit_alias */ -        if (st->st_cur->ste_unoptimized && !st->st_cur->ste_opt_lineno) { -            st->st_cur->ste_opt_lineno = s->lineno; -            st->st_cur->ste_opt_col_offset = s->col_offset; -        }          break;      case Global_kind: {          int i; @@ -1335,6 +1316,39 @@ symtable_visit_stmt(struct symtable *st, stmt_ty s)          VISIT_SEQ(st, withitem, s->v.With.items);          VISIT_SEQ(st, stmt, s->v.With.body);          break; +    case AsyncFunctionDef_kind: +        if (!symtable_add_def(st, s->v.AsyncFunctionDef.name, DEF_LOCAL)) +            VISIT_QUIT(st, 0); +        if (s->v.AsyncFunctionDef.args->defaults) +            VISIT_SEQ(st, expr, s->v.AsyncFunctionDef.args->defaults); +        if (s->v.AsyncFunctionDef.args->kw_defaults) +            VISIT_SEQ_WITH_NULL(st, expr, +                                s->v.AsyncFunctionDef.args->kw_defaults); +        if (!symtable_visit_annotations(st, s, s->v.AsyncFunctionDef.args, +                                        s->v.AsyncFunctionDef.returns)) +            VISIT_QUIT(st, 0); +        if (s->v.AsyncFunctionDef.decorator_list) +            VISIT_SEQ(st, expr, s->v.AsyncFunctionDef.decorator_list); +        if (!symtable_enter_block(st, s->v.AsyncFunctionDef.name, +                                  FunctionBlock, (void *)s, s->lineno, +                                  s->col_offset)) +            VISIT_QUIT(st, 0); +        VISIT(st, arguments, s->v.AsyncFunctionDef.args); +        VISIT_SEQ(st, stmt, s->v.AsyncFunctionDef.body); +        if (!symtable_exit_block(st, s)) +            VISIT_QUIT(st, 0); +        break; +    case AsyncWith_kind: +        VISIT_SEQ(st, withitem, s->v.AsyncWith.items); +        VISIT_SEQ(st, stmt, s->v.AsyncWith.body); +        break; +    case AsyncFor_kind: +        VISIT(st, expr, s->v.AsyncFor.target); +        VISIT(st, expr, s->v.AsyncFor.iter); +        VISIT_SEQ(st, stmt, s->v.AsyncFor.body); +        if (s->v.AsyncFor.orelse) +            VISIT_SEQ(st, stmt, s->v.AsyncFor.orelse); +        break;      }      VISIT_QUIT(st, 1);  } @@ -1343,7 +1357,7 @@ static int  symtable_visit_expr(struct symtable *st, expr_ty e)  {      if (++st->recursion_depth > st->recursion_limit) { -        PyErr_SetString(PyExc_RuntimeError, +        PyErr_SetString(PyExc_RecursionError,                          "maximum recursion depth exceeded during compilation");          VISIT_QUIT(st, 0);      } @@ -1364,8 +1378,7 @@ symtable_visit_expr(struct symtable *st, expr_ty e)          if (e->v.Lambda.args->defaults)              VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);          if (e->v.Lambda.args->kw_defaults) -            VISIT_KWONLYDEFAULTS(st, -                                 e->v.Lambda.args->kw_defaults); +            VISIT_SEQ_WITH_NULL(st, expr, e->v.Lambda.args->kw_defaults);          if (!symtable_enter_block(st, lambda,                                    FunctionBlock, (void *)e, e->lineno,                                    e->col_offset)) @@ -1382,7 +1395,7 @@ symtable_visit_expr(struct symtable *st, expr_ty e)          VISIT(st, expr, e->v.IfExp.orelse);          break;      case Dict_kind: -        VISIT_SEQ(st, expr, e->v.Dict.keys); +        VISIT_SEQ_WITH_NULL(st, expr, e->v.Dict.keys);          VISIT_SEQ(st, expr, e->v.Dict.values);          break;      case Set_kind: @@ -1413,6 +1426,10 @@ symtable_visit_expr(struct symtable *st, expr_ty e)          VISIT(st, expr, e->v.YieldFrom.value);          st->st_cur->ste_generator = 1;          break; +    case Await_kind: +        VISIT(st, expr, e->v.Await.value); +        st->st_cur->ste_generator = 1; +        break;      case Compare_kind:          VISIT(st, expr, e->v.Compare.left);          VISIT_SEQ(st, expr, e->v.Compare.comparators); @@ -1420,11 +1437,7 @@ symtable_visit_expr(struct symtable *st, expr_ty e)      case Call_kind:          VISIT(st, expr, e->v.Call.func);          VISIT_SEQ(st, expr, e->v.Call.args); -        VISIT_SEQ(st, keyword, e->v.Call.keywords); -        if (e->v.Call.starargs) -            VISIT(st, expr, e->v.Call.starargs); -        if (e->v.Call.kwargs) -            VISIT(st, expr, e->v.Call.kwargs); +        VISIT_SEQ_WITH_NULL(st, keyword, e->v.Call.keywords);          break;      case Num_kind:      case Str_kind: @@ -1517,10 +1530,9 @@ symtable_visit_argannotations(struct symtable *st, asdl_seq *args)  }  static int -symtable_visit_annotations(struct symtable *st, stmt_ty s) +symtable_visit_annotations(struct symtable *st, stmt_ty s, +                           arguments_ty a, expr_ty returns)  { -    arguments_ty a = s->v.FunctionDef.args; -      if (a->args && !symtable_visit_argannotations(st, a->args))          return 0;      if (a->vararg && a->vararg->annotation) @@ -1529,8 +1541,8 @@ symtable_visit_annotations(struct symtable *st, stmt_ty s)          VISIT(st, expr, a->kwarg->annotation);      if (a->kwonlyargs && !symtable_visit_argannotations(st, a->kwonlyargs))          return 0; -    if (s->v.FunctionDef.returns) -        VISIT(st, expr, s->v.FunctionDef.returns); +    if (returns) +        VISIT(st, expr, returns);      return 1;  } @@ -1615,7 +1627,6 @@ symtable_visit_alias(struct symtable *st, alias_ty a)              Py_DECREF(store_name);              return 0;          } -        st->st_cur->ste_unoptimized |= OPT_IMPORT_STAR;          Py_DECREF(store_name);          return 1;      } | 
