| 1 |
import unittest |
| 2 |
from test import support |
| 3 |
from test.support import warnings_helper |
| 4 |
import os |
| 5 |
import sys |
| 6 |
import types |
| 7 |
|
| 8 |
try: |
| 9 |
import _multiprocessing |
| 10 |
except ModuleNotFoundError: |
| 11 |
_multiprocessing = None |
| 12 |
|
| 13 |
|
| 14 |
if support.check_sanitizer(address=True, memory=True): |
| 15 |
|
| 16 |
|
| 17 |
raise unittest.SkipTest("workaround ASAN build issues on loading tests " |
| 18 |
"like tk or crypt") |
| 19 |
|
| 20 |
|
| 21 |
class NoAll(RuntimeError): |
| 22 |
pass |
| 23 |
|
| 24 |
class FailedImport(RuntimeError): |
| 25 |
pass |
| 26 |
|
| 27 |
|
| 28 |
class AllTest(unittest.TestCase): |
| 29 |
|
| 30 |
def setUp(self): |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
if _multiprocessing is None: |
| 35 |
sys.modules["_multiprocessing"] = types.ModuleType("_multiprocessing") |
| 36 |
|
| 37 |
def tearDown(self): |
| 38 |
if _multiprocessing is None: |
| 39 |
sys.modules.pop("_multiprocessing") |
| 40 |
|
| 41 |
def check_all(self, modname): |
| 42 |
names = {} |
| 43 |
with warnings_helper.check_warnings( |
| 44 |
(f".*{modname}", DeprecationWarning), |
| 45 |
(".* (module|package)", DeprecationWarning), |
| 46 |
(".* (module|package)", PendingDeprecationWarning), |
| 47 |
("", ResourceWarning), |
| 48 |
quiet=True): |
| 49 |
try: |
| 50 |
exec("import %s" % modname, names) |
| 51 |
except: |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
raise FailedImport(modname) |
| 56 |
if not hasattr(sys.modules[modname], "__all__"): |
| 57 |
raise NoAll(modname) |
| 58 |
names = {} |
| 59 |
with self.subTest(module=modname): |
| 60 |
with warnings_helper.check_warnings( |
| 61 |
("", DeprecationWarning), |
| 62 |
("", ResourceWarning), |
| 63 |
quiet=True): |
| 64 |
try: |
| 65 |
exec("from %s import *" % modname, names) |
| 66 |
except Exception as e: |
| 67 |
|
| 68 |
self.fail("__all__ failure in {}: {}: {}".format( |
| 69 |
modname, e.__class__.__name__, e)) |
| 70 |
if "__builtins__" in names: |
| 71 |
del names["__builtins__"] |
| 72 |
if '__annotations__' in names: |
| 73 |
del names['__annotations__'] |
| 74 |
if "__warningregistry__" in names: |
| 75 |
del names["__warningregistry__"] |
| 76 |
keys = set(names) |
| 77 |
all_list = sys.modules[modname].__all__ |
| 78 |
all_set = set(all_list) |
| 79 |
self.assertCountEqual(all_set, all_list, "in module {}".format(modname)) |
| 80 |
self.assertEqual(keys, all_set, "in module {}".format(modname)) |
| 81 |
|
| 82 |
def walk_modules(self, basedir, modpath): |
| 83 |
if modpath == 'distutils.': |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
return |
| 89 |
for fn in sorted(os.listdir(basedir)): |
| 90 |
path = os.path.join(basedir, fn) |
| 91 |
if os.path.isdir(path): |
| 92 |
pkg_init = os.path.join(path, '__init__.py') |
| 93 |
if os.path.exists(pkg_init): |
| 94 |
yield pkg_init, modpath + fn |
| 95 |
for p, m in self.walk_modules(path, modpath + fn + "."): |
| 96 |
yield p, m |
| 97 |
continue |
| 98 |
if not fn.endswith('.py') or fn == '__init__.py': |
| 99 |
continue |
| 100 |
yield path, modpath + fn[:-3] |
| 101 |
|
| 102 |
def test_all(self): |
| 103 |
|
| 104 |
denylist = set([ |
| 105 |
|
| 106 |
'__future__', |
| 107 |
]) |
| 108 |
|
| 109 |
if not sys.platform.startswith('java'): |
| 110 |
|
| 111 |
|
| 112 |
import _socket |
| 113 |
|
| 114 |
ignored = [] |
| 115 |
failed_imports = [] |
| 116 |
lib_dir = os.path.dirname(os.path.dirname(__file__)) |
| 117 |
for path, modname in self.walk_modules(lib_dir, ""): |
| 118 |
m = modname |
| 119 |
denied = False |
| 120 |
while m: |
| 121 |
if m in denylist: |
| 122 |
denied = True |
| 123 |
break |
| 124 |
m = m.rpartition('.')[0] |
| 125 |
if denied: |
| 126 |
continue |
| 127 |
if support.verbose: |
| 128 |
print(modname) |
| 129 |
try: |
| 130 |
|
| 131 |
|
| 132 |
with open(path, "rb") as f: |
| 133 |
if b"__all__" not in f.read(): |
| 134 |
raise NoAll(modname) |
| 135 |
self.check_all(modname) |
| 136 |
except NoAll: |
| 137 |
ignored.append(modname) |
| 138 |
except FailedImport: |
| 139 |
failed_imports.append(modname) |
| 140 |
|
| 141 |
if support.verbose: |
| 142 |
print('Following modules have no __all__ and have been ignored:', |
| 143 |
ignored) |
| 144 |
print('Following modules failed to be imported:', failed_imports) |
| 145 |
|
| 146 |
|
| 147 |
if __name__ == "__main__": |
| 148 |
unittest.main() |