diff Python/compile.c @ 45771:4e358ed7f515 legacy-trunk

Add a warning for code like: assert (0, 'message') An empty tuple does not create a warning. While questionable usage: assert (), 'message' should not display a warning. Tested manually. The warning message could be improved. Feel free to update it.
author Neal Norwitz <nnorwitz@gmail.com>
date Sat, 15 Mar 2008 22:03:18 +0000
parents 343597c7d682
children ca38f7a4b4c9
line wrap: on
line diff
--- a/Python/compile.c	Sat Mar 15 20:37:50 2008 +0000
+++ b/Python/compile.c	Sat Mar 15 22:03:18 2008 +0000
@@ -2056,6 +2056,14 @@
 		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) {
+		const char* msg =
+			"assertion is always true, perhaps remove parentheses?";
+		if (PyErr_WarnExplicit(PyExc_SyntaxWarning, msg, c->c_filename,
+				       c->u->u_lineno, NULL, NULL) == -1)
+			return 0;
+	}
 	VISIT(c, expr, s->v.Assert.test);
 	end = compiler_new_block(c);
 	if (end == NULL)