summaryrefslogtreecommitdiff
path: root/django/utils/module_loading.py
diff options
context:
space:
mode:
authorBerker Peksag <berker.peksag@gmail.com>2014-01-20 22:15:14 +0200
committerTim Graham <timograham@gmail.com>2014-02-08 11:12:19 -0500
commit5d263dee304fdaf95e18d2f0619d6925984a7f02 (patch)
tree764bc0cd21bca796d9f1bd0b8d2645a9773e48de /django/utils/module_loading.py
parentfcc21837dc691ba6465853c0b68927cb075a4591 (diff)
Fixed #21674 -- Deprecated the import_by_path() function in favor of import_string().
Thanks Aymeric Augustin for the suggestion and review.
Diffstat (limited to 'django/utils/module_loading.py')
-rw-r--r--django/utils/module_loading.py37
1 files changed, 26 insertions, 11 deletions
diff --git a/django/utils/module_loading.py b/django/utils/module_loading.py
index a6fe7aebcb..7ba0381307 100644
--- a/django/utils/module_loading.py
+++ b/django/utils/module_loading.py
@@ -5,33 +5,48 @@ import imp
from importlib import import_module
import os
import sys
+import warnings
from django.core.exceptions import ImproperlyConfigured
from django.utils import six
-def import_by_path(dotted_path, error_prefix=''):
+def import_string(dotted_path):
"""
Import a dotted module path and return the attribute/class designated by the
- last name in the path. Raise ImproperlyConfigured if something goes wrong.
+ last name in the path. Raise ImportError if the import failed.
"""
try:
module_path, class_name = dotted_path.rsplit('.', 1)
except ValueError:
- raise ImproperlyConfigured("%s%s doesn't look like a module path" % (
- error_prefix, dotted_path))
+ msg = "%s doesn't look like a module path" % dotted_path
+ six.reraise(ImportError, ImportError(msg), sys.exc_info()[2])
+
+ module = import_module(module_path)
+
try:
- module = import_module(module_path)
+ return getattr(module, class_name)
+ except AttributeError:
+ msg = 'Module "%s" does not define a "%s" attribute/class' % (
+ dotted_path, class_name)
+ six.reraise(ImportError, ImportError(msg), sys.exc_info()[2])
+
+
+def import_by_path(dotted_path, error_prefix=''):
+ """
+ Import a dotted module path and return the attribute/class designated by the
+ last name in the path. Raise ImproperlyConfigured if something goes wrong.
+ """
+ warnings.warn(
+ 'import_by_path() has been deprecated. Use import_string() instead.',
+ PendingDeprecationWarning, stacklevel=2)
+ try:
+ attr = import_string(dotted_path)
except ImportError as e:
msg = '%sError importing module %s: "%s"' % (
- error_prefix, module_path, e)
+ error_prefix, dotted_path, e)
six.reraise(ImproperlyConfigured, ImproperlyConfigured(msg),
sys.exc_info()[2])
- try:
- attr = getattr(module, class_name)
- except AttributeError:
- raise ImproperlyConfigured('%sModule "%s" does not define a "%s" attribute/class' % (
- error_prefix, module_path, class_name))
return attr