[1.4.x] Fixed #20636 -- Stopped stuffing values in the settings.

In Django < 1.6, override_settings restores the settings module that was
active when the override_settings call was executed, not when it was
run. This can make a difference when override_settings is applied to a
class, since it's executed when the module is imported, not when the
test case is run.

In addition, if the settings module for tests is stored alongside the
tests themselves, importing the settings module can trigger an import
of the tests. Since the settings module isn't fully imported yet,
class-level override_settings statements may store a reference to an
incorrect settings module. Eventually this will result in a crash during
test teardown because the settings module restored by override_settings
won't the one that was active during test setup.

While Django should prevent this situation in the future by failing
loudly in such dubious import sequences, that change won't be backported
to 1.5 and 1.4. However, these versions received the "allowed hosts"
patch and they're prone to "AttributeError: 'Settings' object has no
attribute '_original_allowed_hosts'". To mitigate this regression, this
commits stuffs _original_allowed_hosts on a random module instead of the
settings module.

This problem shouldn't occur in Django 1.6, see #20290, but this patch
will be forward-ported for extra safety.

Also tweaked backup variable names for consistency.

Backport of 0261922 from stable/1.5.x.

Conflicts:
	django/test/utils.py
This commit is contained in:
Aymeric Augustin 2013-06-24 19:48:23 +02:00
parent c97cc85b74
commit e3b6fed320
1 changed files with 13 additions and 9 deletions

View File

@ -4,6 +4,7 @@ import warnings
from django.conf import settings, UserSettingsHolder from django.conf import settings, UserSettingsHolder
from django.core import mail from django.core import mail
from django.test.signals import template_rendered, setting_changed from django.test.signals import template_rendered, setting_changed
from django.http import request
from django.template import Template, loader, TemplateDoesNotExist from django.template import Template, loader, TemplateDoesNotExist
from django.template.loaders import cached from django.template.loaders import cached
from django.utils.translation import deactivate from django.utils.translation import deactivate
@ -69,13 +70,16 @@ def setup_test_environment():
- Set the email backend to the locmem email backend. - Set the email backend to the locmem email backend.
- Setting the active locale to match the LANGUAGE_CODE setting. - Setting the active locale to match the LANGUAGE_CODE setting.
""" """
Template.original_render = Template._render Template._original_render = Template._render
Template._render = instrumented_test_render Template._render = instrumented_test_render
mail.original_email_backend = settings.EMAIL_BACKEND # Storing previous values in the settings module itself is problematic.
# Store them in arbitrary (but related) modules instead. See #20636.
mail._original_email_backend = settings.EMAIL_BACKEND
settings.EMAIL_BACKEND = 'django.core.mail.backends.locmem.EmailBackend' settings.EMAIL_BACKEND = 'django.core.mail.backends.locmem.EmailBackend'
settings._original_allowed_hosts = settings.ALLOWED_HOSTS request._original_allowed_hosts = settings.ALLOWED_HOSTS
settings.ALLOWED_HOSTS = ['*'] settings.ALLOWED_HOSTS = ['*']
mail.outbox = [] mail.outbox = []
@ -90,14 +94,14 @@ def teardown_test_environment():
- Restoring the email sending functions - Restoring the email sending functions
""" """
Template._render = Template.original_render Template._render = Template._original_render
del Template.original_render del Template._original_render
settings.EMAIL_BACKEND = mail.original_email_backend settings.EMAIL_BACKEND = mail._original_email_backend
del mail.original_email_backend del mail._original_email_backend
settings.ALLOWED_HOSTS = settings._original_allowed_hosts settings.ALLOWED_HOSTS = request._original_allowed_hosts
del settings._original_allowed_hosts del request._original_allowed_hosts
del mail.outbox del mail.outbox