| 
									
										
										
										
											2009-02-01 01:34:13 +00:00
										 |  |  | from .. import abc | 
					
						
							| 
									
										
										
										
											2009-02-01 04:00:05 +00:00
										 |  |  | from .. import util | 
					
						
							| 
									
										
										
										
											2013-06-15 18:39:21 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-08 11:10:41 -05:00
										 |  |  | machinery = util.import_importlib('importlib.machinery') | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-16 16:57:18 +02:00
										 |  |  | from test.support import captured_stdout | 
					
						
							| 
									
										
										
										
											2013-06-15 18:39:21 -04:00
										 |  |  | import types | 
					
						
							| 
									
										
										
										
											2013-11-22 09:05:39 -07:00
										 |  |  | import unittest | 
					
						
							| 
									
										
										
										
											2014-01-06 20:49:04 -07:00
										 |  |  | import warnings | 
					
						
							| 
									
										
										
										
											2013-11-22 09:05:39 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class ExecModuleTests(abc.LoaderTests): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def exec_module(self, name): | 
					
						
							|  |  |  |         with util.uncache(name), captured_stdout() as stdout: | 
					
						
							|  |  |  |             spec = self.machinery.ModuleSpec( | 
					
						
							|  |  |  |                     name, self.machinery.FrozenImporter, origin='frozen', | 
					
						
							|  |  |  |                     is_package=self.machinery.FrozenImporter.is_package(name)) | 
					
						
							|  |  |  |             module = types.ModuleType(name) | 
					
						
							|  |  |  |             module.__spec__ = spec | 
					
						
							|  |  |  |             assert not hasattr(module, 'initialized') | 
					
						
							|  |  |  |             self.machinery.FrozenImporter.exec_module(module) | 
					
						
							|  |  |  |             self.assertTrue(module.initialized) | 
					
						
							|  |  |  |             self.assertTrue(hasattr(module, '__spec__')) | 
					
						
							|  |  |  |             self.assertEqual(module.__spec__.origin, 'frozen') | 
					
						
							|  |  |  |             return module, stdout.getvalue() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_module(self): | 
					
						
							|  |  |  |         name = '__hello__' | 
					
						
							|  |  |  |         module, output = self.exec_module(name) | 
					
						
							|  |  |  |         check = {'__name__': name} | 
					
						
							|  |  |  |         for attr, value in check.items(): | 
					
						
							|  |  |  |             self.assertEqual(getattr(module, attr), value) | 
					
						
							|  |  |  |         self.assertEqual(output, 'Hello world!\n') | 
					
						
							|  |  |  |         self.assertTrue(hasattr(module, '__spec__')) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_package(self): | 
					
						
							|  |  |  |         name = '__phello__' | 
					
						
							|  |  |  |         module, output = self.exec_module(name) | 
					
						
							|  |  |  |         check = {'__name__': name} | 
					
						
							|  |  |  |         for attr, value in check.items(): | 
					
						
							|  |  |  |             attr_value = getattr(module, attr) | 
					
						
							|  |  |  |             self.assertEqual(attr_value, value, | 
					
						
							|  |  |  |                         'for {name}.{attr}, {given!r} != {expected!r}'.format( | 
					
						
							|  |  |  |                                  name=name, attr=attr, given=attr_value, | 
					
						
							|  |  |  |                                  expected=value)) | 
					
						
							|  |  |  |         self.assertEqual(output, 'Hello world!\n') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_lacking_parent(self): | 
					
						
							|  |  |  |         name = '__phello__.spam' | 
					
						
							|  |  |  |         with util.uncache('__phello__'): | 
					
						
							|  |  |  |             module, output = self.exec_module(name) | 
					
						
							|  |  |  |             check = {'__name__': name} | 
					
						
							|  |  |  |             for attr, value in check.items(): | 
					
						
							|  |  |  |                 attr_value = getattr(module, attr) | 
					
						
							|  |  |  |                 self.assertEqual(attr_value, value, | 
					
						
							|  |  |  |                         'for {name}.{attr}, {given} != {expected!r}'.format( | 
					
						
							|  |  |  |                                  name=name, attr=attr, given=attr_value, | 
					
						
							|  |  |  |                                  expected=value)) | 
					
						
							|  |  |  |             self.assertEqual(output, 'Hello world!\n') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_module_repr(self): | 
					
						
							| 
									
										
										
										
											2014-01-06 20:49:04 -07:00
										 |  |  |         name = '__hello__' | 
					
						
							|  |  |  |         module, output = self.exec_module(name) | 
					
						
							|  |  |  |         with warnings.catch_warnings(): | 
					
						
							|  |  |  |             warnings.simplefilter('ignore', DeprecationWarning) | 
					
						
							|  |  |  |             repr_str = self.machinery.FrozenImporter.module_repr(module) | 
					
						
							|  |  |  |         self.assertEqual(repr_str, | 
					
						
							|  |  |  |                          "<module '__hello__' (frozen)>") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_module_repr_indirect(self): | 
					
						
							| 
									
										
										
										
											2013-11-22 09:05:39 -07:00
										 |  |  |         name = '__hello__' | 
					
						
							|  |  |  |         module, output = self.exec_module(name) | 
					
						
							|  |  |  |         self.assertEqual(repr(module), | 
					
						
							|  |  |  |                          "<module '__hello__' (frozen)>") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # No way to trigger an error in a frozen module. | 
					
						
							|  |  |  |     test_state_after_failure = None | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_unloadable(self): | 
					
						
							|  |  |  |         assert self.machinery.FrozenImporter.find_module('_not_real') is None | 
					
						
							|  |  |  |         with self.assertRaises(ImportError) as cm: | 
					
						
							|  |  |  |             self.exec_module('_not_real') | 
					
						
							|  |  |  |         self.assertEqual(cm.exception.name, '_not_real') | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-16 11:40:40 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | (Frozen_ExecModuleTests, | 
					
						
							|  |  |  |  Source_ExecModuleTests | 
					
						
							|  |  |  |  ) = util.test_both(ExecModuleTests, machinery=machinery) | 
					
						
							| 
									
										
										
										
											2013-06-15 18:39:21 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-01 01:34:13 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-08 11:10:41 -05:00
										 |  |  | class LoaderTests(abc.LoaderTests): | 
					
						
							| 
									
										
										
										
											2009-02-01 01:34:13 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_module(self): | 
					
						
							| 
									
										
										
										
											2011-05-16 16:57:18 +02:00
										 |  |  |         with util.uncache('__hello__'), captured_stdout() as stdout: | 
					
						
							| 
									
										
										
										
											2014-01-06 20:49:04 -07:00
										 |  |  |             with warnings.catch_warnings(): | 
					
						
							|  |  |  |                 warnings.simplefilter('ignore', DeprecationWarning) | 
					
						
							|  |  |  |                 module = self.machinery.FrozenImporter.load_module('__hello__') | 
					
						
							| 
									
										
										
										
											2012-05-24 20:21:04 -04:00
										 |  |  |             check = {'__name__': '__hello__', | 
					
						
							| 
									
										
										
										
											2012-05-24 20:22:10 -04:00
										 |  |  |                     '__package__': '', | 
					
						
							| 
									
										
										
										
											2013-11-08 11:10:41 -05:00
										 |  |  |                     '__loader__': self.machinery.FrozenImporter, | 
					
						
							| 
									
										
										
										
											2012-05-24 20:21:04 -04:00
										 |  |  |                     } | 
					
						
							| 
									
										
										
										
											2009-02-01 01:34:13 +00:00
										 |  |  |             for attr, value in check.items(): | 
					
						
							|  |  |  |                 self.assertEqual(getattr(module, attr), value) | 
					
						
							| 
									
										
										
										
											2011-05-16 16:57:18 +02:00
										 |  |  |             self.assertEqual(stdout.getvalue(), 'Hello world!\n') | 
					
						
							| 
									
										
										
										
											2012-05-24 20:21:04 -04:00
										 |  |  |             self.assertFalse(hasattr(module, '__file__')) | 
					
						
							| 
									
										
										
										
											2009-02-01 01:34:13 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_package(self): | 
					
						
							| 
									
										
										
										
											2011-05-16 16:57:18 +02:00
										 |  |  |         with util.uncache('__phello__'),  captured_stdout() as stdout: | 
					
						
							| 
									
										
										
										
											2014-01-06 20:49:04 -07:00
										 |  |  |             with warnings.catch_warnings(): | 
					
						
							|  |  |  |                 warnings.simplefilter('ignore', DeprecationWarning) | 
					
						
							|  |  |  |                 module = self.machinery.FrozenImporter.load_module('__phello__') | 
					
						
							| 
									
										
										
										
											2012-05-24 20:22:10 -04:00
										 |  |  |             check = {'__name__': '__phello__', | 
					
						
							|  |  |  |                      '__package__': '__phello__', | 
					
						
							| 
									
										
										
										
											2013-05-31 23:18:39 -04:00
										 |  |  |                      '__path__': [], | 
					
						
							| 
									
										
										
										
											2013-11-08 11:10:41 -05:00
										 |  |  |                      '__loader__': self.machinery.FrozenImporter, | 
					
						
							| 
									
										
										
										
											2012-05-24 20:21:04 -04:00
										 |  |  |                      } | 
					
						
							| 
									
										
										
										
											2009-02-01 01:34:13 +00:00
										 |  |  |             for attr, value in check.items(): | 
					
						
							|  |  |  |                 attr_value = getattr(module, attr) | 
					
						
							|  |  |  |                 self.assertEqual(attr_value, value, | 
					
						
							|  |  |  |                                  "for __phello__.%s, %r != %r" % | 
					
						
							|  |  |  |                                  (attr, attr_value, value)) | 
					
						
							| 
									
										
										
										
											2011-05-16 16:57:18 +02:00
										 |  |  |             self.assertEqual(stdout.getvalue(), 'Hello world!\n') | 
					
						
							| 
									
										
										
										
											2012-05-24 20:21:04 -04:00
										 |  |  |             self.assertFalse(hasattr(module, '__file__')) | 
					
						
							| 
									
										
										
										
											2009-02-01 01:34:13 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_lacking_parent(self): | 
					
						
							| 
									
										
										
										
											2011-05-16 16:57:18 +02:00
										 |  |  |         with util.uncache('__phello__', '__phello__.spam'), \ | 
					
						
							|  |  |  |              captured_stdout() as stdout: | 
					
						
							| 
									
										
										
										
											2014-01-06 20:49:04 -07:00
										 |  |  |             with warnings.catch_warnings(): | 
					
						
							|  |  |  |                 warnings.simplefilter('ignore', DeprecationWarning) | 
					
						
							|  |  |  |                 module = self.machinery.FrozenImporter.load_module('__phello__.spam') | 
					
						
							| 
									
										
										
										
											2012-05-24 20:21:04 -04:00
										 |  |  |             check = {'__name__': '__phello__.spam', | 
					
						
							| 
									
										
										
										
											2009-03-10 05:17:37 +00:00
										 |  |  |                     '__package__': '__phello__', | 
					
						
							| 
									
										
										
										
											2013-11-08 11:10:41 -05:00
										 |  |  |                     '__loader__': self.machinery.FrozenImporter, | 
					
						
							| 
									
										
										
										
											2012-05-24 20:21:04 -04:00
										 |  |  |                     } | 
					
						
							| 
									
										
										
										
											2009-02-01 01:34:13 +00:00
										 |  |  |             for attr, value in check.items(): | 
					
						
							|  |  |  |                 attr_value = getattr(module, attr) | 
					
						
							|  |  |  |                 self.assertEqual(attr_value, value, | 
					
						
							|  |  |  |                                  "for __phello__.spam.%s, %r != %r" % | 
					
						
							|  |  |  |                                  (attr, attr_value, value)) | 
					
						
							| 
									
										
										
										
											2011-05-16 16:57:18 +02:00
										 |  |  |             self.assertEqual(stdout.getvalue(), 'Hello world!\n') | 
					
						
							| 
									
										
										
										
											2012-05-24 20:21:04 -04:00
										 |  |  |             self.assertFalse(hasattr(module, '__file__')) | 
					
						
							| 
									
										
										
										
											2009-02-01 01:34:13 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_module_reuse(self): | 
					
						
							| 
									
										
										
										
											2011-05-16 16:57:18 +02:00
										 |  |  |         with util.uncache('__hello__'), captured_stdout() as stdout: | 
					
						
							| 
									
										
										
										
											2014-01-06 20:49:04 -07:00
										 |  |  |             with warnings.catch_warnings(): | 
					
						
							|  |  |  |                 warnings.simplefilter('ignore', DeprecationWarning) | 
					
						
							|  |  |  |                 module1 = self.machinery.FrozenImporter.load_module('__hello__') | 
					
						
							|  |  |  |                 module2 = self.machinery.FrozenImporter.load_module('__hello__') | 
					
						
							| 
									
										
										
										
											2012-06-27 15:26:26 -04:00
										 |  |  |             self.assertIs(module1, module2) | 
					
						
							| 
									
										
										
										
											2011-05-16 16:57:18 +02:00
										 |  |  |             self.assertEqual(stdout.getvalue(), | 
					
						
							|  |  |  |                              'Hello world!\nHello world!\n') | 
					
						
							| 
									
										
										
										
											2009-02-01 01:34:13 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-24 20:21:04 -04:00
										 |  |  |     def test_module_repr(self): | 
					
						
							|  |  |  |         with util.uncache('__hello__'), captured_stdout(): | 
					
						
							| 
									
										
										
										
											2014-01-06 20:49:04 -07:00
										 |  |  |             with warnings.catch_warnings(): | 
					
						
							|  |  |  |                 warnings.simplefilter('ignore', DeprecationWarning) | 
					
						
							|  |  |  |                 module = self.machinery.FrozenImporter.load_module('__hello__') | 
					
						
							|  |  |  |                 repr_str = self.machinery.FrozenImporter.module_repr(module) | 
					
						
							|  |  |  |             self.assertEqual(repr_str, | 
					
						
							| 
									
										
										
										
											2012-05-24 20:21:04 -04:00
										 |  |  |                              "<module '__hello__' (frozen)>") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-06 20:49:04 -07:00
										 |  |  |     def test_module_repr_indirect(self): | 
					
						
							|  |  |  |         with util.uncache('__hello__'), captured_stdout(): | 
					
						
							|  |  |  |             module = self.machinery.FrozenImporter.load_module('__hello__') | 
					
						
							|  |  |  |         self.assertEqual(repr(module), | 
					
						
							|  |  |  |                          "<module '__hello__' (frozen)>") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-22 09:05:39 -07:00
										 |  |  |     # No way to trigger an error in a frozen module. | 
					
						
							|  |  |  |     test_state_after_failure = None | 
					
						
							| 
									
										
										
										
											2009-02-01 01:34:13 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_unloadable(self): | 
					
						
							| 
									
										
										
										
											2013-11-08 11:10:41 -05:00
										 |  |  |         assert self.machinery.FrozenImporter.find_module('_not_real') is None | 
					
						
							| 
									
										
										
										
											2012-04-12 21:09:01 -04:00
										 |  |  |         with self.assertRaises(ImportError) as cm: | 
					
						
							| 
									
										
										
										
											2013-11-08 11:10:41 -05:00
										 |  |  |             self.machinery.FrozenImporter.load_module('_not_real') | 
					
						
							| 
									
										
										
										
											2012-04-12 21:09:01 -04:00
										 |  |  |         self.assertEqual(cm.exception.name, '_not_real') | 
					
						
							| 
									
										
										
										
											2009-01-18 00:24:28 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-16 11:40:40 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | (Frozen_LoaderTests, | 
					
						
							|  |  |  |  Source_LoaderTests | 
					
						
							|  |  |  |  ) = util.test_both(LoaderTests, machinery=machinery) | 
					
						
							| 
									
										
										
										
											2013-11-08 11:10:41 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-18 00:24:28 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-08 11:10:41 -05:00
										 |  |  | class InspectLoaderTests: | 
					
						
							| 
									
										
										
										
											2009-03-15 02:20:16 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     """Tests for the InspectLoader methods for FrozenImporter.""" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_get_code(self): | 
					
						
							|  |  |  |         # Make sure that the code object is good. | 
					
						
							|  |  |  |         name = '__hello__' | 
					
						
							| 
									
										
										
										
											2011-05-16 16:57:18 +02:00
										 |  |  |         with captured_stdout() as stdout: | 
					
						
							| 
									
										
										
										
											2013-11-08 11:10:41 -05:00
										 |  |  |             code = self.machinery.FrozenImporter.get_code(name) | 
					
						
							| 
									
										
										
										
											2013-06-15 18:39:21 -04:00
										 |  |  |             mod = types.ModuleType(name) | 
					
						
							| 
									
										
										
										
											2011-05-16 16:57:18 +02:00
										 |  |  |             exec(code, mod.__dict__) | 
					
						
							|  |  |  |             self.assertTrue(hasattr(mod, 'initialized')) | 
					
						
							|  |  |  |             self.assertEqual(stdout.getvalue(), 'Hello world!\n') | 
					
						
							| 
									
										
										
										
											2009-03-15 02:20:16 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_get_source(self): | 
					
						
							|  |  |  |         # Should always return None. | 
					
						
							| 
									
										
										
										
											2013-11-08 11:10:41 -05:00
										 |  |  |         result = self.machinery.FrozenImporter.get_source('__hello__') | 
					
						
							| 
									
										
										
										
											2012-06-28 06:15:01 -04:00
										 |  |  |         self.assertIsNone(result) | 
					
						
							| 
									
										
										
										
											2009-03-15 02:20:16 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_is_package(self): | 
					
						
							|  |  |  |         # Should be able to tell what is a package. | 
					
						
							|  |  |  |         test_for = (('__hello__', False), ('__phello__', True), | 
					
						
							|  |  |  |                     ('__phello__.spam', False)) | 
					
						
							|  |  |  |         for name, is_package in test_for: | 
					
						
							| 
									
										
										
										
											2013-11-08 11:10:41 -05:00
										 |  |  |             result = self.machinery.FrozenImporter.is_package(name) | 
					
						
							| 
									
										
										
										
											2012-06-27 15:26:26 -04:00
										 |  |  |             self.assertEqual(bool(result), is_package) | 
					
						
							| 
									
										
										
										
											2009-03-15 02:20:16 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_failure(self): | 
					
						
							|  |  |  |         # Raise ImportError for modules that are not frozen. | 
					
						
							|  |  |  |         for meth_name in ('get_code', 'get_source', 'is_package'): | 
					
						
							| 
									
										
										
										
											2013-11-08 11:10:41 -05:00
										 |  |  |             method = getattr(self.machinery.FrozenImporter, meth_name) | 
					
						
							| 
									
										
										
										
											2012-04-12 21:09:01 -04:00
										 |  |  |             with self.assertRaises(ImportError) as cm: | 
					
						
							| 
									
										
										
										
											2009-08-27 23:49:21 +00:00
										 |  |  |                 method('importlib') | 
					
						
							| 
									
										
										
										
											2012-04-12 21:09:01 -04:00
										 |  |  |             self.assertEqual(cm.exception.name, 'importlib') | 
					
						
							| 
									
										
										
										
											2009-03-15 02:20:16 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-16 11:40:40 -06:00
										 |  |  | (Frozen_ILTests, | 
					
						
							|  |  |  |  Source_ILTests | 
					
						
							|  |  |  |  ) = util.test_both(InspectLoaderTests, machinery=machinery) | 
					
						
							| 
									
										
										
										
											2009-01-18 00:24:28 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if __name__ == '__main__': | 
					
						
							| 
									
										
										
										
											2013-11-08 11:10:41 -05:00
										 |  |  |     unittest.main() |