[Python-checkins] cpython: unittest.mock: a mock created by patch with a spec as the list argument will be

michael.foord python-checkins at python.org
Sun Mar 25 20:53:14 CEST 2012


http://hg.python.org/cpython/rev/7a65fcdb85b6
changeset:   75937:7a65fcdb85b6
user:        Michael Foord <michael at voidspace.org.uk>
date:        Sun Mar 25 19:53:18 2012 +0100
summary:
  unittest.mock: a mock created by patch with a spec as the list argument will be callable if __call__ is in the spec

files:
  Lib/unittest/mock.py                    |   9 +++++-
  Lib/unittest/test/testmock/testpatch.py |  20 +++++++++++++
  2 files changed, 28 insertions(+), 1 deletions(-)


diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py
--- a/Lib/unittest/mock.py
+++ b/Lib/unittest/mock.py
@@ -1166,7 +1166,14 @@
             if new_callable is not None:
                 Klass = new_callable
             elif spec is not None or spec_set is not None:
-                if not _callable(spec or spec_set):
+                this_spec = spec
+                if spec_set is not None:
+                    this_spec = spec_set
+                if _is_list(this_spec):
+                    not_callable = '__call__' not in this_spec
+                else:
+                    not_callable = not callable(this_spec)
+                if not_callable:
                     Klass = NonCallableMagicMock
 
             if spec is not None:
diff --git a/Lib/unittest/test/testmock/testpatch.py b/Lib/unittest/test/testmock/testpatch.py
--- a/Lib/unittest/test/testmock/testpatch.py
+++ b/Lib/unittest/test/testmock/testpatch.py
@@ -1742,6 +1742,26 @@
                 p.stop()
 
 
+    def test_callable_spec_as_list(self):
+        spec = ('__call__',)
+        p = patch(MODNAME, spec=spec)
+        m = p.start()
+        try:
+            self.assertTrue(callable(m))
+        finally:
+            p.stop()
+
+
+    def test_not_callable_spec_as_list(self):
+        spec = ('foo', 'bar')
+        p = patch(MODNAME, spec=spec)
+        m = p.start()
+        try:
+            self.assertFalse(callable(m))
+        finally:
+            p.stop()
+
+
 
 if __name__ == '__main__':
     unittest.main()

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list