From ee85fef48f13da8a29bb9c2f09f116a40337b982 Mon Sep 17 00:00:00 2001 From: Mike Bryant Date: Wed, 8 Oct 2014 19:54:31 +0100 Subject: [PATCH] Use repr instead of str in debugging messages, to avoid premature imports of the urlconf. Fixes #10. --- django_autoconfig/autoconfig.py | 12 ++++++------ django_autoconfig/tests/__init__.py | 29 +++++++++++++++++++++++++++-- 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/django_autoconfig/autoconfig.py b/django_autoconfig/autoconfig.py index 42c8f02..c63475c 100644 --- a/django_autoconfig/autoconfig.py +++ b/django_autoconfig/autoconfig.py @@ -62,7 +62,7 @@ def apply_changes(self, settings): for item in [self.setting_value] + self.before + self.after: if item not in settings[self.setting_name]: settings[self.setting_name] = list(settings[self.setting_name]) + [item] - LOGGER.debug("Added %s to %s.", item, self.setting_name) + LOGGER.debug("Added %r to %r.", item, self.setting_name) changes += 1 elif self.setting_value not in settings[self.setting_name]: return changes @@ -86,7 +86,7 @@ def apply_changes(self, settings): current_value.remove(self.setting_value) current_value.insert(location, self.setting_value) settings[self.setting_name] = current_value - LOGGER.debug("Moved %s %s %s.", self.setting_value, list_name, item) + LOGGER.debug("Moved %r %r %r.", self.setting_value, list_name, item) changes += 1 return changes @@ -101,10 +101,10 @@ def merge_dictionaries(current, new, only_defaults=False): if key not in current: if hasattr(global_settings, key): current[key] = getattr(global_settings, key) - LOGGER.debug("Set %s to global default %s.", key, current[key]) + LOGGER.debug("Set %r to global default %r.", key, current[key]) else: current[key] = copy.copy(value) - LOGGER.debug("Set %s to %s.", key, current[key]) + LOGGER.debug("Set %r to %r.", key, current[key]) changes += 1 continue elif only_defaults: @@ -116,13 +116,13 @@ def merge_dictionaries(current, new, only_defaults=False): for element in value: if element not in current_value: current[key] = list(current_value) + [element] - LOGGER.debug("Added %s to %s.", element, key) + LOGGER.debug("Added %r to %r.", element, key) changes += 1 else: # If we don't know what to do with it, replace it. if current_value != value: current[key] = value - LOGGER.debug("Set %s to %s.", key, current[key]) + LOGGER.debug("Set %r to %r.", key, current[key]) changes += 1 return changes diff --git a/django_autoconfig/tests/__init__.py b/django_autoconfig/tests/__init__.py index e681f38..40493f0 100644 --- a/django_autoconfig/tests/__init__.py +++ b/django_autoconfig/tests/__init__.py @@ -7,9 +7,8 @@ import copy from django.core.exceptions import ImproperlyConfigured import django.core.urlresolvers -from django.core.urlresolvers import resolve, RegexURLResolver +from django.core.urlresolvers import resolve from django import test -from django.test.utils import override_settings class ConfigureSettingsTestCase(test.TestCase): '''Test the configure_settings method.''' @@ -158,6 +157,32 @@ def test_contrib_autoconfig(self): autoconfig.configure_settings(self.settings_dict) self.assertIn('django.contrib.sessions', self.settings_dict['INSTALLED_APPS']) + def test_logging_premature_imports(self): + ''' + Test that logging doesn't cause premature imports. + ''' + + import logging, StringIO + output = StringIO.StringIO() + stream_handler = logging.StreamHandler(output) + logger = logging.getLogger('django_autoconfig.autoconfig') + logger.addHandler(stream_handler) + logger.setLevel(logging.DEBUG) + + self.triggered = False + class Trap(object): + def __init__(self, sentinel): + self.sentinel = sentinel + def __str__(self): + self.sentinel.triggered = True + return 'triggered!' + + autoconfig.merge_dictionaries({}, {'LOGIN_URL': Trap(self)}) + self.assertFalse(self.triggered) + + logger.removeHandler(stream_handler) + + class ConfigureUrlsTestCase(test.TestCase): '''Test the autoconfiguration of the urlconf.''' urls = 'django_autoconfig.autourlconf'