Back to index

python3.2  3.2.2
Public Member Functions
importlib.test.import_.test_caching.UseCache Class Reference

List of all members.

Public Member Functions

def test_using_cache
def test_None_in_cache
def create_mock
def test_using_cache_after_loader
def test_using_cache_for_assigning_to_attribute
def test_using_cache_for_fromlist

Detailed Description

When it comes to sys.modules, import prefers it over anything else.

Once a name has been resolved, sys.modules is checked to see if it contains
the module desired. If so, then it is returned [use cache]. If it is not
found, then the proper steps are taken to perform the import, but
sys.modules is still used to return the imported module (e.g., not what a
loader returns) [from cache on return]. This also applies to imports of
things contained within a package and thus get assigned as an attribute
[from cache to attribute] or pulled in thanks to a fromlist import
[from cache for fromlist]. But if sys.modules contains None then
ImportError is raised [None in cache].

Definition at line 9 of file

Member Function Documentation

def importlib.test.import_.test_caching.UseCache.create_mock (   self,
  return_ = None 

Definition at line 40 of file

00041     def create_mock(self, *names, return_=None):
00042         mock = util.mock_modules(*names)
00043         original_load = mock.load_module
00044         def load_module(self, fullname):
00045             original_load(fullname)
00046             return return_
00047         mock.load_module = MethodType(load_module, mock)
00048         return mock

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 32 of file

00033     def test_None_in_cache(self):
00034         #[None in cache]
00035         name = 'using_None'
00036         with util.uncache(name):
00037             sys.modules[name] = None
00038             with self.assertRaises(ImportError):
00039                 import_util.import_(name)

Here is the call graph for this function:

Definition at line 24 of file

00025     def test_using_cache(self):
00026         # [use cache]
00027         module_to_use = "some module found!"
00028         with util.uncache(module_to_use):
00029             sys.modules['some_module'] = module_to_use
00030             module = import_util.import_('some_module')
00031             self.assertEqual(id(module_to_use), id(module))

Here is the call graph for this function:

Definition at line 52 of file

00053     def test_using_cache_after_loader(self):
00054         # [from cache on return]
00055         with self.create_mock('module') as mock:
00056             with util.import_state(meta_path=[mock]):
00057                 module = import_util.import_('module')
00058                 self.assertEqual(id(module), id(sys.modules['module']))

Here is the call graph for this function:

Definition at line 61 of file

00062     def test_using_cache_for_assigning_to_attribute(self):
00063         # [from cache to attribute]
00064         with self.create_mock('pkg.__init__', 'pkg.module') as importer:
00065             with util.import_state(meta_path=[importer]):
00066                 module = import_util.import_('pkg.module')
00067                 self.assertTrue(hasattr(module, 'module'))
00068                 self.assertTrue(id(module.module), id(sys.modules['pkg.module']))

Here is the call graph for this function:

Definition at line 71 of file

00072     def test_using_cache_for_fromlist(self):
00073         # [from cache for fromlist]
00074         with self.create_mock('pkg.__init__', 'pkg.module') as importer:
00075             with util.import_state(meta_path=[importer]):
00076                 module = import_util.import_('pkg', fromlist=['module'])
00077                 self.assertTrue(hasattr(module, 'module'))
00078                 self.assertEqual(id(module.module),
00079                                  id(sys.modules['pkg.module']))

Here is the call graph for this function:

The documentation for this class was generated from the following file: