blob: 7c0146456d2e9b25194750e9eb4635d35c33dc6d [file] [log] [blame]
Brett Cannon223a19d2009-02-01 01:34:13 +00001from .. import abc
Brett Cannonbcb26c52009-02-01 04:00:05 +00002from .. import util
Brett Cannonef888022013-06-15 18:39:21 -04003
Brett Cannona3c69632013-11-08 11:10:41 -05004machinery = util.import_importlib('importlib.machinery')
5
Eric Snowb523f842013-11-22 09:05:39 -07006
7import sys
Victor Stinner272e2432011-05-16 16:57:18 +02008from test.support import captured_stdout
Brett Cannonef888022013-06-15 18:39:21 -04009import types
Eric Snowb523f842013-11-22 09:05:39 -070010import unittest
Eric Snow1500d492014-01-06 20:49:04 -070011import warnings
Eric Snowb523f842013-11-22 09:05:39 -070012
13
14class ExecModuleTests(abc.LoaderTests):
15
16 def exec_module(self, name):
17 with util.uncache(name), captured_stdout() as stdout:
18 spec = self.machinery.ModuleSpec(
19 name, self.machinery.FrozenImporter, origin='frozen',
20 is_package=self.machinery.FrozenImporter.is_package(name))
21 module = types.ModuleType(name)
22 module.__spec__ = spec
23 assert not hasattr(module, 'initialized')
24 self.machinery.FrozenImporter.exec_module(module)
25 self.assertTrue(module.initialized)
26 self.assertTrue(hasattr(module, '__spec__'))
27 self.assertEqual(module.__spec__.origin, 'frozen')
28 return module, stdout.getvalue()
29
30 def test_module(self):
31 name = '__hello__'
32 module, output = self.exec_module(name)
33 check = {'__name__': name}
34 for attr, value in check.items():
35 self.assertEqual(getattr(module, attr), value)
36 self.assertEqual(output, 'Hello world!\n')
37 self.assertTrue(hasattr(module, '__spec__'))
38
39 def test_package(self):
40 name = '__phello__'
41 module, output = self.exec_module(name)
42 check = {'__name__': name}
43 for attr, value in check.items():
44 attr_value = getattr(module, attr)
45 self.assertEqual(attr_value, value,
46 'for {name}.{attr}, {given!r} != {expected!r}'.format(
47 name=name, attr=attr, given=attr_value,
48 expected=value))
49 self.assertEqual(output, 'Hello world!\n')
50
51 def test_lacking_parent(self):
52 name = '__phello__.spam'
53 with util.uncache('__phello__'):
54 module, output = self.exec_module(name)
55 check = {'__name__': name}
56 for attr, value in check.items():
57 attr_value = getattr(module, attr)
58 self.assertEqual(attr_value, value,
59 'for {name}.{attr}, {given} != {expected!r}'.format(
60 name=name, attr=attr, given=attr_value,
61 expected=value))
62 self.assertEqual(output, 'Hello world!\n')
63
Eric Snowb523f842013-11-22 09:05:39 -070064 def test_module_repr(self):
65 name = '__hello__'
66 module, output = self.exec_module(name)
Eric Snow1500d492014-01-06 20:49:04 -070067 with warnings.catch_warnings():
68 warnings.simplefilter('ignore', DeprecationWarning)
69 repr_str = self.machinery.FrozenImporter.module_repr(module)
70 self.assertEqual(repr_str,
71 "<module '__hello__' (frozen)>")
72
73 def test_module_repr_indirect(self):
74 name = '__hello__'
75 module, output = self.exec_module(name)
Eric Snowb523f842013-11-22 09:05:39 -070076 self.assertEqual(repr(module),
77 "<module '__hello__' (frozen)>")
78
79 # No way to trigger an error in a frozen module.
80 test_state_after_failure = None
81
82 def test_unloadable(self):
83 assert self.machinery.FrozenImporter.find_module('_not_real') is None
84 with self.assertRaises(ImportError) as cm:
85 self.exec_module('_not_real')
86 self.assertEqual(cm.exception.name, '_not_real')
87
88Frozen_ExecModuleTests, Source_ExecModuleTests = util.test_both(ExecModuleTests,
89 machinery=machinery)
Brett Cannonef888022013-06-15 18:39:21 -040090
Brett Cannon23cbd8a2009-01-18 00:24:28 +000091
Brett Cannona3c69632013-11-08 11:10:41 -050092class LoaderTests(abc.LoaderTests):
Brett Cannon23cbd8a2009-01-18 00:24:28 +000093
Brett Cannon223a19d2009-02-01 01:34:13 +000094 def test_module(self):
Victor Stinner272e2432011-05-16 16:57:18 +020095 with util.uncache('__hello__'), captured_stdout() as stdout:
Eric Snow1500d492014-01-06 20:49:04 -070096 with warnings.catch_warnings():
97 warnings.simplefilter('ignore', DeprecationWarning)
98 module = self.machinery.FrozenImporter.load_module('__hello__')
Eric V. Smith984b11f2012-05-24 20:21:04 -040099 check = {'__name__': '__hello__',
Eric V. Smith283d0ba2012-05-24 20:22:10 -0400100 '__package__': '',
Brett Cannona3c69632013-11-08 11:10:41 -0500101 '__loader__': self.machinery.FrozenImporter,
Eric V. Smith984b11f2012-05-24 20:21:04 -0400102 }
Brett Cannon223a19d2009-02-01 01:34:13 +0000103 for attr, value in check.items():
104 self.assertEqual(getattr(module, attr), value)
Victor Stinner272e2432011-05-16 16:57:18 +0200105 self.assertEqual(stdout.getvalue(), 'Hello world!\n')
Eric V. Smith984b11f2012-05-24 20:21:04 -0400106 self.assertFalse(hasattr(module, '__file__'))
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000107
Brett Cannon223a19d2009-02-01 01:34:13 +0000108 def test_package(self):
Victor Stinner272e2432011-05-16 16:57:18 +0200109 with util.uncache('__phello__'), captured_stdout() as stdout:
Eric Snow1500d492014-01-06 20:49:04 -0700110 with warnings.catch_warnings():
111 warnings.simplefilter('ignore', DeprecationWarning)
112 module = self.machinery.FrozenImporter.load_module('__phello__')
Eric V. Smith283d0ba2012-05-24 20:22:10 -0400113 check = {'__name__': '__phello__',
114 '__package__': '__phello__',
Brett Cannon3e0651b2013-05-31 23:18:39 -0400115 '__path__': [],
Brett Cannona3c69632013-11-08 11:10:41 -0500116 '__loader__': self.machinery.FrozenImporter,
Eric V. Smith984b11f2012-05-24 20:21:04 -0400117 }
Brett Cannon223a19d2009-02-01 01:34:13 +0000118 for attr, value in check.items():
119 attr_value = getattr(module, attr)
120 self.assertEqual(attr_value, value,
121 "for __phello__.%s, %r != %r" %
122 (attr, attr_value, value))
Victor Stinner272e2432011-05-16 16:57:18 +0200123 self.assertEqual(stdout.getvalue(), 'Hello world!\n')
Eric V. Smith984b11f2012-05-24 20:21:04 -0400124 self.assertFalse(hasattr(module, '__file__'))
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000125
Brett Cannon223a19d2009-02-01 01:34:13 +0000126 def test_lacking_parent(self):
Victor Stinner272e2432011-05-16 16:57:18 +0200127 with util.uncache('__phello__', '__phello__.spam'), \
128 captured_stdout() as stdout:
Eric Snow1500d492014-01-06 20:49:04 -0700129 with warnings.catch_warnings():
130 warnings.simplefilter('ignore', DeprecationWarning)
131 module = self.machinery.FrozenImporter.load_module('__phello__.spam')
Eric V. Smith984b11f2012-05-24 20:21:04 -0400132 check = {'__name__': '__phello__.spam',
Brett Cannon2cf03a82009-03-10 05:17:37 +0000133 '__package__': '__phello__',
Brett Cannona3c69632013-11-08 11:10:41 -0500134 '__loader__': self.machinery.FrozenImporter,
Eric V. Smith984b11f2012-05-24 20:21:04 -0400135 }
Brett Cannon223a19d2009-02-01 01:34:13 +0000136 for attr, value in check.items():
137 attr_value = getattr(module, attr)
138 self.assertEqual(attr_value, value,
139 "for __phello__.spam.%s, %r != %r" %
140 (attr, attr_value, value))
Victor Stinner272e2432011-05-16 16:57:18 +0200141 self.assertEqual(stdout.getvalue(), 'Hello world!\n')
Eric V. Smith984b11f2012-05-24 20:21:04 -0400142 self.assertFalse(hasattr(module, '__file__'))
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000143
Brett Cannon223a19d2009-02-01 01:34:13 +0000144 def test_module_reuse(self):
Victor Stinner272e2432011-05-16 16:57:18 +0200145 with util.uncache('__hello__'), captured_stdout() as stdout:
Eric Snow1500d492014-01-06 20:49:04 -0700146 with warnings.catch_warnings():
147 warnings.simplefilter('ignore', DeprecationWarning)
148 module1 = self.machinery.FrozenImporter.load_module('__hello__')
149 module2 = self.machinery.FrozenImporter.load_module('__hello__')
Eric V. Smithfaae3ad2012-06-27 15:26:26 -0400150 self.assertIs(module1, module2)
Victor Stinner272e2432011-05-16 16:57:18 +0200151 self.assertEqual(stdout.getvalue(),
152 'Hello world!\nHello world!\n')
Brett Cannon223a19d2009-02-01 01:34:13 +0000153
Eric V. Smith984b11f2012-05-24 20:21:04 -0400154 def test_module_repr(self):
155 with util.uncache('__hello__'), captured_stdout():
Eric Snow1500d492014-01-06 20:49:04 -0700156 with warnings.catch_warnings():
157 warnings.simplefilter('ignore', DeprecationWarning)
158 module = self.machinery.FrozenImporter.load_module('__hello__')
159 repr_str = self.machinery.FrozenImporter.module_repr(module)
160 self.assertEqual(repr_str,
Eric V. Smith984b11f2012-05-24 20:21:04 -0400161 "<module '__hello__' (frozen)>")
162
Eric Snow1500d492014-01-06 20:49:04 -0700163 def test_module_repr_indirect(self):
164 with util.uncache('__hello__'), captured_stdout():
165 module = self.machinery.FrozenImporter.load_module('__hello__')
166 self.assertEqual(repr(module),
167 "<module '__hello__' (frozen)>")
168
Eric Snowb523f842013-11-22 09:05:39 -0700169 # No way to trigger an error in a frozen module.
170 test_state_after_failure = None
Brett Cannon223a19d2009-02-01 01:34:13 +0000171
172 def test_unloadable(self):
Brett Cannona3c69632013-11-08 11:10:41 -0500173 assert self.machinery.FrozenImporter.find_module('_not_real') is None
Brett Cannonbbb66802012-04-12 21:09:01 -0400174 with self.assertRaises(ImportError) as cm:
Brett Cannona3c69632013-11-08 11:10:41 -0500175 self.machinery.FrozenImporter.load_module('_not_real')
Brett Cannonbbb66802012-04-12 21:09:01 -0400176 self.assertEqual(cm.exception.name, '_not_real')
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000177
Brett Cannona3c69632013-11-08 11:10:41 -0500178Frozen_LoaderTests, Source_LoaderTests = util.test_both(LoaderTests,
179 machinery=machinery)
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000180
Brett Cannona3c69632013-11-08 11:10:41 -0500181
182class InspectLoaderTests:
Brett Cannon8d110132009-03-15 02:20:16 +0000183
184 """Tests for the InspectLoader methods for FrozenImporter."""
185
186 def test_get_code(self):
187 # Make sure that the code object is good.
188 name = '__hello__'
Victor Stinner272e2432011-05-16 16:57:18 +0200189 with captured_stdout() as stdout:
Brett Cannona3c69632013-11-08 11:10:41 -0500190 code = self.machinery.FrozenImporter.get_code(name)
Brett Cannonef888022013-06-15 18:39:21 -0400191 mod = types.ModuleType(name)
Victor Stinner272e2432011-05-16 16:57:18 +0200192 exec(code, mod.__dict__)
193 self.assertTrue(hasattr(mod, 'initialized'))
194 self.assertEqual(stdout.getvalue(), 'Hello world!\n')
Brett Cannon8d110132009-03-15 02:20:16 +0000195
196 def test_get_source(self):
197 # Should always return None.
Brett Cannona3c69632013-11-08 11:10:41 -0500198 result = self.machinery.FrozenImporter.get_source('__hello__')
Eric V. Smithb1095152012-06-28 06:15:01 -0400199 self.assertIsNone(result)
Brett Cannon8d110132009-03-15 02:20:16 +0000200
201 def test_is_package(self):
202 # Should be able to tell what is a package.
203 test_for = (('__hello__', False), ('__phello__', True),
204 ('__phello__.spam', False))
205 for name, is_package in test_for:
Brett Cannona3c69632013-11-08 11:10:41 -0500206 result = self.machinery.FrozenImporter.is_package(name)
Eric V. Smithfaae3ad2012-06-27 15:26:26 -0400207 self.assertEqual(bool(result), is_package)
Brett Cannon8d110132009-03-15 02:20:16 +0000208
209 def test_failure(self):
210 # Raise ImportError for modules that are not frozen.
211 for meth_name in ('get_code', 'get_source', 'is_package'):
Brett Cannona3c69632013-11-08 11:10:41 -0500212 method = getattr(self.machinery.FrozenImporter, meth_name)
Brett Cannonbbb66802012-04-12 21:09:01 -0400213 with self.assertRaises(ImportError) as cm:
Brett Cannon2153dc02009-08-27 23:49:21 +0000214 method('importlib')
Brett Cannonbbb66802012-04-12 21:09:01 -0400215 self.assertEqual(cm.exception.name, 'importlib')
Brett Cannon8d110132009-03-15 02:20:16 +0000216
Brett Cannona3c69632013-11-08 11:10:41 -0500217Frozen_ILTests, Source_ILTests = util.test_both(InspectLoaderTests,
218 machinery=machinery)
Brett Cannon23cbd8a2009-01-18 00:24:28 +0000219
220
221if __name__ == '__main__':
Brett Cannona3c69632013-11-08 11:10:41 -0500222 unittest.main()