def run_test(self, source):
     with source_util.create_modules(self.module_name) as mapping:
         with open(mapping[self.module_name], u'wb') as file:
             file.write(source)
         loader = _bootstrap._SourceFileLoader(self.module_name,
                                    mapping[self.module_name])
         return loader.load_module(self.module_name)
Exemplo n.º 2
0
 def test_bad_syntax(self):
     with source_util.create_modules(u'_temp') as mapping:
         with open(mapping[u'_temp'], u'w') as file:
             file.write(u'=')
         loader = _bootstrap._SourceFileLoader(u'_temp', mapping[u'_temp'])
         with self.assertRaises(SyntaxError):
             loader.load_module(u'_temp')
         self.assertTrue(u'_temp' not in sys.modules)
Exemplo n.º 3
0
 def test_module(self):
     with source_util.create_modules(u'_temp') as mapping:
         loader = _bootstrap._SourceFileLoader(u'_temp', mapping[u'_temp'])
         module = loader.load_module(u'_temp')
         self.assertTrue(u'_temp' in sys.modules)
         check = {u'__name__': u'_temp', u'__file__': mapping[u'_temp'],
                  u'__package__': u''}
         for attr, value in check.items():
             self.assertEqual(getattr(module, attr), value)
 def run_test(self, line_ending):
     module_name = u'_temp'
     source_lines = ["a = 42", "b = -13", '']
     source = line_ending.join(source_lines)
     with source_util.create_modules(module_name) as mapping:
         with open(mapping[module_name], u'wb') as file:
             file.write(source)
         loader = _bootstrap._SourceFileLoader(module_name,
                                              mapping[module_name])
         return loader.load_module(module_name)
Exemplo n.º 5
0
 def test_state_after_failure(self):
     # A failed reload should leave the original module intact.
     attributes = (u'__file__', u'__path__', u'__package__')
     value = u'<test>'
     name = u'_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], u'w') as file:
             file.write(u'+++ bad syntax +++')
         loader = _bootstrap._SourceFileLoader(u'_temp', mapping[u'_temp'])
         with self.assertRaises(SyntaxError):
             loader.load_module(name)
         for attr in attributes:
             self.assertEqual(getattr(orig_module, attr), value)
Exemplo n.º 6
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 = u'_temp.py'
     with open(file_path, u'w') as file:
         file.write(u"# test file for importlib_full")
     try:
         with util.uncache(u'_temp'):
             loader = _bootstrap._SourceFileLoader(u'_temp', file_path)
             mod = loader.load_module(u'_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)
Exemplo n.º 7
0
 def test_module_reuse(self):
     with source_util.create_modules(u'_temp') as mapping:
         loader = _bootstrap._SourceFileLoader(u'_temp', mapping[u'_temp'])
         module = loader.load_module(u'_temp')
         module_id = id(module)
         module_dict_id = id(module.__dict__)
         with open(mapping[u'_temp'], u'w') as file:
             file.write(u"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(u'_temp')
         self.assertTrue(u'testing_var' in module.__dict__,
                      u"'testing_var' not in "
                         u"{0}".format(list(module.__dict__.keys())))
         self.assertEqual(module, sys.modules[u'_temp'])
         self.assertEqual(id(module), module_id)
         self.assertEqual(id(module.__dict__), module_dict_id)