| OLD | NEW |
| 1 from importlib import machinery | 1 from importlib import machinery |
| 2 import imp | 2 import imp |
| 3 import unittest | 3 import unittest |
| 4 from .. import abc | 4 from .. import abc |
| 5 from .. import util | 5 from .. import util |
| 6 from test.support import captured_stdout | 6 from test.support import captured_stdout |
| 7 | 7 |
| 8 class LoaderTests(abc.LoaderTests): | 8 class LoaderTests(abc.LoaderTests): |
| 9 | 9 |
| 10 def test_module(self): | 10 def test_module(self): |
| 11 with util.uncache('__hello__'), captured_stdout() as stdout: | 11 with util.uncache('__hello__'), captured_stdout() as stdout: |
| 12 module = machinery.FrozenImporter.load_module('__hello__') | 12 module = machinery.FrozenImporter.load_module('__hello__') |
| 13 check = {'__name__': '__hello__', | 13 check = {'__name__': '__hello__', '__file__': '<frozen>', |
| 14 '__package__': '', | 14 '__package__': '', '__loader__': machinery.FrozenImporter} |
| 15 '__loader__': machinery.FrozenImporter, | |
| 16 } | |
| 17 for attr, value in check.items(): | 15 for attr, value in check.items(): |
| 18 self.assertEqual(getattr(module, attr), value) | 16 self.assertEqual(getattr(module, attr), value) |
| 19 self.assertEqual(stdout.getvalue(), 'Hello world!\n') | 17 self.assertEqual(stdout.getvalue(), 'Hello world!\n') |
| 20 self.assertFalse(hasattr(module, '__file__')) | |
| 21 | 18 |
| 22 def test_package(self): | 19 def test_package(self): |
| 23 with util.uncache('__phello__'), captured_stdout() as stdout: | 20 with util.uncache('__phello__'), captured_stdout() as stdout: |
| 24 module = machinery.FrozenImporter.load_module('__phello__') | 21 module = machinery.FrozenImporter.load_module('__phello__') |
| 25 check = {'__name__': '__phello__', | 22 check = {'__name__': '__phello__', '__file__': '<frozen>', |
| 26 '__package__': '__phello__', | 23 '__package__': '__phello__', '__path__': ['__phello__'], |
| 27 '__path__': ['__phello__'], | 24 '__loader__': machinery.FrozenImporter} |
| 28 '__loader__': machinery.FrozenImporter, | |
| 29 } | |
| 30 for attr, value in check.items(): | 25 for attr, value in check.items(): |
| 31 attr_value = getattr(module, attr) | 26 attr_value = getattr(module, attr) |
| 32 self.assertEqual(attr_value, value, | 27 self.assertEqual(attr_value, value, |
| 33 "for __phello__.%s, %r != %r" % | 28 "for __phello__.%s, %r != %r" % |
| 34 (attr, attr_value, value)) | 29 (attr, attr_value, value)) |
| 35 self.assertEqual(stdout.getvalue(), 'Hello world!\n') | 30 self.assertEqual(stdout.getvalue(), 'Hello world!\n') |
| 36 self.assertFalse(hasattr(module, '__file__')) | |
| 37 | 31 |
| 38 def test_lacking_parent(self): | 32 def test_lacking_parent(self): |
| 39 with util.uncache('__phello__', '__phello__.spam'), \ | 33 with util.uncache('__phello__', '__phello__.spam'), \ |
| 40 captured_stdout() as stdout: | 34 captured_stdout() as stdout: |
| 41 module = machinery.FrozenImporter.load_module('__phello__.spam') | 35 module = machinery.FrozenImporter.load_module('__phello__.spam') |
| 42 check = {'__name__': '__phello__.spam', | 36 check = {'__name__': '__phello__.spam', '__file__': '<frozen>', |
| 43 '__package__': '__phello__', | 37 '__package__': '__phello__', |
| 44 '__loader__': machinery.FrozenImporter, | 38 '__loader__': machinery.FrozenImporter} |
| 45 } | |
| 46 for attr, value in check.items(): | 39 for attr, value in check.items(): |
| 47 attr_value = getattr(module, attr) | 40 attr_value = getattr(module, attr) |
| 48 self.assertEqual(attr_value, value, | 41 self.assertEqual(attr_value, value, |
| 49 "for __phello__.spam.%s, %r != %r" % | 42 "for __phello__.spam.%s, %r != %r" % |
| 50 (attr, attr_value, value)) | 43 (attr, attr_value, value)) |
| 51 self.assertEqual(stdout.getvalue(), 'Hello world!\n') | 44 self.assertEqual(stdout.getvalue(), 'Hello world!\n') |
| 52 self.assertFalse(hasattr(module, '__file__')) | |
| 53 | 45 |
| 54 def test_module_reuse(self): | 46 def test_module_reuse(self): |
| 55 with util.uncache('__hello__'), captured_stdout() as stdout: | 47 with util.uncache('__hello__'), captured_stdout() as stdout: |
| 56 module1 = machinery.FrozenImporter.load_module('__hello__') | 48 module1 = machinery.FrozenImporter.load_module('__hello__') |
| 57 module2 = machinery.FrozenImporter.load_module('__hello__') | 49 module2 = machinery.FrozenImporter.load_module('__hello__') |
| 58 self.assertTrue(module1 is module2) | 50 self.assertTrue(module1 is module2) |
| 59 self.assertEqual(stdout.getvalue(), | 51 self.assertEqual(stdout.getvalue(), |
| 60 'Hello world!\nHello world!\n') | 52 'Hello world!\nHello world!\n') |
| 61 | |
| 62 def test_module_repr(self): | |
| 63 with util.uncache('__hello__'), captured_stdout(): | |
| 64 module = machinery.FrozenImporter.load_module('__hello__') | |
| 65 self.assertEqual(repr(module), | |
| 66 "<module '__hello__' (frozen)>") | |
| 67 | 53 |
| 68 def test_state_after_failure(self): | 54 def test_state_after_failure(self): |
| 69 # No way to trigger an error in a frozen module. | 55 # No way to trigger an error in a frozen module. |
| 70 pass | 56 pass |
| 71 | 57 |
| 72 def test_unloadable(self): | 58 def test_unloadable(self): |
| 73 assert machinery.FrozenImporter.find_module('_not_real') is None | 59 assert machinery.FrozenImporter.find_module('_not_real') is None |
| 74 with self.assertRaises(ImportError) as cm: | 60 with self.assertRaises(ImportError) as cm: |
| 75 machinery.FrozenImporter.load_module('_not_real') | 61 machinery.FrozenImporter.load_module('_not_real') |
| 76 self.assertEqual(cm.exception.name, '_not_real') | 62 self.assertEqual(cm.exception.name, '_not_real') |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 112 self.assertEqual(cm.exception.name, 'importlib') | 98 self.assertEqual(cm.exception.name, 'importlib') |
| 113 | 99 |
| 114 | 100 |
| 115 def test_main(): | 101 def test_main(): |
| 116 from test.support import run_unittest | 102 from test.support import run_unittest |
| 117 run_unittest(LoaderTests, InspectLoaderTests) | 103 run_unittest(LoaderTests, InspectLoaderTests) |
| 118 | 104 |
| 119 | 105 |
| 120 if __name__ == '__main__': | 106 if __name__ == '__main__': |
| 121 test_main() | 107 test_main() |
| OLD | NEW |