[Python-checkins] r61406 - in python/trunk: Misc/NEWS Python/compile.c

neal.norwitz python-checkins at python.org
Sat Mar 15 23:03:18 CET 2008


Author: neal.norwitz
Date: Sat Mar 15 23:03:18 2008
New Revision: 61406

Modified:
   python/trunk/Misc/NEWS
   python/trunk/Python/compile.c
Log:
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.


Modified: python/trunk/Misc/NEWS
==============================================================================
--- python/trunk/Misc/NEWS	(original)
+++ python/trunk/Misc/NEWS	Sat Mar 15 23:03:18 2008
@@ -12,6 +12,8 @@
 Core and builtins
 -----------------
 
+- Add a warning when asserting a non-empty tuple which is always true.
+
 - Issue #2179: speed up with statement execution by storing the exit method
   on the stack instead of in a temporary variable (patch by Jeffrey Yaskin)
 

Modified: python/trunk/Python/compile.c
==============================================================================
--- python/trunk/Python/compile.c	(original)
+++ python/trunk/Python/compile.c	Sat Mar 15 23:03:18 2008
@@ -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)


More information about the Python-checkins mailing list