예제 #1
0
 def run_test(self, source):
     with source_util.create_modules(self.module_name) as mapping:
         with open(mapping[self.module_name], 'wb') as file:
             file.write(source)
         loader = _bootstrap._SourceFileLoader(self.module_name,
                                               mapping[self.module_name])
         return loader.load_module(self.module_name)
예제 #2
0
 def run_test(self, source):
     with source_util.create_modules(self.module_name) as mapping:
         with open(mapping[self.module_name], 'wb') as file:
             file.write(source)
         loader = _bootstrap._SourceFileLoader(self.module_name,
                                    mapping[self.module_name])
         return loader.load_module(self.module_name)
예제 #3
0
 def test_timestamp_overflow(self):
     # When a modification timestamp is larger than 2**32, it should be
     # truncated rather than raise an OverflowError.
     with source_util.create_modules('_temp') as mapping:
         source = mapping['_temp']
         compiled = imp.cache_from_source(source)
         with open(source, 'w') as f:
             f.write("x = 5")
         try:
             os.utime(source, (2**33 - 5, 2**33 - 5))
         except OverflowError:
             self.skipTest("cannot set modification time to large integer")
         except OSError as e:
             if e.errno != getattr(errno, 'EOVERFLOW', None):
                 raise
             self.skipTest(
                 "cannot set modification time to large integer ({})".
                 format(e))
         loader = _bootstrap._SourceFileLoader('_temp', mapping['_temp'])
         mod = loader.load_module('_temp')
         # Sanity checks.
         self.assertEqual(mod.__cached__, compiled)
         self.assertEqual(mod.x, 5)
         # The pyc file was created.
         os.stat(compiled)
 def test_bad_syntax(self):
     with source_util.create_modules('_temp') as mapping:
         with open(mapping['_temp'], 'w') as file:
             file.write('=')
         loader = _bootstrap._SourceFileLoader('_temp', mapping['_temp'])
         with self.assertRaises(SyntaxError):
             loader.load_module('_temp')
         self.assertTrue('_temp' not in sys.modules)
예제 #5
0
 def test_bad_syntax(self):
     with source_util.create_modules('_temp') as mapping:
         with open(mapping['_temp'], 'w') as file:
             file.write('=')
         loader = _bootstrap._SourceFileLoader('_temp', mapping['_temp'])
         with self.assertRaises(SyntaxError):
             loader.load_module('_temp')
         self.assertTrue('_temp' not in sys.modules)
 def test_module(self):
     with source_util.create_modules('_temp') as mapping:
         loader = _bootstrap._SourceFileLoader('_temp', mapping['_temp'])
         module = loader.load_module('_temp')
         self.assertTrue('_temp' in sys.modules)
         check = {'__name__': '_temp', '__file__': mapping['_temp'],
                  '__package__': ''}
         for attr, value in check.items():
             self.assertEqual(getattr(module, attr), value)
예제 #7
0
 def run_test(self, line_ending):
     module_name = '_temp'
     source_lines = [b"a = 42", b"b = -13", b'']
     source = line_ending.join(source_lines)
     with source_util.create_modules(module_name) as mapping:
         with open(mapping[module_name], 'wb') as file:
             file.write(source)
         loader = _bootstrap._SourceFileLoader(module_name,
                                              mapping[module_name])
         return loader.load_module(module_name)
예제 #8
0
 def run_test(self, line_ending):
     module_name = '_temp'
     source_lines = [b"a = 42", b"b = -13", b'']
     source = line_ending.join(source_lines)
     with source_util.create_modules(module_name) as mapping:
         with open(mapping[module_name], 'wb') as file:
             file.write(source)
         loader = _bootstrap._SourceFileLoader(module_name,
                                               mapping[module_name])
         return loader.load_module(module_name)
예제 #9
0
 def test_module(self):
     with source_util.create_modules('_temp') as mapping:
         loader = _bootstrap._SourceFileLoader('_temp', mapping['_temp'])
         module = loader.load_module('_temp')
         self.assertTrue('_temp' in sys.modules)
         check = {
             '__name__': '_temp',
             '__file__': mapping['_temp'],
             '__package__': ''
         }
         for attr, value in check.items():
             self.assertEqual(getattr(module, attr), value)
 def test_state_after_failure(self):
     # A failed reload should leave the original module intact.
     attributes = ('__file__', '__path__', '__package__')
     value = '<test>'
     name = '_temp'
     with source_util.create_modules(name) as mapping:
         orig_module = imp.new_module(name)
         for attr in attributes:
             setattr(orig_module, attr, value)
         with open(mapping[name], 'w') as file:
             file.write('+++ bad syntax +++')
         loader = _bootstrap._SourceFileLoader('_temp', mapping['_temp'])
         with self.assertRaises(SyntaxError):
             loader.load_module(name)
         for attr in attributes:
             self.assertEqual(getattr(orig_module, attr), value)
예제 #11
0
 def test_state_after_failure(self):
     # A failed reload should leave the original module intact.
     attributes = ('__file__', '__path__', '__package__')
     value = '<test>'
     name = '_temp'
     with source_util.create_modules(name) as mapping:
         orig_module = imp.new_module(name)
         for attr in attributes:
             setattr(orig_module, attr, value)
         with open(mapping[name], 'w') as file:
             file.write('+++ bad syntax +++')
         loader = _bootstrap._SourceFileLoader('_temp', mapping['_temp'])
         with self.assertRaises(SyntaxError):
             loader.load_module(name)
         for attr in attributes:
             self.assertEqual(getattr(orig_module, attr), value)
 def test_file_from_empty_string_dir(self):
     # Loading a module found from an empty string entry on sys.path should
     # not only work, but keep all attributes relative.
     file_path = '_temp.py'
     with open(file_path, 'w') as file:
         file.write("# test file for importlib")
     try:
         with util.uncache('_temp'):
             loader = _bootstrap._SourceFileLoader('_temp', file_path)
             mod = loader.load_module('_temp')
             self.assertEqual(file_path, mod.__file__)
             self.assertEqual(imp.cache_from_source(file_path),
                              mod.__cached__)
     finally:
         os.unlink(file_path)
         pycache = os.path.dirname(imp.cache_from_source(file_path))
         shutil.rmtree(pycache)
예제 #13
0
 def test_file_from_empty_string_dir(self):
     # Loading a module found from an empty string entry on sys.path should
     # not only work, but keep all attributes relative.
     file_path = '_temp.py'
     with open(file_path, 'w') as file:
         file.write("# test file for importlib")
     try:
         with util.uncache('_temp'):
             loader = _bootstrap._SourceFileLoader('_temp', file_path)
             mod = loader.load_module('_temp')
             self.assertEqual(file_path, mod.__file__)
             self.assertEqual(imp.cache_from_source(file_path),
                              mod.__cached__)
     finally:
         os.unlink(file_path)
         pycache = os.path.dirname(imp.cache_from_source(file_path))
         shutil.rmtree(pycache)
 def test_module_reuse(self):
     with source_util.create_modules('_temp') as mapping:
         loader = _bootstrap._SourceFileLoader('_temp', mapping['_temp'])
         module = loader.load_module('_temp')
         module_id = id(module)
         module_dict_id = id(module.__dict__)
         with open(mapping['_temp'], 'w') as file:
             file.write("testing_var = 42\n")
         # For filesystems where the mtime is only to a second granularity,
         # everything that has happened above can be too fast;
         # force an mtime on the source that is guaranteed to be different
         # than the original mtime.
         loader.path_mtime = self.fake_mtime(loader.path_mtime)
         module = loader.load_module('_temp')
         self.assertTrue('testing_var' in module.__dict__,
                      "'testing_var' not in "
                         "{0}".format(list(module.__dict__.keys())))
         self.assertEqual(module, sys.modules['_temp'])
         self.assertEqual(id(module), module_id)
         self.assertEqual(id(module.__dict__), module_dict_id)
예제 #15
0
 def test_module_reuse(self):
     with source_util.create_modules('_temp') as mapping:
         loader = _bootstrap._SourceFileLoader('_temp', mapping['_temp'])
         module = loader.load_module('_temp')
         module_id = id(module)
         module_dict_id = id(module.__dict__)
         with open(mapping['_temp'], 'w') as file:
             file.write("testing_var = 42\n")
         # For filesystems where the mtime is only to a second granularity,
         # everything that has happened above can be too fast;
         # force an mtime on the source that is guaranteed to be different
         # than the original mtime.
         loader.path_mtime = self.fake_mtime(loader.path_mtime)
         module = loader.load_module('_temp')
         self.assertTrue(
             'testing_var' in module.__dict__, "'testing_var' not in "
             "{0}".format(list(module.__dict__.keys())))
         self.assertEqual(module, sys.modules['_temp'])
         self.assertEqual(id(module), module_id)
         self.assertEqual(id(module.__dict__), module_dict_id)
예제 #16
0
 def test_timestamp_overflow(self):
     # When a modification timestamp is larger than 2**32, it should be
     # truncated rather than raise an OverflowError.
     with source_util.create_modules('_temp') as mapping:
         source = mapping['_temp']
         compiled = imp.cache_from_source(source)
         with open(source, 'w') as f:
             f.write("x = 5")
         try:
             os.utime(source, (2 ** 33 - 5, 2 ** 33 - 5))
         except OverflowError:
             self.skipTest("cannot set modification time to large integer")
         except OSError as e:
             if e.errno != getattr(errno, 'EOVERFLOW', None):
                 raise
             self.skipTest("cannot set modification time to large integer ({})".format(e))
         loader = _bootstrap._SourceFileLoader('_temp', mapping['_temp'])
         mod = loader.load_module('_temp')
         # Sanity checks.
         self.assertEqual(mod.__cached__, compiled)
         self.assertEqual(mod.x, 5)
         # The pyc file was created.
         os.stat(compiled)