diff options
author | Gustavo Niemeyer <gustavo@niemeyer.net> | 2010-06-19 22:55:31 -0300 |
---|---|---|
committer | Gustavo Niemeyer <gustavo@niemeyer.net> | 2010-06-19 22:55:31 -0300 |
commit | 9fd75500a2d7097c539ea4be23fa9763b02c3d29 (patch) | |
tree | 9cbfc09accaa90f55069f33b65a5d4ff37933cc0 | |
parent | 88f2b4b1ac94b26bafe2357fbc3129030109780b (diff) | |
download | mocker-9fd75500a2d7097c539ea4be23fa9763b02c3d29.tar.gz |
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.
-rw-r--r-- | mocker.py | 2 | ||||
-rwxr-xr-x | test.py | 21 |
2 files changed, 22 insertions, 1 deletions
@@ -1053,6 +1053,8 @@ class Mock(object): if self.__mocker__.is_recording() or self.__mocker_type__ is None: return type(self) return self.__mocker_type__ + if name == "__length_hint__": + raise AttributeError("No __length_hint__ here!") return self.__mocker_act__("getattr", (name,)) def __setattr__(self, name, value): @@ -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(): |