Skip to content
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

Multiple django.setup() calls break other plugins #531

Closed
pakal opened this Issue Oct 27, 2017 · 7 comments

Comments

Projects
None yet
2 participants
@pakal
Copy link

pakal commented Oct 27, 2017

In pytest-django hooks, _setup_django() is called multiple times, in conftest setup and as a a trylast, and it leads to multiple resets of logging config (driven by django LOGGING setting).

Inbetween these calls, other plugins like pytest-capturelog try to setup their own logging handlers, but these get reset. I've monkey patched like below, but I guess _setup_django() could use a flag to prevent its multiple execution.

def pytest_configure():
    # we have to prevent bug where pytest calls django.setup() multiple
    # times, breaking the logging conf (and pytest-capturelog plugin)
    import django.apps
    if django.apps.apps.ready:
        django.setup = lambda: None

@blueyed blueyed added the bug label Oct 27, 2017

@blueyed

This comment has been minimized.

Copy link
Contributor

blueyed commented Oct 27, 2017

Thanks for the report.

It would be nice to have a failing test case for pytest-django itself as a first step.

@pakal

This comment has been minimized.

Copy link
Author

pakal commented Oct 30, 2017

Here is a testcase to show the problem : #538

I tested it against python2.7 and Django==1.11.6, sorry I couldn't use the whole test suite or tox locally since there are lots of breakages on windows (something to do with file locking on DB removal).

Basically django.setup() should be idempotent, but in the meantime a workaround can be added here (I talk with django devs about this evolution).

@pakal

This comment has been minimized.

Copy link
Author

pakal commented Oct 30, 2017

For reference, the ticket on Django side : https://code.djangoproject.com/ticket/28752#ticket

@blueyed

This comment has been minimized.

Copy link
Contributor

blueyed commented Oct 30, 2017

@pakal

Would it be OK to make django.setup() idempotent, or even expose a "is_ready" flag for easier introspection ?

There is django.conf.settings.configured for this I think?!

@pakal

This comment has been minimized.

Copy link
Author

pakal commented Oct 30, 2017

Is it the same thing ?

It looks like "settings.configured" is to say that a source of settings has been specified, not that django.setup() has been called, but it's fuzzy for me : https://docs.djangoproject.com/en/1.11/topics/settings/#either-configure-or-django-settings-module-is-required

@blueyed

This comment has been minimized.

Copy link
Contributor

blueyed commented Oct 30, 2017

I am not sure, but it came to my mind - IIRC pytest-django has checks for this at least.. haven't looked at the code, nor do I remember it apparently good enough, sorry.

/cc @pelme

@blueyed

This comment has been minimized.

Copy link
Contributor

blueyed commented Jul 26, 2018

Please test/review #629.

blueyed added a commit to blueyed/pytest-django that referenced this issue Jul 26, 2018

blueyed added a commit to blueyed/pytest-django that referenced this issue Jul 30, 2018

@blueyed blueyed closed this in #629 Aug 5, 2018

blueyed added a commit that referenced this issue Aug 5, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.