diff options
author | tsukasa-au <tsukasa-au@users.noreply.github.com> | 2021-03-16 11:14:41 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-03-16 11:14:41 (GMT) |
commit | a8ef4572a6b28bcfc0b10b34fa4204954b9dd761 (patch) | |
tree | 59548765f737b26d8e8fa57b49dabad8f57a5365 /Python/compile.c | |
parent | 1330338583d183250186a8123b99d2283e945b4f (diff) | |
download | cpython-a8ef4572a6b28bcfc0b10b34fa4204954b9dd761.zip cpython-a8ef4572a6b28bcfc0b10b34fa4204954b9dd761.tar.gz cpython-a8ef4572a6b28bcfc0b10b34fa4204954b9dd761.tar.bz2 |
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.
Diffstat (limited to 'Python/compile.c')
-rw-r--r-- | Python/compile.c | 12 |
1 files changed, 8 insertions, 4 deletions
diff --git a/Python/compile.c b/Python/compile.c index ea1bf6b..a1260aa 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; |