]> granicus.if.org Git - python/commitdiff
Add a warning for code like:
authorNeal Norwitz <nnorwitz@gmail.com>
Sat, 15 Mar 2008 22:03:18 +0000 (22:03 +0000)
committerNeal Norwitz <nnorwitz@gmail.com>
Sat, 15 Mar 2008 22:03:18 +0000 (22:03 +0000)
  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.

Misc/NEWS
Python/compile.c

index e778b8762d92eecdaaec4df0def6bbfb4a8fe250..ccb7baabcb82e0a46908d57d7b6cc9d8f2c42e13 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,8 @@ What's New in Python 2.6 alpha 2?
 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)
 
index 43b7569e62c0af16ed0a41d33ee59798e14c26f3..47a63e73151cd36deeb1631db78a194681e76527 100644 (file)
@@ -2056,6 +2056,14 @@ compiler_assert(struct compiler *c, stmt_ty s)
                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)