From a0ce9a444188403cbb56cad0f5cb2789cca09826 Mon Sep 17 00:00:00 2001 From: Ronny Pfannschmidt Date: Thu, 13 Sep 2018 15:38:36 +0200 Subject: [PATCH] remove the legacy code about im_func and generalize using fix and compat.getimfunc --- src/_pytest/python.py | 9 ++++----- src/_pytest/unittest.py | 3 ++- testing/python/metafunc.py | 8 ++------ 3 files changed, 8 insertions(+), 12 deletions(-) diff --git a/src/_pytest/python.py b/src/_pytest/python.py index 051650272..1976be80c 100644 --- a/src/_pytest/python.py +++ b/src/_pytest/python.py @@ -37,6 +37,7 @@ from _pytest.compat import ( getlocation, enum, get_default_arg_names, + getimfunc, ) from _pytest.outcomes import fail from _pytest.mark.structures import ( @@ -681,14 +682,12 @@ class Class(PyCollector): def setup(self): setup_class = _get_xunit_func(self.obj, "setup_class") if setup_class is not None: - setup_class = getattr(setup_class, "im_func", setup_class) - setup_class = getattr(setup_class, "__func__", setup_class) + setup_class = getimfunc(setup_class) setup_class(self.obj) fin_class = getattr(self.obj, "teardown_class", None) if fin_class is not None: - fin_class = getattr(fin_class, "im_func", fin_class) - fin_class = getattr(fin_class, "__func__", fin_class) + fin_class = getimfunc(fin_class) self.addfinalizer(lambda: fin_class(self.obj)) @@ -1433,7 +1432,7 @@ class Function(FunctionMixin, nodes.Item, fixtures.FuncargnamesCompatAttr): @property def function(self): "underlying python 'function' object" - return getattr(self.obj, "im_func", self.obj) + return getimfunc(self.obj) def _getobj(self): name = self.name diff --git a/src/_pytest/unittest.py b/src/_pytest/unittest.py index a135dbd53..a2fd6ad5a 100644 --- a/src/_pytest/unittest.py +++ b/src/_pytest/unittest.py @@ -9,6 +9,7 @@ import _pytest._code from _pytest.config import hookimpl from _pytest.outcomes import fail, skip, xfail from _pytest.python import transfer_markers, Class, Module, Function +from _pytest.compat import getimfunc def pytest_pycollect_makeitem(collector, name, obj): @@ -53,7 +54,7 @@ class UnitTestCase(Class): x = getattr(self.obj, name) if not getattr(x, "__test__", True): continue - funcobj = getattr(x, "im_func", x) + funcobj = getimfunc(x) transfer_markers(funcobj, cls, module) yield TestCaseFunction(name, parent=self, callobj=funcobj) foundsomething = True diff --git a/testing/python/metafunc.py b/testing/python/metafunc.py index 1f5daa9b9..7ff1e2283 100644 --- a/testing/python/metafunc.py +++ b/testing/python/metafunc.py @@ -796,7 +796,7 @@ class TestMetafuncFunctional(object): p = testdir.makepyfile( """ # assumes that generate/provide runs in the same process - import sys, pytest + import sys, pytest, six def pytest_generate_tests(metafunc): metafunc.addcall(param=metafunc) @@ -815,11 +815,7 @@ class TestMetafuncFunctional(object): def test_method(self, metafunc, pytestconfig): assert metafunc.config == pytestconfig assert metafunc.module.__name__ == __name__ - if sys.version_info > (3, 0): - unbound = TestClass.test_method - else: - unbound = TestClass.test_method.im_func - # XXX actually have an unbound test function here? + unbound = six.get_unbound_function(TestClass.test_method) assert metafunc.function == unbound assert metafunc.cls == TestClass """