Ticket #15662: module_has_submodule.patch

File module_has_submodule.patch, 2.7 KB (added by Bradley Ayers <bradley.ayers@…>, 14 years ago)

Added tests

  • tests/regressiontests/utils/module_loading.py

     
    11import os
    22import sys
     3import imp
    34from zipimport import zipimporter
    45
    56from django.utils import unittest
     
    89
    910
    1011class DefaultLoader(unittest.TestCase):
     12    def setUp(self):
     13        sys.meta_path.insert(0, ProxyFinder())
     14
     15    def tearDown(self):
     16        sys.meta_path.pop(0)
     17
    1118    def test_loader(self):
    1219        "Normal module existence can be tested"
    1320        test_module = import_module('regressiontests.utils.test_module')
     
    2532        self.assertFalse(module_has_submodule(test_module, 'no_such_module'))
    2633        self.assertRaises(ImportError, import_module, 'regressiontests.utils.test_module.no_such_module')
    2734
     35        # A child that doesn't exist, but is the name of a package on the path
     36        self.assertFalse(module_has_submodule(test_module, 'django'))
     37        self.assertRaises(ImportError, import_module, 'regressiontests.utils.test_module.django')
     38
    2839        # Don't be confused by caching of import misses
    2940        import types  # causes attempted import of regressiontests.utils.types
    3041        self.assertFalse(module_has_submodule(sys.modules['regressiontests.utils'], 'types'))
     
    8495        self.assertFalse(module_has_submodule(egg_module, 'no_such_module'))
    8596        self.assertRaises(ImportError, import_module, 'egg_module.sub1.sub2.no_such_module')
    8697
     98class ProxyFinder(object):
     99    def __init__(self):
     100        self._cache = {}
     101
     102    def find_module(self, fullname, path=None):
     103        tail = fullname.rsplit('.', 1)[-1]
     104        try:
     105            self._cache[fullname] = imp.find_module(tail, path)
     106        except ImportError:
     107            return None
     108        else:
     109            return self  # this is a loader as well
     110
     111    def load_module(self, fullname):
     112        if fullname in sys.modules:
     113            return sys.modules[fullname]
     114        fd, fn, info = self._cache[fullname]
     115        return imp.load_module(fullname, fd, fn, info)
     116
    87117class TestFinder(object):
    88118    def __init__(self, *args, **kwargs):
    89119        self.importer = zipimporter(*args, **kwargs)
  • django/utils/module_loading.py

     
    1212    except KeyError:
    1313        pass
    1414    for finder in sys.meta_path:
    15         if finder.find_module(name):
     15        if finder.find_module(name, package):
    1616            return True
    1717    for entry in package.__path__:  # No __path__, then not a package.
    1818        try:
Back to Top