| 1 | n/a | from importlib import machinery |
|---|
| 2 | n/a | import imp |
|---|
| 3 | n/a | import unittest |
|---|
| 4 | n/a | from .. import abc |
|---|
| 5 | n/a | from .. import util |
|---|
| 6 | n/a | from test.support import captured_stdout |
|---|
| 7 | n/a | |
|---|
| 8 | n/a | class LoaderTests(abc.LoaderTests): |
|---|
| 9 | n/a | |
|---|
| 10 | n/a | def test_module(self): |
|---|
| 11 | n/a | with util.uncache('__hello__'), captured_stdout() as stdout: |
|---|
| 12 | n/a | module = machinery.FrozenImporter.load_module('__hello__') |
|---|
| 13 | n/a | check = {'__name__': '__hello__', |
|---|
| 14 | n/a | '__package__': '', |
|---|
| 15 | n/a | '__loader__': machinery.FrozenImporter, |
|---|
| 16 | n/a | } |
|---|
| 17 | n/a | for attr, value in check.items(): |
|---|
| 18 | n/a | self.assertEqual(getattr(module, attr), value) |
|---|
| 19 | n/a | self.assertEqual(stdout.getvalue(), 'Hello world!\n') |
|---|
| 20 | n/a | self.assertFalse(hasattr(module, '__file__')) |
|---|
| 21 | n/a | |
|---|
| 22 | n/a | def test_package(self): |
|---|
| 23 | n/a | with util.uncache('__phello__'), captured_stdout() as stdout: |
|---|
| 24 | n/a | module = machinery.FrozenImporter.load_module('__phello__') |
|---|
| 25 | n/a | check = {'__name__': '__phello__', |
|---|
| 26 | n/a | '__package__': '__phello__', |
|---|
| 27 | n/a | '__path__': ['__phello__'], |
|---|
| 28 | n/a | '__loader__': machinery.FrozenImporter, |
|---|
| 29 | n/a | } |
|---|
| 30 | n/a | for attr, value in check.items(): |
|---|
| 31 | n/a | attr_value = getattr(module, attr) |
|---|
| 32 | n/a | self.assertEqual(attr_value, value, |
|---|
| 33 | n/a | "for __phello__.%s, %r != %r" % |
|---|
| 34 | n/a | (attr, attr_value, value)) |
|---|
| 35 | n/a | self.assertEqual(stdout.getvalue(), 'Hello world!\n') |
|---|
| 36 | n/a | self.assertFalse(hasattr(module, '__file__')) |
|---|
| 37 | n/a | |
|---|
| 38 | n/a | def test_lacking_parent(self): |
|---|
| 39 | n/a | with util.uncache('__phello__', '__phello__.spam'), \ |
|---|
| 40 | n/a | captured_stdout() as stdout: |
|---|
| 41 | n/a | module = machinery.FrozenImporter.load_module('__phello__.spam') |
|---|
| 42 | n/a | check = {'__name__': '__phello__.spam', |
|---|
| 43 | n/a | '__package__': '__phello__', |
|---|
| 44 | n/a | '__loader__': machinery.FrozenImporter, |
|---|
| 45 | n/a | } |
|---|
| 46 | n/a | for attr, value in check.items(): |
|---|
| 47 | n/a | attr_value = getattr(module, attr) |
|---|
| 48 | n/a | self.assertEqual(attr_value, value, |
|---|
| 49 | n/a | "for __phello__.spam.%s, %r != %r" % |
|---|
| 50 | n/a | (attr, attr_value, value)) |
|---|
| 51 | n/a | self.assertEqual(stdout.getvalue(), 'Hello world!\n') |
|---|
| 52 | n/a | self.assertFalse(hasattr(module, '__file__')) |
|---|
| 53 | n/a | |
|---|
| 54 | n/a | def test_module_reuse(self): |
|---|
| 55 | n/a | with util.uncache('__hello__'), captured_stdout() as stdout: |
|---|
| 56 | n/a | module1 = machinery.FrozenImporter.load_module('__hello__') |
|---|
| 57 | n/a | module2 = machinery.FrozenImporter.load_module('__hello__') |
|---|
| 58 | n/a | self.assertIs(module1, module2) |
|---|
| 59 | n/a | self.assertEqual(stdout.getvalue(), |
|---|
| 60 | n/a | 'Hello world!\nHello world!\n') |
|---|
| 61 | n/a | |
|---|
| 62 | n/a | def test_module_repr(self): |
|---|
| 63 | n/a | with util.uncache('__hello__'), captured_stdout(): |
|---|
| 64 | n/a | module = machinery.FrozenImporter.load_module('__hello__') |
|---|
| 65 | n/a | self.assertEqual(repr(module), |
|---|
| 66 | n/a | "<module '__hello__' (frozen)>") |
|---|
| 67 | n/a | |
|---|
| 68 | n/a | def test_state_after_failure(self): |
|---|
| 69 | n/a | # No way to trigger an error in a frozen module. |
|---|
| 70 | n/a | pass |
|---|
| 71 | n/a | |
|---|
| 72 | n/a | def test_unloadable(self): |
|---|
| 73 | n/a | assert machinery.FrozenImporter.find_module('_not_real') is None |
|---|
| 74 | n/a | with self.assertRaises(ImportError) as cm: |
|---|
| 75 | n/a | machinery.FrozenImporter.load_module('_not_real') |
|---|
| 76 | n/a | self.assertEqual(cm.exception.name, '_not_real') |
|---|
| 77 | n/a | |
|---|
| 78 | n/a | |
|---|
| 79 | n/a | class InspectLoaderTests(unittest.TestCase): |
|---|
| 80 | n/a | |
|---|
| 81 | n/a | """Tests for the InspectLoader methods for FrozenImporter.""" |
|---|
| 82 | n/a | |
|---|
| 83 | n/a | def test_get_code(self): |
|---|
| 84 | n/a | # Make sure that the code object is good. |
|---|
| 85 | n/a | name = '__hello__' |
|---|
| 86 | n/a | with captured_stdout() as stdout: |
|---|
| 87 | n/a | code = machinery.FrozenImporter.get_code(name) |
|---|
| 88 | n/a | mod = imp.new_module(name) |
|---|
| 89 | n/a | exec(code, mod.__dict__) |
|---|
| 90 | n/a | self.assertTrue(hasattr(mod, 'initialized')) |
|---|
| 91 | n/a | self.assertEqual(stdout.getvalue(), 'Hello world!\n') |
|---|
| 92 | n/a | |
|---|
| 93 | n/a | def test_get_source(self): |
|---|
| 94 | n/a | # Should always return None. |
|---|
| 95 | n/a | result = machinery.FrozenImporter.get_source('__hello__') |
|---|
| 96 | n/a | self.assertIsNone(result) |
|---|
| 97 | n/a | |
|---|
| 98 | n/a | def test_is_package(self): |
|---|
| 99 | n/a | # Should be able to tell what is a package. |
|---|
| 100 | n/a | test_for = (('__hello__', False), ('__phello__', True), |
|---|
| 101 | n/a | ('__phello__.spam', False)) |
|---|
| 102 | n/a | for name, is_package in test_for: |
|---|
| 103 | n/a | result = machinery.FrozenImporter.is_package(name) |
|---|
| 104 | n/a | self.assertEqual(bool(result), is_package) |
|---|
| 105 | n/a | |
|---|
| 106 | n/a | def test_failure(self): |
|---|
| 107 | n/a | # Raise ImportError for modules that are not frozen. |
|---|
| 108 | n/a | for meth_name in ('get_code', 'get_source', 'is_package'): |
|---|
| 109 | n/a | method = getattr(machinery.FrozenImporter, meth_name) |
|---|
| 110 | n/a | with self.assertRaises(ImportError) as cm: |
|---|
| 111 | n/a | method('importlib') |
|---|
| 112 | n/a | self.assertEqual(cm.exception.name, 'importlib') |
|---|
| 113 | n/a | |
|---|
| 114 | n/a | |
|---|
| 115 | n/a | def test_main(): |
|---|
| 116 | n/a | from test.support import run_unittest |
|---|
| 117 | n/a | run_unittest(LoaderTests, InspectLoaderTests) |
|---|
| 118 | n/a | |
|---|
| 119 | n/a | |
|---|
| 120 | n/a | if __name__ == '__main__': |
|---|
| 121 | n/a | test_main() |
|---|