Skip to content

fix fixture for python3 fixes #2334 #2814

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions _pytest/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
)
from _pytest.outcomes import fail, TEST_OUTCOME
from _pytest.compat import FuncargnamesCompatAttr
import types
import functools

if sys.version_info[:2] == (2, 6):
from ordereddict import OrderedDict
Expand Down Expand Up @@ -824,6 +826,17 @@ def pytest_fixture_setup(fixturedef, request):
return result


def copy_func(f):
"""Based on http://stackoverflow.com/a/6528148/190597 (Glenn Maynard)"""
g = types.FunctionType(f.__code__, f.__globals__, name=f.__name__,
argdefs=f.__defaults__,
closure=f.__closure__)
g = functools.update_wrapper(g, f)
g.__kwdefaults__ = f.__kwdefaults__
return g



class FixtureFunctionMarker:
def __init__(self, scope, params, autouse=False, ids=None, name=None):
self.scope = scope
Expand All @@ -835,8 +848,10 @@ def __init__(self, scope, params, autouse=False, ids=None, name=None):
def __call__(self, function):
if isclass(function):
raise ValueError(
"class fixtures not supported (may be in the future)")
function._pytestfixturefunction = self
"class fixtures not supported (may be in the future)")
if getattr(function, "_pytestfixturefunction", False):
function = copy_func(function)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the assigment was reoved, as such this is broken

from my pov we should not copy the function but warn about the mistake reserving the right to fail later on

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that copying the function is dangerous. I think we can target features and raise an error if @pytest.fixture is being applied more than once to the same function.

It is clearly a problem and if it is working is mostly by accident.

return function


Expand Down