summaryrefslogtreecommitdiff
path: root/Python/symtable.c
diff options
context:
space:
mode:
Diffstat (limited to 'Python/symtable.c')
-rw-r--r--Python/symtable.c123
1 files changed, 71 insertions, 52 deletions
diff --git a/Python/symtable.c b/Python/symtable.c
index 951267342b..1591a20f50 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;
@@ -371,15 +368,20 @@ error_at_directive(PySTEntryObject *ste, PyObject *name)
Py_ssize_t i;
PyObject *data;
assert(ste->ste_directives);
- for (i = 0; ; i++) {
+ for (i = 0; i < PyList_GET_SIZE(ste->ste_directives); i++) {
data = PyList_GET_ITEM(ste->ste_directives, i);
assert(PyTuple_CheckExact(data));
- if (PyTuple_GET_ITEM(data, 0) == name)
- break;
+ assert(PyUnicode_CheckExact(PyTuple_GET_ITEM(data, 0)));
+ if (PyUnicode_Compare(PyTuple_GET_ITEM(data, 0), name) == 0) {
+ PyErr_SyntaxLocationObject(ste->ste_table->st_filename,
+ PyLong_AsLong(PyTuple_GET_ITEM(data, 1)),
+ PyLong_AsLong(PyTuple_GET_ITEM(data, 2)));
+
+ return 0;
+ }
}
- PyErr_SyntaxLocationObject(ste->ste_table->st_filename,
- PyLong_AsLong(PyTuple_GET_ITEM(data, 1)),
- PyLong_AsLong(PyTuple_GET_ITEM(data, 2)));
+ PyErr_SetString(PyExc_RuntimeError,
+ "BUG: internal directive bookkeeping broken");
return 0;
}
@@ -852,7 +854,7 @@ analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
/* Copy the bound and global dictionaries.
- These dictionary are used by all blocks enclosed by the
+ These dictionaries are used by all blocks enclosed by the
current block. The analyze_block() call modifies these
dictionaries.
@@ -1086,13 +1088,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); \
} \
}
@@ -1118,14 +1120,17 @@ symtable_new_tmpname(struct symtable *st)
static int
symtable_record_directive(struct symtable *st, identifier name, stmt_ty s)
{
- PyObject *data;
+ PyObject *data, *mangled;
int res;
if (!st->st_cur->ste_directives) {
st->st_cur->ste_directives = PyList_New(0);
if (!st->st_cur->ste_directives)
return 0;
}
- data = Py_BuildValue("(Oii)", name, s->lineno, s->col_offset);
+ mangled = _Py_Mangle(st->st_private, name);
+ if (!mangled)
+ return 0;
+ data = Py_BuildValue("(Nii)", mangled, s->lineno, s->col_offset);
if (!data)
return 0;
res = PyList_Append(st->st_cur->ste_directives, data);
@@ -1138,7 +1143,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 +1154,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 +1175,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 +1246,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 +1324,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 +1365,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 +1386,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 +1403,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 +1434,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 +1445,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 +1538,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 +1549,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 +1635,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;
}