Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #15525 -- Custom template tags loading breaks whenever template…

…tags is a python file

git-svn-id: http://code.djangoproject.com/svn/django/trunk@16703 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 0f8f46802a172d47971a848227065894efd6a622 1 parent 77189af
Chris Beaven authored August 28, 2011
10  django/utils/module_loading.py
@@ -11,10 +11,16 @@ def module_has_submodule(package, module_name):
11 11
         return sys.modules[name] is not None
12 12
     except KeyError:
13 13
         pass
  14
+    try:
  15
+        package_path = package.__path__   # No __path__, then not a package.
  16
+    except AttributeError:
  17
+        # Since the remainder of this function assumes that we're dealing with
  18
+        # a package (module with a __path__), so if it's not, then bail here.
  19
+        return False
14 20
     for finder in sys.meta_path:
15  
-        if finder.find_module(name, package.__path__):
  21
+        if finder.find_module(name, package_path):
16 22
             return True
17  
-    for entry in package.__path__:  # No __path__, then not a package.
  23
+    for entry in package_path:
18 24
         try:
19 25
             # Try the cached finder.
20 26
             finder = sys.path_importer_cache[entry]
7  tests/regressiontests/utils/module_loading.py
@@ -18,6 +18,8 @@ def tearDown(self):
18 18
     def test_loader(self):
19 19
         "Normal module existence can be tested"
20 20
         test_module = import_module('regressiontests.utils.test_module')
  21
+        test_no_submodule = import_module(
  22
+            'regressiontests.utils.test_no_submodule')
21 23
 
22 24
         # An importable child
23 25
         self.assertTrue(module_has_submodule(test_module, 'good_module'))
@@ -40,6 +42,11 @@ def test_loader(self):
40 42
         import types  # causes attempted import of regressiontests.utils.types
41 43
         self.assertFalse(module_has_submodule(sys.modules['regressiontests.utils'], 'types'))
42 44
 
  45
+        # A module which doesn't have a __path__ (so no submodules)
  46
+        self.assertFalse(module_has_submodule(test_no_submodule, 'anything'))
  47
+        self.assertRaises(ImportError, import_module,
  48
+            'regressiontests.utils.test_no_submodule.anything')
  49
+
43 50
 class EggLoader(unittest.TestCase):
44 51
     def setUp(self):
45 52
         self.old_path = sys.path[:]
1  tests/regressiontests/utils/test_no_submodule.py
... ...
@@ -0,0 +1 @@
  1
+# Used to test for modules which don't have submodules.

0 notes on commit 0f8f468

Please sign in to comment.
Something went wrong with that request. Please try again.