Skip to content

Commit

Permalink
Fixed #14698 -- Ensure that module_has_sumodule doesn't mistake a cac…
Browse files Browse the repository at this point in the history
…he miss for an existent package. Thanks to Łukasz Rekucki for the report and patch, and to shields for the test case.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@15362 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
freakboy3742 committed Jan 30, 2011
1 parent 4392c8e commit 56ebab9
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
7 changes: 5 additions & 2 deletions django/utils/module_loading.py
Expand Up @@ -6,8 +6,11 @@
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
Expand Down
4 changes: 4 additions & 0 deletions tests/regressiontests/utils/module_loading.py
Expand Up @@ -25,6 +25,10 @@ def test_loader(self):
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[:]
Expand Down

0 comments on commit 56ebab9

Please sign in to comment.