From ce6a070414ed1e1374d1e6212bfbff61b6d5d755 Mon Sep 17 00:00:00 2001 From: Zackery Spytz Date: Sun, 25 Aug 2019 03:44:09 -0600 Subject: bpo-34880: Add the LOAD_ASSERTION_ERROR opcode. (GH-15073) Fix assert statement misbehavior if AssertionError is shadowed. --- Python/compile.c | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) (limited to 'Python/compile.c') diff --git a/Python/compile.c b/Python/compile.c index 1bf05e29130..edd8625cba9 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -1129,6 +1129,8 @@ stack_effect(int opcode, int oparg, int jump) return (oparg & FVS_MASK) == FVS_HAVE_SPEC ? -1 : 0; case LOAD_METHOD: return 1; + case LOAD_ASSERTION_ERROR: + return 1; default: return PY_INVALID_STACK_EFFECT; } @@ -3253,16 +3255,10 @@ compiler_from_import(struct compiler *c, stmt_ty s) static int compiler_assert(struct compiler *c, stmt_ty s) { - static PyObject *assertion_error = NULL; basicblock *end; if (c->c_optimize) return 1; - if (assertion_error == NULL) { - assertion_error = PyUnicode_InternFromString("AssertionError"); - if (assertion_error == NULL) - return 0; - } if (s->v.Assert.test->kind == Tuple_kind && asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) { @@ -3277,7 +3273,7 @@ compiler_assert(struct compiler *c, stmt_ty s) return 0; if (!compiler_jump_if(c, s->v.Assert.test, end, 1)) return 0; - ADDOP_O(c, LOAD_GLOBAL, assertion_error, names); + ADDOP(c, LOAD_ASSERTION_ERROR); if (s->v.Assert.msg) { VISIT(c, expr, s->v.Assert.msg); ADDOP_I(c, CALL_FUNCTION, 1); -- cgit v1.2.3