Commits

Ronny Pfannschmidt  committed 6fc942d

monkeypatch descriptor patch

  • Participants
  • Parent commits 1aabf10

Comments (0)

Files changed (3)

File kill-memoizedcall

-# HG changeset patch
-# Parent 3a876497815d887f3c13589aa81b238e8bee3b1e
-diff --git a/_pytest/main.py b/_pytest/main.py
---- a/_pytest/main.py
-+++ b/_pytest/main.py
-@@ -251,24 +251,6 @@ class Node(object):
-     def teardown(self):
-         pass
- 
--    def _memoizedcall(self, attrname, function):
--        exattrname = "_ex_" + attrname
--        failure = getattr(self, exattrname, None)
--        if failure is not None:
--            py.builtin._reraise(failure[0], failure[1], failure[2])
--        if hasattr(self, attrname):
--            return getattr(self, attrname)
--        try:
--            res = function()
--        except py.builtin._sysex:
--            raise
--        except:
--            failure = py.std.sys.exc_info()
--            setattr(self, exattrname, failure)
--            raise
--        setattr(self, attrname, res)
--        return res
--
-     def listchain(self):
-         """ return list of all parent collectors up to self,
-             starting from root of collection tree. """
-@@ -345,10 +327,6 @@ class Collector(Node):
-             return str(exc.args[0])
-         return self._repr_failure_py(excinfo, style="short")
- 
--    def _memocollect(self):
--        """ internal helper method to cache results of calling collect(). """
--        return self._memoizedcall('_collected', lambda: list(self.collect()))
--
-     def _prunetraceback(self, excinfo):
-         if hasattr(self, 'fspath'):
-             path = self.fspath
-diff --git a/_pytest/python.py b/_pytest/python.py
---- a/_pytest/python.py
-+++ b/_pytest/python.py
-@@ -332,8 +332,12 @@ def transfer_markers(funcobj, cls, mod):
- 
- class Module(pytest.File, PyCollector):
-     """ Collector for test classes and functions. """
-+    _obj = None
-+
-     def _getobj(self):
--        return self._memoizedcall('_obj', self._importtestmodule)
-+        if self._obj is None:
-+            self._obj = self._importtestmodule()
-+        return _obj
- 
-     def collect(self):
-         self.session.funcargmanager._parsefactories(self.obj, self.nodeid)
-diff --git a/_pytest/runner.py b/_pytest/runner.py
---- a/_pytest/runner.py
-+++ b/_pytest/runner.py
-@@ -244,7 +244,7 @@ class TeardownErrorReport(BaseReport):
-         self.__dict__.update(extra)
- 
- def pytest_make_collect_report(collector):
--    call = CallInfo(collector._memocollect, "memocollect")
-+    call = CallInfo(lambda: list(collector.collect()), "collect")
-     longrepr = None
-     if not call.excinfo:
-         outcome = "passed"

File monkeypatch-descriptors

+# HG changeset patch
+# Parent da7434cf50bc85c393c42c1559ced0715f4db5e7
+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)
+         if raising and oldval is notset:
+             raise AttributeError("%r has no attribute %r" %(obj, name))
+         self._setattr.insert(0, (obj, name, oldval))
+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):
+     result.stdout.fnmatch_lines("""
+         *1 passed*
+     """)
++
++
++def test_issue156_undo_staticmethod():
++    monkeypatch = MonkeyPatch()
++
++    class Sample(object):
++        @staticmethod
++        def hello():
++            return True
++    monkeypatch.setattr(Sample, 'hello', None)
++    assert Sample.hello is None
++
++    monkeypatch.undo()
++    assert Sample.hello()
-kill-memoizedcall
-issue187
+monkeypatch-descriptors
+issue187 #+issue187
 parametrize
 handle-usageerror
 asser-reloader-package