From 44c1f801a3068647673c7e827827bab7cc72264c Mon Sep 17 00:00:00 2001 From: Duncan McGreggor Date: Fri, 13 Feb 2009 05:49:02 -0600 Subject: - Added a new unit test to check for a bug in mocker where the original attribute gets swallowed by a subsequent exception check. - Added a fix for the bug. --- mocker.py | 3 ++- test.py | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/mocker.py b/mocker.py index 3b24111..a8904f3 100644 --- a/mocker.py +++ b/mocker.py @@ -2020,6 +2020,7 @@ class Patcher(Task): try: return unpatched(*action.args, **action.kwargs) except AttributeError: + (type, value, traceback) = sys.exc_info() if action.kind == "getattr": # The normal behavior of Python is to try __getattribute__, # and if it raises AttributeError, try __getattr__. We've @@ -2031,7 +2032,7 @@ class Patcher(Task): pass else: return __getattr__(*action.args, **action.kwargs) - raise + raise type, value, traceback class PatchedMethod(object): diff --git a/test.py b/test.py index e98acb8..ec9578f 100755 --- a/test.py +++ b/test.py @@ -3983,6 +3983,20 @@ class PatcherTest(TestCase): self.assertEquals(obj.method(), "original") self.assertRaises(AssertionError, obj.method) + def test_original_exception_raised(self): + class MyClass(object): + def non_existing_attribute(self): + return self.bad_attribute + + mock_class = self.mocker.patch(MyClass) + mock_class.run() + self.mocker.replay() + my_class = MyClass() + try: + my_class.non_existing_attribute() + except AttributeError, error: + message = "'MyClass' object has no attribute 'bad_attribute'" + self.assertEquals(message, error.message) def main(): try: -- cgit v1.2.1