From 9fd75500a2d7097c539ea4be23fa9763b02c3d29 Mon Sep 17 00:00:00 2001 From: Gustavo Niemeyer Date: Sat, 19 Jun 2010 22:55:31 -0300 Subject: Fixed support for Python 2.6. Mocking of iterators was broken in certain cases because, even though that's *not* documented, Python tries to use __length_hint__ in some cases. --- test.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) (limited to 'test.py') diff --git a/test.py b/test.py index 6816513..68f3125 100755 --- a/test.py +++ b/test.py @@ -2524,6 +2524,25 @@ class MockTest(TestCase): else: self.fail("AttributeError not raised.") + def test_mock_raises_attribute_error_on_length_hint(self): + """ + In Python 2.6+ list() uses __length_hint__() as a hint. When + we mock iter(), it shouldn't explode due to the lack of + __length_hint__. + """ + def raise_error(path): + raise MatchError("Kaboom!") + + self.mocker.act = raise_error + try: + self.mock.__length_hint__ + except AttributeError, e: + self.assertEquals(str(e), "No __length_hint__ here!") + except MatchError: + self.fail("Expected AttributeError, not MatchError.") + else: + self.fail("AttributeError not raised.") + def test_nonzero(self): self.assertEquals(bool(self.mock), True) # True due to 42. (path,) = self.paths @@ -4000,7 +4019,7 @@ class PatcherTest(TestCase): obj.use_non_existing_attribute() except AttributeError, error: message = "'C' object has no attribute 'bad_attribute'" - self.assertEquals(message, error.message) + self.assertEquals(message, str(error)) def main(): -- cgit v1.2.1