Ronny Pfannschmidt avatar Ronny Pfannschmidt committed a91703a

update monkeypatch patch

Comments (0)

Files changed (1)

monkeypatch-descriptors

 # HG changeset patch
-# Parent da7434cf50bc85c393c42c1559ced0715f4db5e7
+# Parent d46b4480e6de290d9b965618981f9f186030380f
+fix issue 156 - monkypaych undo broken for staticmethod
+
 diff --git a/_pytest/monkeypatch.py b/_pytest/monkeypatch.py
 --- a/_pytest/monkeypatch.py
 +++ b/_pytest/monkeypatch.py
-@@ -36,7 +36,11 @@ class monkeypatch:
-     def setattr(self, obj, name, value, raising=True):
-         """ set attribute ``name`` on ``obj`` to ``value``, by default
-         raise AttributeEror if the attribute did not exist. """
--        oldval = getattr(obj, name, notset)
-+        if isinstance(obj, type):
-+            # don't cause descriptor invocations on class attributes
-+            oldval = obj.__dict__.get(name, notset)
-+        else:
-+            oldval = getattr(obj, name, notset)
+@@ -39,6 +39,10 @@ class monkeypatch:
+         oldval = getattr(obj, name, notset)
          if raising and oldval is notset:
              raise AttributeError("%r has no attribute %r" %(obj, name))
++
++        vars = getattr(obj, '__dict__', notset)
++        if vars is not notset:
++            oldval = vars.get(name, notset)
          self._setattr.insert(0, (obj, name, oldval))
+         setattr(obj, name, value)
+ 
 diff --git a/testing/test_monkeypatch.py b/testing/test_monkeypatch.py
 --- a/testing/test_monkeypatch.py
 +++ b/testing/test_monkeypatch.py
-@@ -206,3 +206,17 @@ def test_issue185_time_breaks(testdir):
+@@ -206,3 +206,38 @@ def test_issue185_time_breaks(testdir):
      result.stdout.fnmatch_lines("""
          *1 passed*
      """)
 +
 +
-+def test_issue156_undo_staticmethod():
++
++class SampleNew(object):
++    @staticmethod
++    def hello():
++        return True
++
++
++class SampleNewInherit(SampleNew):
++    pass
++
++
++class SampleOld:
++    #oldstyle on python2
++    @staticmethod
++    def hello():
++        return True
++
++class SampleOldInherit(SampleOld):
++    pass
++
++
++@pytest.mark.parametrize('Sample', [
++    SampleNew, SampleNewInherit,
++    SampleOld, SampleOldInherit,
++], ids=['new', 'new-inherit', 'old', 'old-inherit'])
++def test_issue156_undo_staticmethod(Sample):
 +    monkeypatch = MonkeyPatch()
 +
-+    class Sample(object):
-+        @staticmethod
-+        def hello():
-+            return True
 +    monkeypatch.setattr(Sample, 'hello', None)
 +    assert Sample.hello is None
 +
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.