From 09a9ce1da1634fa03144d444d6115427fd63c506 Mon Sep 17 00:00:00 2001 From: Ronny Pfannschmidt Date: Sat, 9 Oct 2010 07:35:28 +0200 Subject: [PATCH] fix and test a unbound local in _diff_text of the assertion plugin --HG-- branch : trunk --- py/_plugin/pytest_assertion.py | 7 ++++++- testing/plugin/test_pytest_assertion.py | 11 +++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/py/_plugin/pytest_assertion.py b/py/_plugin/pytest_assertion.py index af380369e..b433eb0a9 100644 --- a/py/_plugin/pytest_assertion.py +++ b/py/_plugin/pytest_assertion.py @@ -75,8 +75,12 @@ def pytest_assertrepr_compare(op, left, right): except py.builtin._sysex: raise except: + excinfo = py.code.ExceptionInfo() explanation = ['(pytest_assertion plugin: representation of ' - 'details failed. Probably an object has a faulty __repr__.)'] + 'details failed. Probably an object has a faulty __repr__.)', + str(excinfo) + ] + if not explanation: return None @@ -95,6 +99,7 @@ def _diff_text(left, right): identical to keep the diff minimal. """ explanation = [] + i = 0 # just in case left or right has zero length for i in range(min(len(left), len(right))): if left[i] != right[i]: break diff --git a/testing/plugin/test_pytest_assertion.py b/testing/plugin/test_pytest_assertion.py index 6812abc47..d80d00bb1 100644 --- a/testing/plugin/test_pytest_assertion.py +++ b/testing/plugin/test_pytest_assertion.py @@ -103,6 +103,17 @@ class TestAssert_reprcompare: expl = callequal({}, {'1': A()}) assert 'faulty' in "".join(expl) + def test_one_repr_empty(self): + """ + the faulty empty string repr did trigger + a unbound local error in _diff_text + """ + class A(str): + def __repr__(self): + return '' + expl = callequal(A(), '') + assert not expl + @needsnewassert def test_pytest_assertrepr_compare_integration(testdir): testdir.makepyfile("""