diff options
author | Russell Keith-Magee <russell@keith-magee.com> | 2011-01-30 13:20:00 +0000 |
---|---|---|
committer | Russell Keith-Magee <russell@keith-magee.com> | 2011-01-30 13:20:00 +0000 |
commit | 7e83c3b91f46458c6b2243b21ac144499bec47cf (patch) | |
tree | a73b95823e78a5c1eea3a196bc9f532616fbf44d | |
parent | a289eba59c72c8abab9722b2038ff74c366d0f04 (diff) | |
download | django-7e83c3b91f46458c6b2243b21ac144499bec47cf.tar.gz |
[1.2.X] Fixed #14698 -- Ensure that module_has_sumodule doesn't mistake a cache miss for an existent package. Thanks to Ćukasz Rekucki for the report and patch, and to shields for the test case.
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
-rw-r--r-- | django/utils/module_loading.py | 7 | ||||
-rw-r--r-- | 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[:] |