#28752: django.setup() should not be runnable multiple times
--------------------------------+--------------------------------------
Reporter: pascal chambon | Owner: nobody
Type: Bug | Status: new
Component: Core (Other) | Version: 1.11
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------+--------------------------------------
Comment (by pascal chambon):
Well, I'm usually quite dedicated to retrocompatibility (see django-
compat-patcher package), but for once any breakage would be due to a
strange misuse of django.setup().
In the code below, we see that django.setup() performs 3 steps :
- overridding logging configuration with django settings
- setting the script prefix
- populating the apps registry
The last 2 steps are now idempotent it seems.
Only overridding logging breaks some setups (eg. with pytest fixtures),
and I can't find any use case where it would be a wanted behaviour.
If users want to reset logging several times, they may as well call
configure_logging() by themselves.
Until django.setup() is protected against double executions, we'll have
weird bugs surfacing each time we add new steps to it ''(are these
idempotent, or reentrant, or runnable multiple times...)'', so I'd
advocate fixing this once and for all.
On "how" to do it, I think a threading lock + a boolean guard would be
easy and sufficient, wouldn't they ? With a system to raise an error if
django.setup() ends up being called multiple times by the same thread
(which often smells like missing "if __name__ == '__main'__" conditions in
imported scripts).
{{{
configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
if set_prefix:
set_script_prefix(
'/' if settings.FORCE_SCRIPT_NAME is None else
settings.FORCE_SCRIPT_NAME
)
apps.populate(settings.INSTALLED_APPS)
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/28752#comment:13>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/063.00205178636b3654314e006480dbea93%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.