From a0cbc060b614fdea1dc6db8ad55d76e20e059bb2 Mon Sep 17 00:00:00 2001 From: hpk Date: Thu, 21 Aug 2008 15:43:45 +0200 Subject: [PATCH] [svn r57554] fix case where argument to AssertionError has broken __repr__ --HG-- branch : trunk --- py/magic/assertion.py | 9 ++++++++- py/magic/testing/test_assertion.py | 8 ++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/py/magic/assertion.py b/py/magic/assertion.py index 518fa06a3..31eb13c95 100644 --- a/py/magic/assertion.py +++ b/py/magic/assertion.py @@ -8,7 +8,14 @@ class AssertionError(BuiltinAssertionError): def __init__(self, *args): BuiltinAssertionError.__init__(self, *args) if args: - self.msg = str(args[0]) + try: + self.msg = str(args[0]) + except (KeyboardInterrupt, SystemExit): + raise + except: + self.msg = "<[broken __repr__] %s at %0xd>" %( + args[0].__class__, id(args[0])) + else: f = sys._getframe(1) try: diff --git a/py/magic/testing/test_assertion.py b/py/magic/testing/test_assertion.py index d2eb0f8fd..b3b337175 100644 --- a/py/magic/testing/test_assertion.py +++ b/py/magic/testing/test_assertion.py @@ -96,3 +96,11 @@ def test_assert_implicit_multiline(): except AssertionError, e: assert e.msg.find('assert [1, 2, 3] !=') != -1 + +def test_assert_with_brokenrepr_arg(): + class BrokenRepr: + def __repr__(self): 0 / 0 + e = AssertionError(BrokenRepr()) + if e.msg.find("broken __repr__") == -1: + py.test.fail("broken __repr__ not handle correctly") +