From 0818cd0411508ccf926f92f9164bc744aa5be849 Mon Sep 17 00:00:00 2001 From: Michael Foord Date: Wed, 7 Nov 2012 17:08:38 +0000 Subject: Proper compatibility with functools.wraps --- README.txt | 4 +++- docs/changelog.txt | 6 +----- mock.py | 10 ++++------ tests/testpatch.py | 6 ++++++ 4 files changed, 14 insertions(+), 12 deletions(-) diff --git a/README.txt b/README.txt index b24f5f9..677faf9 100644 --- a/README.txt +++ b/README.txt @@ -171,7 +171,9 @@ the `__init__` method, and on callable objects where it copies the signature of the `__call__` method. The distribution contains tests and documentation. The tests require -`unittest2 `_ to run. +`unittest2 `_ to run on Python 2.5, 2.6 +or 3.1. For Python 2.7 and 3.2 they can be run with +`python -m unittest discover`. Docs from the in-development version of `mock` can be found at `mock.readthedocs.org `_. diff --git a/docs/changelog.txt b/docs/changelog.txt index 91a050d..3ae45d0 100644 --- a/docs/changelog.txt +++ b/docs/changelog.txt @@ -5,16 +5,12 @@ CHANGELOG ========= 2012/11/05 Version 1.0.1 ------------------------ +------------------------ * Functions decorated with `patch` variants have a `__wrapped__` attribute pointing to the original function. This brings compatibility with the default behaviour in Python 3.3 (due to a new feature in `functools.wraps`). -Note that due to changes in `tox`, `mock` is no longer tested with Python 2.4. -The compatibility code has not been removed so it probably still works, but -tests are no longer run. - 2012/10/07 Version 1.0.0 ------------------------ diff --git a/mock.py b/mock.py index cb4c733..2a3f869 100644 --- a/mock.py +++ b/mock.py @@ -9,10 +9,6 @@ # Released subject to the BSD License # Please see http://www.voidspace.org.uk/python/license.shtml -# Scripts maintained at http://www.voidspace.org.uk/python/index.shtml -# Comments, suggestions and bug reports welcome. - - __all__ = ( 'Mock', 'MagicMock', @@ -52,7 +48,8 @@ except ImportError: f.__name__ = original.__name__ f.__doc__ = original.__doc__ f.__module__ = original.__module__ - f.__wrapped__ = original + wrapped = getattr(original, '__wrapped__', original) + f.__wrapped__ = wrapped return f return inner else: @@ -62,7 +59,8 @@ else: def wraps(func): def inner(f): f = original_wraps(func)(f) - f.__wrapped__ = func + wrapped = getattr(func, '__wrapped__', func) + f.__wrapped__ = wrapped return f return inner diff --git a/tests/testpatch.py b/tests/testpatch.py index 2c13b49..8eb719b 100644 --- a/tests/testpatch.py +++ b/tests/testpatch.py @@ -1790,6 +1790,12 @@ class PatchTest(unittest2.TestCase): self.assertIs(decorated.__wrapped__, function) + def test_wrapped_several_times_patch(self): + decorated = patch('sys.modules')(function) + decorated = patch('sys.modules')(decorated) + self.assertIs(decorated.__wrapped__, function) + + def test_wrapped_patch_object(self): decorated = patch.object(sys, 'modules')(function) self.assertIs(decorated.__wrapped__, function) -- cgit v1.2.1