summaryrefslogtreecommitdiff
path: root/django/apps/cache.py
diff options
context:
space:
mode:
authorAymeric Augustin <aymeric.augustin@m4x.org>2013-12-24 00:07:54 +0100
committerAymeric Augustin <aymeric.augustin@m4x.org>2013-12-24 00:12:09 +0100
commit2ec8e3443bc9345d986710244e05a3cce56c8da1 (patch)
tree6d30cd176a6d9f8fd9f498c6410829b33bba8902 /django/apps/cache.py
parent7577d03809849d3d878d0ee57ec9d6a8524b5b32 (diff)
Fixed override_settings when set_available_apps raises an exception.
Previously, this would corrupt the settings, because __exit__ isn't called when __enter__raises an exception.
Diffstat (limited to 'django/apps/cache.py')
-rw-r--r--django/apps/cache.py25
1 files changed, 14 insertions, 11 deletions
diff --git a/django/apps/cache.py b/django/apps/cache.py
index 3a95ea1306..840b9fbf68 100644
--- a/django/apps/cache.py
+++ b/django/apps/cache.py
@@ -52,7 +52,7 @@ class AppCache(object):
# Cache for get_models.
self._get_models_cache = {}
- def populate_apps(self):
+ def populate_apps(self, installed_apps=None):
"""
Populate app-related information.
@@ -77,7 +77,9 @@ class AppCache(object):
# Application modules aren't expected to import anything, and
# especially not other application modules, even indirectly.
# Therefore we simply import them sequentially.
- for app_name in settings.INSTALLED_APPS:
+ if installed_apps is None:
+ installed_apps = settings.INSTALLED_APPS
+ for app_name in installed_apps:
app_config = AppConfig.create(app_name)
self.app_configs[app_config.label] = app_config
@@ -299,6 +301,8 @@ class AppCache(object):
available must be an iterable of application names.
+ set_available_apps() must be balanced with unset_available_apps().
+
Primarily used for performance optimization in TransactionTestCase.
This method is safe is the sense that it doesn't trigger any imports.
@@ -323,10 +327,13 @@ class AppCache(object):
def set_installed_apps(self, installed):
"""
- Enables a different set of installed_apps for get_app_config[s].
+ Enables a different set of installed apps for get_app_config[s].
installed must be an iterable in the same format as INSTALLED_APPS.
+ set_installed_apps() must be balanced with unset_installed_apps(),
+ even if it exits with an exception.
+
Primarily used as a receiver of the setting_changed signal in tests.
This method may trigger new imports, which may add new models to the
@@ -337,14 +344,10 @@ class AppCache(object):
"""
self.stored_app_configs.append(self.app_configs)
self.app_configs = OrderedDict()
- try:
- self._apps_loaded = False
- self.populate_apps()
- self._models_loaded = False
- self.populate_models()
- except Exception:
- self.unset_installed_apps()
- raise
+ self._apps_loaded = False
+ self.populate_apps(installed)
+ self._models_loaded = False
+ self.populate_models()
def unset_installed_apps(self):
"""