From 7e83c3b91f46458c6b2243b21ac144499bec47cf Mon Sep 17 00:00:00 2001 From: Russell Keith-Magee Date: Sun, 30 Jan 2011 13:20:00 +0000 Subject: [PATCH] =?UTF-8?q?[1.2.X]=20Fixed=20#14698=20--=20Ensure=20that?= =?UTF-8?q?=20module=5Fhas=5Fsumodule=20doesn't=20mistake=20a=20cache=20mi?= =?UTF-8?q?ss=20for=20an=20existent=20package.=20Thanks=20to=20=C5=81ukasz?= =?UTF-8?q?=20Rekucki=20for=20the=20report=20and=20patch,=20and=20to=20shi?= =?UTF-8?q?elds=20for=20the=20test=20case.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Backport of r15362 from trunk. git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.2.X@15364 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/utils/module_loading.py | 7 +++++-- tests/regressiontests/utils/module_loading.py | 4 ++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/django/utils/module_loading.py b/django/utils/module_loading.py index f251035387..32ca69a9fd 100644 --- a/django/utils/module_loading.py +++ b/django/utils/module_loading.py @@ -6,8 +6,11 @@ import sys def module_has_submodule(package, module_name): """See if 'module' is in 'package'.""" name = ".".join([package.__name__, module_name]) - if name in sys.modules: - return True + try: + # None indicates a cached miss; see mark_miss() in Python/import.c. + return sys.modules[name] is not None + except KeyError: + pass for finder in sys.meta_path: if finder.find_module(name): return True diff --git a/tests/regressiontests/utils/module_loading.py b/tests/regressiontests/utils/module_loading.py index 8cbefbb011..4cc67ea92f 100644 --- a/tests/regressiontests/utils/module_loading.py +++ b/tests/regressiontests/utils/module_loading.py @@ -24,6 +24,10 @@ class DefaultLoader(unittest.TestCase): self.assertFalse(module_has_submodule(test_module, 'no_such_module')) self.assertRaises(ImportError, import_module, 'regressiontests.utils.test_module.no_such_module') + # Don't be confused by caching of import misses + import types # causes attempted import of regressiontests.utils.types + self.assertFalse(module_has_submodule(sys.modules['regressiontests.utils'], 'types')) + class EggLoader(unittest.TestCase): def setUp(self): self.old_path = sys.path[:]