ยปCore Development>Code coverage>Lib/test/test_module.py

Python code coverage for Lib/test/test_module.py

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