From 56ebab9d8596f63db38053816ec961656e49ed38 Mon Sep 17 00:00:00 2001 From: Russell Keith-Magee Date: Sun, 30 Jan 2011 13:10:47 +0000 Subject: [PATCH] =?UTF-8?q?Fixed=20#14698=20--=20Ensure=20that=20module=5F?= =?UTF-8?q?has=5Fsumodule=20doesn't=20mistake=20a=20cache=20miss=20for=20a?= =?UTF-8?q?n=20existent=20package.=20Thanks=20to=20=C5=81ukasz=20Rekucki?= =?UTF-8?q?=20for=20the=20report=20and=20patch,=20and=20to=20shields=20for?= =?UTF-8?q?=20the=20test=20case.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit git-svn-id: http://code.djangoproject.com/svn/django/trunk@15362 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 cffec9bad9..24c0b0753f 100644 --- a/tests/regressiontests/utils/module_loading.py +++ b/tests/regressiontests/utils/module_loading.py @@ -25,6 +25,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[:]