From 0bb84abca7d933b712afab8a5b11d379c1e74571 Mon Sep 17 00:00:00 2001 From: Benjamin Peterson Date: Tue, 24 May 2011 18:15:08 -0500 Subject: [PATCH] handle comparison results which raise when asked for their truth value --- _pytest/assertrewrite.py | 6 +++++- testing/test_assertrewrite.py | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/_pytest/assertrewrite.py b/_pytest/assertrewrite.py index ad4f48f1a..04d20dd5c 100644 --- a/_pytest/assertrewrite.py +++ b/_pytest/assertrewrite.py @@ -25,7 +25,11 @@ def _format_boolop(operands, explanations, is_or): def _call_reprcompare(ops, results, expls, each_obj): for i, res, expl in zip(range(len(ops)), results, expls): - if not res: + try: + done = not res + except Exception: + done = True + if done: break if py.code._reprcompare is not None: custom = py.code._reprcompare(ops[i], each_obj[i], each_obj[i + 1]) diff --git a/testing/test_assertrewrite.py b/testing/test_assertrewrite.py index 4e478d6d6..df6620d11 100644 --- a/testing/test_assertrewrite.py +++ b/testing/test_assertrewrite.py @@ -239,3 +239,17 @@ class TestAssertionRewrite: def f(): assert 1 < 3 < 5 <= 4 < 7 assert getmsg(f) == "assert 5 <= 4" + + def test_assert_raising_nonzero_in_comparison(self): + def f(): + class A(object): + def __nonzero__(self): + raise ValueError(42) + def __lt__(self, other): + return A() + def __repr__(self): + return "" + def myany(x): + return False + assert myany(A() < 0) + assert " < 0" in getmsg(f)