1 | n/a | # Test the module type |
---|
2 | n/a | import unittest |
---|
3 | n/a | import weakref |
---|
4 | n/a | from test.support import gc_collect, requires_type_collecting |
---|
5 | n/a | from test.support.script_helper import assert_python_ok |
---|
6 | n/a | |
---|
7 | n/a | import sys |
---|
8 | n/a | ModuleType = type(sys) |
---|
9 | n/a | |
---|
10 | n/a | class FullLoader: |
---|
11 | n/a | @classmethod |
---|
12 | n/a | def module_repr(cls, m): |
---|
13 | n/a | return "<module '{}' (crafted)>".format(m.__name__) |
---|
14 | n/a | |
---|
15 | n/a | class BareLoader: |
---|
16 | n/a | pass |
---|
17 | n/a | |
---|
18 | n/a | |
---|
19 | n/a | class ModuleTests(unittest.TestCase): |
---|
20 | n/a | def test_uninitialized(self): |
---|
21 | n/a | # An uninitialized module has no __dict__ or __name__, |
---|
22 | n/a | # and __doc__ is None |
---|
23 | n/a | foo = ModuleType.__new__(ModuleType) |
---|
24 | n/a | self.assertTrue(foo.__dict__ is None) |
---|
25 | n/a | self.assertRaises(SystemError, dir, foo) |
---|
26 | n/a | try: |
---|
27 | n/a | s = foo.__name__ |
---|
28 | n/a | self.fail("__name__ = %s" % repr(s)) |
---|
29 | n/a | except AttributeError: |
---|
30 | n/a | pass |
---|
31 | n/a | self.assertEqual(foo.__doc__, ModuleType.__doc__) |
---|
32 | n/a | |
---|
33 | n/a | def test_uninitialized_missing_getattr(self): |
---|
34 | n/a | # Issue 8297 |
---|
35 | n/a | # test the text in the AttributeError of an uninitialized module |
---|
36 | n/a | foo = ModuleType.__new__(ModuleType) |
---|
37 | n/a | self.assertRaisesRegex( |
---|
38 | n/a | AttributeError, "module has no attribute 'not_here'", |
---|
39 | n/a | getattr, foo, "not_here") |
---|
40 | n/a | |
---|
41 | n/a | def test_missing_getattr(self): |
---|
42 | n/a | # Issue 8297 |
---|
43 | n/a | # test the text in the AttributeError |
---|
44 | n/a | foo = ModuleType("foo") |
---|
45 | n/a | self.assertRaisesRegex( |
---|
46 | n/a | AttributeError, "module 'foo' has no attribute 'not_here'", |
---|
47 | n/a | getattr, foo, "not_here") |
---|
48 | n/a | |
---|
49 | n/a | def test_no_docstring(self): |
---|
50 | n/a | # Regularly initialized module, no docstring |
---|
51 | n/a | foo = ModuleType("foo") |
---|
52 | n/a | self.assertEqual(foo.__name__, "foo") |
---|
53 | n/a | self.assertEqual(foo.__doc__, None) |
---|
54 | n/a | self.assertIs(foo.__loader__, None) |
---|
55 | n/a | self.assertIs(foo.__package__, None) |
---|
56 | n/a | self.assertIs(foo.__spec__, None) |
---|
57 | n/a | self.assertEqual(foo.__dict__, {"__name__": "foo", "__doc__": None, |
---|
58 | n/a | "__loader__": None, "__package__": None, |
---|
59 | n/a | "__spec__": None}) |
---|
60 | n/a | |
---|
61 | n/a | def test_ascii_docstring(self): |
---|
62 | n/a | # ASCII docstring |
---|
63 | n/a | foo = ModuleType("foo", "foodoc") |
---|
64 | n/a | self.assertEqual(foo.__name__, "foo") |
---|
65 | n/a | self.assertEqual(foo.__doc__, "foodoc") |
---|
66 | n/a | self.assertEqual(foo.__dict__, |
---|
67 | n/a | {"__name__": "foo", "__doc__": "foodoc", |
---|
68 | n/a | "__loader__": None, "__package__": None, |
---|
69 | n/a | "__spec__": None}) |
---|
70 | n/a | |
---|
71 | n/a | def test_unicode_docstring(self): |
---|
72 | n/a | # Unicode docstring |
---|
73 | n/a | foo = ModuleType("foo", "foodoc\u1234") |
---|
74 | n/a | self.assertEqual(foo.__name__, "foo") |
---|
75 | n/a | self.assertEqual(foo.__doc__, "foodoc\u1234") |
---|
76 | n/a | self.assertEqual(foo.__dict__, |
---|
77 | n/a | {"__name__": "foo", "__doc__": "foodoc\u1234", |
---|
78 | n/a | "__loader__": None, "__package__": None, |
---|
79 | n/a | "__spec__": None}) |
---|
80 | n/a | |
---|
81 | n/a | def test_reinit(self): |
---|
82 | n/a | # Reinitialization should not replace the __dict__ |
---|
83 | n/a | foo = ModuleType("foo", "foodoc\u1234") |
---|
84 | n/a | foo.bar = 42 |
---|
85 | n/a | d = foo.__dict__ |
---|
86 | n/a | foo.__init__("foo", "foodoc") |
---|
87 | n/a | self.assertEqual(foo.__name__, "foo") |
---|
88 | n/a | self.assertEqual(foo.__doc__, "foodoc") |
---|
89 | n/a | self.assertEqual(foo.bar, 42) |
---|
90 | n/a | self.assertEqual(foo.__dict__, |
---|
91 | n/a | {"__name__": "foo", "__doc__": "foodoc", "bar": 42, |
---|
92 | n/a | "__loader__": None, "__package__": None, "__spec__": None}) |
---|
93 | n/a | self.assertTrue(foo.__dict__ is d) |
---|
94 | n/a | |
---|
95 | n/a | def test_dont_clear_dict(self): |
---|
96 | n/a | # See issue 7140. |
---|
97 | n/a | def f(): |
---|
98 | n/a | foo = ModuleType("foo") |
---|
99 | n/a | foo.bar = 4 |
---|
100 | n/a | return foo |
---|
101 | n/a | gc_collect() |
---|
102 | n/a | self.assertEqual(f().__dict__["bar"], 4) |
---|
103 | n/a | |
---|
104 | n/a | @requires_type_collecting |
---|
105 | n/a | def test_clear_dict_in_ref_cycle(self): |
---|
106 | n/a | destroyed = [] |
---|
107 | n/a | m = ModuleType("foo") |
---|
108 | n/a | m.destroyed = destroyed |
---|
109 | n/a | s = """class A: |
---|
110 | n/a | def __init__(self, l): |
---|
111 | n/a | self.l = l |
---|
112 | n/a | def __del__(self): |
---|
113 | n/a | self.l.append(1) |
---|
114 | n/a | a = A(destroyed)""" |
---|
115 | n/a | exec(s, m.__dict__) |
---|
116 | n/a | del m |
---|
117 | n/a | gc_collect() |
---|
118 | n/a | self.assertEqual(destroyed, [1]) |
---|
119 | n/a | |
---|
120 | n/a | def test_weakref(self): |
---|
121 | n/a | m = ModuleType("foo") |
---|
122 | n/a | wr = weakref.ref(m) |
---|
123 | n/a | self.assertIs(wr(), m) |
---|
124 | n/a | del m |
---|
125 | n/a | gc_collect() |
---|
126 | n/a | self.assertIs(wr(), None) |
---|
127 | n/a | |
---|
128 | n/a | def test_module_repr_minimal(self): |
---|
129 | n/a | # reprs when modules have no __file__, __name__, or __loader__ |
---|
130 | n/a | m = ModuleType('foo') |
---|
131 | n/a | del m.__name__ |
---|
132 | n/a | self.assertEqual(repr(m), "<module '?'>") |
---|
133 | n/a | |
---|
134 | n/a | def test_module_repr_with_name(self): |
---|
135 | n/a | m = ModuleType('foo') |
---|
136 | n/a | self.assertEqual(repr(m), "<module 'foo'>") |
---|
137 | n/a | |
---|
138 | n/a | def test_module_repr_with_name_and_filename(self): |
---|
139 | n/a | m = ModuleType('foo') |
---|
140 | n/a | m.__file__ = '/tmp/foo.py' |
---|
141 | n/a | self.assertEqual(repr(m), "<module 'foo' from '/tmp/foo.py'>") |
---|
142 | n/a | |
---|
143 | n/a | def test_module_repr_with_filename_only(self): |
---|
144 | n/a | m = ModuleType('foo') |
---|
145 | n/a | del m.__name__ |
---|
146 | n/a | m.__file__ = '/tmp/foo.py' |
---|
147 | n/a | self.assertEqual(repr(m), "<module '?' from '/tmp/foo.py'>") |
---|
148 | n/a | |
---|
149 | n/a | def test_module_repr_with_loader_as_None(self): |
---|
150 | n/a | m = ModuleType('foo') |
---|
151 | n/a | assert m.__loader__ is None |
---|
152 | n/a | self.assertEqual(repr(m), "<module 'foo'>") |
---|
153 | n/a | |
---|
154 | n/a | def test_module_repr_with_bare_loader_but_no_name(self): |
---|
155 | n/a | m = ModuleType('foo') |
---|
156 | n/a | del m.__name__ |
---|
157 | n/a | # Yes, a class not an instance. |
---|
158 | n/a | m.__loader__ = BareLoader |
---|
159 | n/a | loader_repr = repr(BareLoader) |
---|
160 | n/a | self.assertEqual( |
---|
161 | n/a | repr(m), "<module '?' ({})>".format(loader_repr)) |
---|
162 | n/a | |
---|
163 | n/a | def test_module_repr_with_full_loader_but_no_name(self): |
---|
164 | n/a | # m.__loader__.module_repr() will fail because the module has no |
---|
165 | n/a | # m.__name__. This exception will get suppressed and instead the |
---|
166 | n/a | # loader's repr will be used. |
---|
167 | n/a | m = ModuleType('foo') |
---|
168 | n/a | del m.__name__ |
---|
169 | n/a | # Yes, a class not an instance. |
---|
170 | n/a | m.__loader__ = FullLoader |
---|
171 | n/a | loader_repr = repr(FullLoader) |
---|
172 | n/a | self.assertEqual( |
---|
173 | n/a | repr(m), "<module '?' ({})>".format(loader_repr)) |
---|
174 | n/a | |
---|
175 | n/a | def test_module_repr_with_bare_loader(self): |
---|
176 | n/a | m = ModuleType('foo') |
---|
177 | n/a | # Yes, a class not an instance. |
---|
178 | n/a | m.__loader__ = BareLoader |
---|
179 | n/a | module_repr = repr(BareLoader) |
---|
180 | n/a | self.assertEqual( |
---|
181 | n/a | repr(m), "<module 'foo' ({})>".format(module_repr)) |
---|
182 | n/a | |
---|
183 | n/a | def test_module_repr_with_full_loader(self): |
---|
184 | n/a | m = ModuleType('foo') |
---|
185 | n/a | # Yes, a class not an instance. |
---|
186 | n/a | m.__loader__ = FullLoader |
---|
187 | n/a | self.assertEqual( |
---|
188 | n/a | repr(m), "<module 'foo' (crafted)>") |
---|
189 | n/a | |
---|
190 | n/a | def test_module_repr_with_bare_loader_and_filename(self): |
---|
191 | n/a | # Because the loader has no module_repr(), use the file name. |
---|
192 | n/a | m = ModuleType('foo') |
---|
193 | n/a | # Yes, a class not an instance. |
---|
194 | n/a | m.__loader__ = BareLoader |
---|
195 | n/a | m.__file__ = '/tmp/foo.py' |
---|
196 | n/a | self.assertEqual(repr(m), "<module 'foo' from '/tmp/foo.py'>") |
---|
197 | n/a | |
---|
198 | n/a | def test_module_repr_with_full_loader_and_filename(self): |
---|
199 | n/a | # Even though the module has an __file__, use __loader__.module_repr() |
---|
200 | n/a | m = ModuleType('foo') |
---|
201 | n/a | # Yes, a class not an instance. |
---|
202 | n/a | m.__loader__ = FullLoader |
---|
203 | n/a | m.__file__ = '/tmp/foo.py' |
---|
204 | n/a | self.assertEqual(repr(m), "<module 'foo' (crafted)>") |
---|
205 | n/a | |
---|
206 | n/a | def test_module_repr_builtin(self): |
---|
207 | n/a | self.assertEqual(repr(sys), "<module 'sys' (built-in)>") |
---|
208 | n/a | |
---|
209 | n/a | def test_module_repr_source(self): |
---|
210 | n/a | r = repr(unittest) |
---|
211 | n/a | starts_with = "<module 'unittest' from '" |
---|
212 | n/a | ends_with = "__init__.py'>" |
---|
213 | n/a | self.assertEqual(r[:len(starts_with)], starts_with, |
---|
214 | n/a | '{!r} does not start with {!r}'.format(r, starts_with)) |
---|
215 | n/a | self.assertEqual(r[-len(ends_with):], ends_with, |
---|
216 | n/a | '{!r} does not end with {!r}'.format(r, ends_with)) |
---|
217 | n/a | |
---|
218 | n/a | @requires_type_collecting |
---|
219 | n/a | def test_module_finalization_at_shutdown(self): |
---|
220 | n/a | # Module globals and builtins should still be available during shutdown |
---|
221 | n/a | rc, out, err = assert_python_ok("-c", "from test import final_a") |
---|
222 | n/a | self.assertFalse(err) |
---|
223 | n/a | lines = out.splitlines() |
---|
224 | n/a | self.assertEqual(set(lines), { |
---|
225 | n/a | b"x = a", |
---|
226 | n/a | b"x = b", |
---|
227 | n/a | b"final_a.x = a", |
---|
228 | n/a | b"final_b.x = b", |
---|
229 | n/a | b"len = len", |
---|
230 | n/a | b"shutil.rmtree = rmtree"}) |
---|
231 | n/a | |
---|
232 | n/a | def test_descriptor_errors_propagate(self): |
---|
233 | n/a | class Descr: |
---|
234 | n/a | def __get__(self, o, t): |
---|
235 | n/a | raise RuntimeError |
---|
236 | n/a | class M(ModuleType): |
---|
237 | n/a | melon = Descr() |
---|
238 | n/a | self.assertRaises(RuntimeError, getattr, M("mymod"), "melon") |
---|
239 | n/a | |
---|
240 | n/a | # frozen and namespace module reprs are tested in importlib. |
---|
241 | n/a | |
---|
242 | n/a | |
---|
243 | n/a | if __name__ == '__main__': |
---|
244 | n/a | unittest.main() |
---|