From a8ef4572a6b28bcfc0b10b34fa4204954b9dd761 Mon Sep 17 00:00:00 2001 From: tsukasa-au Date: Tue, 16 Mar 2021 22:14:41 +1100 Subject: bpo-43497: Emit SyntaxWarnings for assertions with tuple constants. (GH-24867) * bpo-43497: Emit SyntaxWarnings for assertions with tuple constants. Add a test that shows that a tuple constant (a tuple, where all of its members are also compile-time constants) produces a SyntaxWarning. Then fix this failure. * Make SyntaxWarnings also work when "optimized". * Split tests for SyntaxWarning to SyntaxError conversion SyntaxWarnings emitted by the compiler when configured to be errors are actually raised as SyntaxError exceptions. Move these tests into their own method and add a test to ensure they are raised. Previously we only tested that they were not raised for a "valid" assertion statement. --- Python/compile.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'Python/compile.c') diff --git a/Python/compile.c b/Python/compile.c index ea1bf6bf923..a1260aadd62 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -3355,10 +3355,12 @@ compiler_assert(struct compiler *c, stmt_ty s) { basicblock *end; - if (c->c_optimize) - return 1; - if (s->v.Assert.test->kind == Tuple_kind && - asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) + /* Always emit a warning if the test is a non-zero length tuple */ + if ((s->v.Assert.test->kind == Tuple_kind && + asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) || + (s->v.Assert.test->kind == Constant_kind && + PyTuple_Check(s->v.Assert.test->v.Constant.value) && + PyTuple_Size(s->v.Assert.test->v.Constant.value) > 0)) { if (!compiler_warn(c, "assertion is always true, " "perhaps remove parentheses?")) @@ -3366,6 +3368,8 @@ compiler_assert(struct compiler *c, stmt_ty s) return 0; } } + if (c->c_optimize) + return 1; end = compiler_new_block(c); if (end == NULL) return 0; -- cgit v1.2.3