summaryrefslogtreecommitdiff
path: root/django/utils/module_loading.py
diff options
context:
space:
mode:
authorKaren Tracey <kmtracey@gmail.com>2010-04-15 18:44:51 +0000
committerKaren Tracey <kmtracey@gmail.com>2010-04-15 18:44:51 +0000
commit29341aaffc28866e15e11109b26c6446f39cb68d (patch)
tree03c1bd6ae14bea9b45203e7be8874624ac63d804 /django/utils/module_loading.py
parent9ac4d2fc613d76129b0e980a7310321021982a19 (diff)
downloaddjango-29341aaffc28866e15e11109b26c6446f39cb68d.tar.gz
Fixed #13348: Restored ability to load models from apps in eggs. Thanks Ramiro and metzen for pointers on how to find out if a module loaded from an egg has a particular submodule.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@12982 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/utils/module_loading.py')
-rw-r--r--django/utils/module_loading.py27
1 files changed, 27 insertions, 0 deletions
diff --git a/django/utils/module_loading.py b/django/utils/module_loading.py
new file mode 100644
index 0000000000..b23336e9df
--- /dev/null
+++ b/django/utils/module_loading.py
@@ -0,0 +1,27 @@
+import os
+import imp
+
+def module_has_submodule(mod, submod_name):
+ # If the module was loaded from an egg, __loader__ will be set and
+ # its find_module must be used to search for submodules.
+ loader = getattr(mod, '__loader__', None)
+ if loader:
+ mod_path = "%s.%s" % (mod.__name__, submod_name)
+ mod_path = mod_path[len(loader.prefix):]
+ x = loader.find_module(mod_path)
+ if x is None:
+ # zipimport.zipimporter.find_module is documented to take
+ # dotted paths but in fact through Pyton 2.7 is observed
+ # to require os.sep in place of dots...so try using os.sep
+ # if the dotted path version failed to find the requested
+ # submodule.
+ x = loader.find_module(mod_path.replace('.', os.sep))
+ return x is not None
+
+ try:
+ imp.find_module(submod_name, mod.__path__)
+ return True
+ except ImportError:
+ return False
+
+