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

Python code coverage for Lib/test/test___all__.py

#countcontent
1n/aimport unittest
2n/afrom test import support
3n/aimport os
4n/aimport sys
5n/a
6n/a
7n/aclass NoAll(RuntimeError):
8n/a pass
9n/a
10n/aclass FailedImport(RuntimeError):
11n/a pass
12n/a
13n/a
14n/aclass AllTest(unittest.TestCase):
15n/a
16n/a def check_all(self, modname):
17n/a names = {}
18n/a with support.check_warnings(
19n/a (".* (module|package)", DeprecationWarning),
20n/a ("", ResourceWarning),
21n/a quiet=True):
22n/a try:
23n/a exec("import %s" % modname, names)
24n/a except:
25n/a # Silent fail here seems the best route since some modules
26n/a # may not be available or not initialize properly in all
27n/a # environments.
28n/a raise FailedImport(modname)
29n/a if not hasattr(sys.modules[modname], "__all__"):
30n/a raise NoAll(modname)
31n/a names = {}
32n/a with self.subTest(module=modname):
33n/a try:
34n/a exec("from %s import *" % modname, names)
35n/a except Exception as e:
36n/a # Include the module name in the exception string
37n/a self.fail("__all__ failure in {}: {}: {}".format(
38n/a modname, e.__class__.__name__, e))
39n/a if "__builtins__" in names:
40n/a del names["__builtins__"]
41n/a if '__annotations__' in names:
42n/a del names['__annotations__']
43n/a keys = set(names)
44n/a all_list = sys.modules[modname].__all__
45n/a all_set = set(all_list)
46n/a self.assertCountEqual(all_set, all_list, "in module {}".format(modname))
47n/a self.assertEqual(keys, all_set, "in module {}".format(modname))
48n/a
49n/a def walk_modules(self, basedir, modpath):
50n/a for fn in sorted(os.listdir(basedir)):
51n/a path = os.path.join(basedir, fn)
52n/a if os.path.isdir(path):
53n/a pkg_init = os.path.join(path, '__init__.py')
54n/a if os.path.exists(pkg_init):
55n/a yield pkg_init, modpath + fn
56n/a for p, m in self.walk_modules(path, modpath + fn + "."):
57n/a yield p, m
58n/a continue
59n/a if not fn.endswith('.py') or fn == '__init__.py':
60n/a continue
61n/a yield path, modpath + fn[:-3]
62n/a
63n/a def test_all(self):
64n/a # Blacklisted modules and packages
65n/a blacklist = set([
66n/a # Will raise a SyntaxError when compiling the exec statement
67n/a '__future__',
68n/a ])
69n/a
70n/a if not sys.platform.startswith('java'):
71n/a # In case _socket fails to build, make this test fail more gracefully
72n/a # than an AttributeError somewhere deep in CGIHTTPServer.
73n/a import _socket
74n/a
75n/a ignored = []
76n/a failed_imports = []
77n/a lib_dir = os.path.dirname(os.path.dirname(__file__))
78n/a for path, modname in self.walk_modules(lib_dir, ""):
79n/a m = modname
80n/a blacklisted = False
81n/a while m:
82n/a if m in blacklist:
83n/a blacklisted = True
84n/a break
85n/a m = m.rpartition('.')[0]
86n/a if blacklisted:
87n/a continue
88n/a if support.verbose:
89n/a print(modname)
90n/a try:
91n/a # This heuristic speeds up the process by removing, de facto,
92n/a # most test modules (and avoiding the auto-executing ones).
93n/a with open(path, "rb") as f:
94n/a if b"__all__" not in f.read():
95n/a raise NoAll(modname)
96n/a self.check_all(modname)
97n/a except NoAll:
98n/a ignored.append(modname)
99n/a except FailedImport:
100n/a failed_imports.append(modname)
101n/a
102n/a if support.verbose:
103n/a print('Following modules have no __all__ and have been ignored:',
104n/a ignored)
105n/a print('Following modules failed to be imported:', failed_imports)
106n/a
107n/a
108n/aif __name__ == "__main__":
109n/a unittest.main()