summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGustavo Niemeyer <gustavo@niemeyer.net>2010-06-20 13:21:07 -0300
committerGustavo Niemeyer <gustavo@niemeyer.net>2010-06-20 13:21:07 -0300
commit1a572f6c05cc6c7aa2b58e810684525cb8b66de7 (patch)
treed7609c0c52dab8a060e070983a60dcba83b43889
parentf7ee6d1b32656d7143b9b02ce1afe0f7fa56dde9 (diff)
downloadmocker-1a572f6c05cc6c7aa2b58e810684525cb8b66de7.tar.gz
Unwrap bound methods on replace() and proxy(), as suggested
by James Henstridge (#270782).
-rw-r--r--NEWS3
-rw-r--r--mocker.py2
-rwxr-xr-xtest.py12
3 files changed, 17 insertions, 0 deletions
diff --git a/NEWS b/NEWS
index 6560f17..2022549 100644
--- a/NEWS
+++ b/NEWS
@@ -4,6 +4,9 @@
- Changed license to BSD, since the PSF license only applies to Python
itself (#583335).
+- Unwrap bound methods on replace() and proxy(), as suggested
+ by James Henstridge (#270782).
+
- 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.
diff --git a/mocker.py b/mocker.py
index 6a030e0..336e696 100644
--- a/mocker.py
+++ b/mocker.py
@@ -627,6 +627,8 @@ class MockerBase(object):
for attr in attr_stack:
object = getattr(object, attr)
break
+ if isinstance(object, types.UnboundMethodType):
+ object = object.im_func
if spec is True:
spec = object
if type is True:
diff --git a/test.py b/test.py
index 44f9efc..21c9d63 100755
--- a/test.py
+++ b/test.py
@@ -210,6 +210,13 @@ class IntegrationTest(TestCase):
self.assertTrue(os.path.isfile("unexistent"))
self.assertFalse(os.path.isfile("unexistent"))
+ def test_replace_class_method(self):
+ empty = self.mocker.replace("Queue.Queue.empty")
+ expect(empty()).result(False)
+ self.mocker.replay()
+ from Queue import Queue
+ self.assertEquals(Queue().empty(), False)
+
def test_patch_with_spec(self):
class C(object):
def method(self, a, b):
@@ -1348,6 +1355,11 @@ class MockerTest(TestCase):
mock = self.mocker.replace(original, passthrough=False)
self.assertEquals(mock.__mocker_passthrough__, False)
+ def test_replace_with_bound_method(self):
+ from Queue import Queue
+ mock = self.mocker.replace(Queue.empty)
+ self.assertEquals(mock.__mocker_object__, Queue.empty.im_func)
+
def test_add_and_get_event(self):
self.mocker.add_event(41)
self.assertEquals(self.mocker.add_event(42), 42)