diff options
author | Robert Collins <robertc@robertcollins.net> | 2014-08-22 14:38:38 +1200 |
---|---|---|
committer | Robert Collins <robertc@robertcollins.net> | 2014-08-22 14:38:38 +1200 |
commit | 9ec1b3af612bf93882dda4ce07a6ef4329e1a6f8 (patch) | |
tree | 3dc04a2c49bc2befd04528acb8093ce318cfa4bf | |
parent | 8727fe0376204056557267b13dc073fc571f67f6 (diff) | |
parent | a66dc6828dcaab4751aaa97a7ab5590b8e0be343 (diff) | |
download | fixtures-9ec1b3af612bf93882dda4ce07a6ef4329e1a6f8.tar.gz |
* ``MonkeyPatch`` now preserves ``staticmethod`` functions.
(Dan Kenigsberg)
-rw-r--r-- | NEWS | 6 | ||||
-rw-r--r-- | lib/fixtures/_fixtures/monkeypatch.py | 8 | ||||
-rw-r--r-- | lib/fixtures/tests/_fixtures/test_monkeypatch.py | 15 |
3 files changed, 29 insertions, 0 deletions
@@ -6,6 +6,12 @@ fixtures release notes NEXT ~~~~ +CHANGES +------- + +* ``MonkeyPatch`` now preserves ``staticmethod`` functions. + (Dan Kenigsberg) + 0.3.14 ~~~~~~ diff --git a/lib/fixtures/_fixtures/monkeypatch.py b/lib/fixtures/_fixtures/monkeypatch.py index 42de7ec..858c587 100644 --- a/lib/fixtures/_fixtures/monkeypatch.py +++ b/lib/fixtures/_fixtures/monkeypatch.py @@ -17,6 +17,9 @@ __all__ = [ 'MonkeyPatch' ] +import sys +import types + from fixtures import Fixture @@ -62,6 +65,11 @@ class MonkeyPatch(Fixture): if old_value is sentinel: self.addCleanup(self._safe_delete, current, attribute) else: + # Python 2's setattr transforms function into instancemethod + if (sys.version_info.major == 2 and + isinstance(current, (type, types.ClassType)) and + isinstance(old_value, types.FunctionType)): + old_value = staticmethod(old_value) self.addCleanup(setattr, current, attribute, old_value) def _safe_delete(self, obj, attribute): diff --git a/lib/fixtures/tests/_fixtures/test_monkeypatch.py b/lib/fixtures/tests/_fixtures/test_monkeypatch.py index b5977d3..1a84d7f 100644 --- a/lib/fixtures/tests/_fixtures/test_monkeypatch.py +++ b/lib/fixtures/tests/_fixtures/test_monkeypatch.py @@ -19,6 +19,11 @@ from fixtures import MonkeyPatch, TestWithFixtures reference = 23 +class C(object): + @staticmethod + def foo(): pass +def bar(): pass + class TestMonkeyPatch(testtools.TestCase, TestWithFixtures): def test_patch_and_restore(self): @@ -66,3 +71,13 @@ class TestMonkeyPatch(testtools.TestCase, TestWithFixtures): finally: fixture.cleanUp() self.assertFalse('new_attr' in globals()) + + def test_patch_staticmethod(self): + oldfoo = C.foo + fixture = MonkeyPatch( + 'fixtures.tests._fixtures.test_monkeypatch.C.foo', + bar) + with fixture: + pass + self.assertEqual(oldfoo, C.foo) + |