diff --git a/Python/compile.c b/Python/compile.c index a992e4b465..fb93c78911 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -1691,7 +1691,8 @@ compiler_body(struct compiler *c, asdl_seq *stmts) c->u->u_lineno = st->lineno; } /* Every annotated class and module should have __annotations__. */ - if (find_ann(stmts)) { + /* if not -OO mode, setup annotations */ + if (find_ann(stmts) && c->c_optimize < 2) { ADDOP(c, SETUP_ANNOTATIONS); } if (!asdl_seq_LEN(stmts)) @@ -1735,7 +1736,8 @@ compiler_mod(struct compiler *c, mod_ty mod) } break; case Interactive_kind: - if (find_ann(mod->v.Interactive.body)) { + /* if not -OO mode, setup annotations */ + if (find_ann(mod->v.Interactive.body) && c->c_optimize < 2) { ADDOP(c, SETUP_ANNOTATIONS); } c->c_interactive = 1; @@ -2105,7 +2107,12 @@ compiler_function(struct compiler *c, stmt_ty s, int is_async) return 0; } - annotations = compiler_visit_annotations(c, args, returns); + /* if not -OO mode, add annotations */ + if (c->c_optimize < 2) { + annotations = compiler_visit_annotations(c, args, returns); + } else { + annotations = -1; + } if (annotations == 0) { return 0; } @@ -5032,6 +5039,12 @@ compiler_annassign(struct compiler *c, stmt_ty s) VISIT(c, expr, s->v.AnnAssign.value); VISIT(c, expr, targ); } + + /* if -OO mode, skip annotations */ + if (c->c_optimize >= 2) { + return 1; + } + switch (targ->kind) { case Name_kind: /* If we have a simple name in a module or class, store annotation. */