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