From 0f8f46802a172d47971a848227065894efd6a622 Mon Sep 17 00:00:00 2001
From: Chris Beaven <smileychris@gmail.com>
Date: Sun, 28 Aug 2011 02:37:16 +0000
Subject: [PATCH] Fixed #15525 -- Custom template tags loading breaks whenever
 templatetags is a python file

git-svn-id: http://code.djangoproject.com/svn/django/trunk@16703 bcc190cf-cafb-0310-a4f2-bffc1f526a37
---
 django/utils/module_loading.py                   | 10 ++++++++--
 tests/regressiontests/utils/module_loading.py    |  7 +++++++
 tests/regressiontests/utils/test_no_submodule.py |  1 +
 3 files changed, 16 insertions(+), 2 deletions(-)
 create mode 100644 tests/regressiontests/utils/test_no_submodule.py

diff --git a/django/utils/module_loading.py b/django/utils/module_loading.py
index 26a7e51e0d..f8aadb34df 100644
--- a/django/utils/module_loading.py
+++ b/django/utils/module_loading.py
@@ -11,10 +11,16 @@ def module_has_submodule(package, module_name):
         return sys.modules[name] is not None
     except KeyError:
         pass
+    try:
+        package_path = package.__path__   # No __path__, then not a package.
+    except AttributeError:
+        # Since the remainder of this function assumes that we're dealing with
+        # a package (module with a __path__), so if it's not, then bail here.
+        return False
     for finder in sys.meta_path:
-        if finder.find_module(name, package.__path__):
+        if finder.find_module(name, package_path):
             return True
-    for entry in package.__path__:  # No __path__, then not a package.
+    for entry in package_path:
         try:
             # Try the cached finder.
             finder = sys.path_importer_cache[entry]
diff --git a/tests/regressiontests/utils/module_loading.py b/tests/regressiontests/utils/module_loading.py
index 2e9d167fe9..cbd81c7294 100644
--- a/tests/regressiontests/utils/module_loading.py
+++ b/tests/regressiontests/utils/module_loading.py
@@ -18,6 +18,8 @@ class DefaultLoader(unittest.TestCase):
     def test_loader(self):
         "Normal module existence can be tested"
         test_module = import_module('regressiontests.utils.test_module')
+        test_no_submodule = import_module(
+            'regressiontests.utils.test_no_submodule')
 
         # An importable child
         self.assertTrue(module_has_submodule(test_module, 'good_module'))
@@ -40,6 +42,11 @@ class DefaultLoader(unittest.TestCase):
         import types  # causes attempted import of regressiontests.utils.types
         self.assertFalse(module_has_submodule(sys.modules['regressiontests.utils'], 'types'))
 
+        # A module which doesn't have a __path__ (so no submodules)
+        self.assertFalse(module_has_submodule(test_no_submodule, 'anything'))
+        self.assertRaises(ImportError, import_module,
+            'regressiontests.utils.test_no_submodule.anything')
+
 class EggLoader(unittest.TestCase):
     def setUp(self):
         self.old_path = sys.path[:]
diff --git a/tests/regressiontests/utils/test_no_submodule.py b/tests/regressiontests/utils/test_no_submodule.py
new file mode 100644
index 0000000000..3c424ac788
--- /dev/null
+++ b/tests/regressiontests/utils/test_no_submodule.py
@@ -0,0 +1 @@
+# Used to test for modules which don't have submodules.