Merge lp:~danken/python-fixtures/staticmethod into lp:~python-fixtures/python-fixtures/trunk

Proposed by Dan Kenigsberg
Status: Merged
Merged at revision: 76
Proposed branch: lp:~danken/python-fixtures/staticmethod
Merge into: lp:~python-fixtures/python-fixtures/trunk
Diff against target: 55 lines (+23/-0)
2 files modified
lib/fixtures/_fixtures/monkeypatch.py (+8/-0)
lib/fixtures/tests/_fixtures/test_monkeypatch.py (+15/-0)
To merge this branch: bzr merge lp:~danken/python-fixtures/staticmethod
Reviewer Review Type Date Requested Status
python-fixtures committers Pending
Review via email: mp+202207@code.launchpad.net

Description of the change

When MonkeyPatch() is applied to a staticmethod of a class object, it does not restore it into its former state, due to an arcane Python 2 behaviour. Please review the suggested fix to the problem.

To post a comment you must log in.
Revision history for this message
Robert Collins (lifeless) wrote :

thank you for the fix, sorry I didn't notice the pending merge till now!

Revision history for this message
Robert Collins (lifeless) wrote :

And merged - thanks again!

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'lib/fixtures/_fixtures/monkeypatch.py'
--- lib/fixtures/_fixtures/monkeypatch.py 2012-07-04 17:51:59 +0000
+++ lib/fixtures/_fixtures/monkeypatch.py 2014-01-19 02:22:26 +0000
@@ -17,6 +17,9 @@
17 'MonkeyPatch'17 'MonkeyPatch'
18 ]18 ]
1919
20import sys
21import types
22
20from fixtures import Fixture23from fixtures import Fixture
2124
2225
@@ -62,6 +65,11 @@
62 if old_value is sentinel:65 if old_value is sentinel:
63 self.addCleanup(self._safe_delete, current, attribute)66 self.addCleanup(self._safe_delete, current, attribute)
64 else:67 else:
68 # Python 2's setattr transforms function into instancemethod
69 if (sys.version_info.major == 2 and
70 isinstance(current, (type, types.ClassType)) and
71 isinstance(old_value, types.FunctionType)):
72 old_value = staticmethod(old_value)
65 self.addCleanup(setattr, current, attribute, old_value)73 self.addCleanup(setattr, current, attribute, old_value)
6674
67 def _safe_delete(self, obj, attribute):75 def _safe_delete(self, obj, attribute):
6876
=== modified file 'lib/fixtures/tests/_fixtures/test_monkeypatch.py'
--- lib/fixtures/tests/_fixtures/test_monkeypatch.py 2012-07-04 17:51:59 +0000
+++ lib/fixtures/tests/_fixtures/test_monkeypatch.py 2014-01-19 02:22:26 +0000
@@ -19,6 +19,11 @@
1919
20reference = 2320reference = 23
2121
22class C(object):
23 @staticmethod
24 def foo(): pass
25def bar(): pass
26
22class TestMonkeyPatch(testtools.TestCase, TestWithFixtures):27class TestMonkeyPatch(testtools.TestCase, TestWithFixtures):
2328
24 def test_patch_and_restore(self):29 def test_patch_and_restore(self):
@@ -66,3 +71,13 @@
66 finally:71 finally:
67 fixture.cleanUp()72 fixture.cleanUp()
68 self.assertFalse('new_attr' in globals())73 self.assertFalse('new_attr' in globals())
74
75 def test_patch_staticmethod(self):
76 oldfoo = C.foo
77 fixture = MonkeyPatch(
78 'fixtures.tests._fixtures.test_monkeypatch.C.foo',
79 bar)
80 with fixture:
81 pass
82 self.assertEqual(oldfoo, C.foo)
83

Subscribers

People subscribed via source and target branches