Back to index

python3.2  3.2.2
Public Member Functions
importlib.test.source.test_file_loader.SimpleTest Class Reference

List of all members.

Public Member Functions

def test_module
def test_package
def test_lacking_parent
def fake_mtime
def test_module_reuse
def test_state_after_failure
def test_bad_syntax
def test_file_from_empty_string_dir

Detailed Description

Should have no issue importing a source module [basic]. And if there is
a syntax error, it should raise a SyntaxError [syntax error].

Definition at line 19 of file test_file_loader.py.


Member Function Documentation

Fake mtime to always be higher than expected.

Definition at line 61 of file test_file_loader.py.

00061 
00062     def fake_mtime(self, fxn):
00063         """Fake mtime to always be higher than expected."""
00064         return lambda name: fxn(name) + 1

Here is the caller graph for this function:

Definition at line 104 of file test_file_loader.py.

00104 
00105     def test_bad_syntax(self):
00106         with source_util.create_modules('_temp') as mapping:
00107             with open(mapping['_temp'], 'w') as file:
00108                 file.write('=')
00109             loader = _bootstrap._SourceFileLoader('_temp', mapping['_temp'])
00110             with self.assertRaises(SyntaxError):
00111                 loader.load_module('_temp')
00112             self.assertTrue('_temp' not in sys.modules)

Here is the call graph for this function:

Definition at line 113 of file test_file_loader.py.

00113 
00114     def test_file_from_empty_string_dir(self):
00115         # Loading a module found from an empty string entry on sys.path should
00116         # not only work, but keep all attributes relative.
00117         file_path = '_temp.py'
00118         with open(file_path, 'w') as file:
00119             file.write("# test file for importlib")
00120         try:
00121             with util.uncache('_temp'):
00122                 loader = _bootstrap._SourceFileLoader('_temp', file_path)
00123                 mod = loader.load_module('_temp')
00124                 self.assertEqual(file_path, mod.__file__)
00125                 self.assertEqual(imp.cache_from_source(file_path),
00126                                  mod.__cached__)
00127         finally:
00128             os.unlink(file_path)
00129             pycache = os.path.dirname(imp.cache_from_source(file_path))
00130             shutil.rmtree(pycache)
00131 

Here is the call graph for this function:

Definition at line 50 of file test_file_loader.py.

00050 
00051     def test_lacking_parent(self):
00052         with source_util.create_modules('_pkg.__init__', '_pkg.mod')as mapping:
00053             loader = _bootstrap._SourceFileLoader('_pkg.mod',
00054                                                     mapping['_pkg.mod'])
00055             module = loader.load_module('_pkg.mod')
00056             self.assertTrue('_pkg.mod' in sys.modules)
00057             check = {'__name__': '_pkg.mod', '__file__': mapping['_pkg.mod'],
00058                      '__package__': '_pkg'}
00059             for attr, value in check.items():
00060                 self.assertEqual(getattr(module, attr), value)

Here is the call graph for this function:

Definition at line 27 of file test_file_loader.py.

00027 
00028     def test_module(self):
00029         with source_util.create_modules('_temp') as mapping:
00030             loader = _bootstrap._SourceFileLoader('_temp', mapping['_temp'])
00031             module = loader.load_module('_temp')
00032             self.assertTrue('_temp' in sys.modules)
00033             check = {'__name__': '_temp', '__file__': mapping['_temp'],
00034                      '__package__': ''}
00035             for attr, value in check.items():
00036                 self.assertEqual(getattr(module, attr), value)

Here is the call graph for this function:

Definition at line 65 of file test_file_loader.py.

00065 
00066     def test_module_reuse(self):
00067         with source_util.create_modules('_temp') as mapping:
00068             loader = _bootstrap._SourceFileLoader('_temp', mapping['_temp'])
00069             module = loader.load_module('_temp')
00070             module_id = id(module)
00071             module_dict_id = id(module.__dict__)
00072             with open(mapping['_temp'], 'w') as file:
00073                 file.write("testing_var = 42\n")
00074             # For filesystems where the mtime is only to a second granularity,
00075             # everything that has happened above can be too fast;
00076             # force an mtime on the source that is guaranteed to be different
00077             # than the original mtime.
00078             loader.path_mtime = self.fake_mtime(loader.path_mtime)
00079             module = loader.load_module('_temp')
00080             self.assertTrue('testing_var' in module.__dict__,
00081                          "'testing_var' not in "
00082                             "{0}".format(list(module.__dict__.keys())))
00083             self.assertEqual(module, sys.modules['_temp'])
00084             self.assertEqual(id(module), module_id)
00085             self.assertEqual(id(module.__dict__), module_dict_id)

Here is the call graph for this function:

Definition at line 37 of file test_file_loader.py.

00037 
00038     def test_package(self):
00039         with source_util.create_modules('_pkg.__init__') as mapping:
00040             loader = _bootstrap._SourceFileLoader('_pkg',
00041                                                  mapping['_pkg.__init__'])
00042             module = loader.load_module('_pkg')
00043             self.assertTrue('_pkg' in sys.modules)
00044             check = {'__name__': '_pkg', '__file__': mapping['_pkg.__init__'],
00045                      '__path__': [os.path.dirname(mapping['_pkg.__init__'])],
00046                      '__package__': '_pkg'}
00047             for attr, value in check.items():
00048                 self.assertEqual(getattr(module, attr), value)
00049 

Here is the call graph for this function:

Definition at line 86 of file test_file_loader.py.

00086 
00087     def test_state_after_failure(self):
00088         # A failed reload should leave the original module intact.
00089         attributes = ('__file__', '__path__', '__package__')
00090         value = '<test>'
00091         name = '_temp'
00092         with source_util.create_modules(name) as mapping:
00093             orig_module = imp.new_module(name)
00094             for attr in attributes:
00095                 setattr(orig_module, attr, value)
00096             with open(mapping[name], 'w') as file:
00097                 file.write('+++ bad syntax +++')
00098             loader = _bootstrap._SourceFileLoader('_temp', mapping['_temp'])
00099             with self.assertRaises(SyntaxError):
00100                 loader.load_module(name)
00101             for attr in attributes:
00102                 self.assertEqual(getattr(orig_module, attr), value)

Here is the call graph for this function:


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