summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGustavo Niemeyer <gustavo@niemeyer.net>2010-06-19 22:55:31 -0300
committerGustavo Niemeyer <gustavo@niemeyer.net>2010-06-19 22:55:31 -0300
commit9fd75500a2d7097c539ea4be23fa9763b02c3d29 (patch)
tree9cbfc09accaa90f55069f33b65a5d4ff37933cc0
parent88f2b4b1ac94b26bafe2357fbc3129030109780b (diff)
downloadmocker-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.py2
-rwxr-xr-xtest.py21
2 files changed, 22 insertions, 1 deletions
diff --git a/mocker.py b/mocker.py
index a49675f..eb03bac 100644
--- a/mocker.py
+++ b/mocker.py
@@ -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):
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():