summaryrefslogtreecommitdiff
path: root/django/utils
diff options
context:
space:
mode:
authorJacob Kaplan-Moss <jacob@jacobian.org>2009-03-18 16:55:59 +0000
committerJacob Kaplan-Moss <jacob@jacobian.org>2009-03-18 16:55:59 +0000
commitc485e236bd7e5ea40c64b2fe54d85dbb15b2fd39 (patch)
treefc5e86abf39b69181b2084f5c39038f1811b6b44 /django/utils
parentee2f04d79e5bca55637b9bb3301618738a4e342a (diff)
Fixed #8193: all dynamic imports in Django are now done correctly. I know this because Brett Cannon borrowed the time machine and brought Python 2.7's '`importlib` back for inclusion in Django. Thanks for the patch-from-the-future, Brett!
git-svn-id: http://code.djangoproject.com/svn/django/trunk@10088 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/utils')
-rw-r--r--django/utils/importlib.py36
-rw-r--r--django/utils/translation/trans_real.py10
2 files changed, 39 insertions, 7 deletions
diff --git a/django/utils/importlib.py b/django/utils/importlib.py
new file mode 100644
index 0000000000..ef4d0e4a27
--- /dev/null
+++ b/django/utils/importlib.py
@@ -0,0 +1,36 @@
+# Taken from Python 2.7 with permission from/by the original author.
+import sys
+
+def _resolve_name(name, package, level):
+ """Return the absolute name of the module to be imported."""
+ if not hasattr(package, 'rindex'):
+ raise ValueError("'package' not set to a string")
+ dot = len(package)
+ for x in xrange(level, 1, -1):
+ try:
+ dot = package.rindex('.', 0, dot)
+ except ValueError:
+ raise ValueError("attempted relative import beyond top-level "
+ "package")
+ return "%s.%s" % (package[:dot], name)
+
+
+def import_module(name, package=None):
+ """Import a module.
+
+ The 'package' argument is required when performing a relative import. It
+ specifies the package to use as the anchor point from which to resolve the
+ relative import to an absolute import.
+
+ """
+ if name.startswith('.'):
+ if not package:
+ raise TypeError("relative imports require the 'package' argument")
+ level = 0
+ for character in name:
+ if character != '.':
+ break
+ level += 1
+ name = _resolve_name(name[level:], package, level)
+ __import__(name)
+ return sys.modules[name]
diff --git a/django/utils/translation/trans_real.py b/django/utils/translation/trans_real.py
index 7a0e104b37..48ed7cc885 100644
--- a/django/utils/translation/trans_real.py
+++ b/django/utils/translation/trans_real.py
@@ -7,6 +7,7 @@ import sys
import gettext as gettext_module
from cStringIO import StringIO
+from django.utils.importlib import import_module
from django.utils.safestring import mark_safe, SafeData
from django.utils.thread_support import currentThread
@@ -125,7 +126,7 @@ def translation(language):
if settings.SETTINGS_MODULE is not None:
parts = settings.SETTINGS_MODULE.split('.')
- project = __import__(parts[0], {}, {}, [])
+ project = import_module(parts[0])
projectpath = os.path.join(os.path.dirname(project.__file__), 'locale')
else:
projectpath = None
@@ -176,12 +177,7 @@ def translation(language):
res = _merge(projectpath)
for appname in settings.INSTALLED_APPS:
- p = appname.rfind('.')
- if p >= 0:
- app = getattr(__import__(appname[:p], {}, {}, [appname[p+1:]]), appname[p+1:])
- else:
- app = __import__(appname, {}, {}, [])
-
+ app = import_module(appname)
apppath = os.path.join(os.path.dirname(app.__file__), 'locale')
if os.path.isdir(apppath):