| 1 |
import unittest |
| 2 |
from test import support |
| 3 |
from test.support import warnings_helper |
| 4 |
import os |
| 5 |
import sys |
| 6 |
|
| 7 |
|
| 8 |
if support.check_sanitizer(address=True, memory=True): |
| 9 |
SKIP_MODULES = frozenset(( |
| 10 |
# gh-90791: Tests involving libX11 can SEGFAULT on ASAN/MSAN builds. |
| 11 |
# Skip modules, packages and tests using '_tkinter'. |
| 12 |
'_tkinter', |
| 13 |
'tkinter', |
| 14 |
'test_tkinter', |
| 15 |
'test_ttk', |
| 16 |
'test_ttk_textonly', |
| 17 |
'idlelib', |
| 18 |
'test_idle', |
| 19 |
)) |
| 20 |
else: |
| 21 |
SKIP_MODULES = () |
| 22 |
|
| 23 |
|
| 24 |
class NoAll(RuntimeError): |
| 25 |
pass |
| 26 |
|
| 27 |
class FailedImport(RuntimeError): |
| 28 |
pass |
| 29 |
|
| 30 |
|
| 31 |
class AllTest(unittest.TestCase): |
| 32 |
|
| 33 |
def check_all(self, modname): |
| 34 |
names = {} |
| 35 |
with warnings_helper.check_warnings( |
| 36 |
(f".*{modname}", DeprecationWarning), |
| 37 |
(".* (module|package)", DeprecationWarning), |
| 38 |
(".* (module|package)", PendingDeprecationWarning), |
| 39 |
("", ResourceWarning), |
| 40 |
("", SyntaxWarning), |
| 41 |
quiet=True): |
| 42 |
try: |
| 43 |
exec("import %s" % modname, names) |
| 44 |
except: |
| 45 |
# Silent fail here seems the best route since some modules |
| 46 |
# may not be available or not initialize properly in all |
| 47 |
# environments. |
| 48 |
raise FailedImport(modname) |
| 49 |
if not hasattr(sys.modules[modname], "__all__"): |
| 50 |
raise NoAll(modname) |
| 51 |
names = {} |
| 52 |
with self.subTest(module=modname): |
| 53 |
with warnings_helper.check_warnings( |
| 54 |
("", DeprecationWarning), |
| 55 |
("", ResourceWarning), |
| 56 |
("", SyntaxWarning), |
| 57 |
quiet=True): |
| 58 |
try: |
| 59 |
exec("from %s import *" % modname, names) |
| 60 |
except Exception as e: |
| 61 |
# Include the module name in the exception string |
| 62 |
self.fail("__all__ failure in {}: {}: {}".format( |
| 63 |
modname, e.__class__.__name__, e)) |
| 64 |
if "__builtins__" in names: |
| 65 |
del names["__builtins__"] |
| 66 |
if '__annotations__' in names: |
| 67 |
del names['__annotations__'] |
| 68 |
if "__warningregistry__" in names: |
| 69 |
del names["__warningregistry__"] |
| 70 |
keys = set(names) |
| 71 |
all_list = sys.modules[modname].__all__ |
| 72 |
all_set = set(all_list) |
| 73 |
self.assertCountEqual(all_set, all_list, "in module {}".format(modname)) |
| 74 |
self.assertEqual(keys, all_set, "in module {}".format(modname)) |
| 75 |
# Verify __dir__ is non-empty and doesn't produce an error |
| 76 |
self.assertTrue(dir(sys.modules[modname])) |
| 77 |
|
| 78 |
def walk_modules(self, basedir, modpath): |
| 79 |
for fn in sorted(os.listdir(basedir)): |
| 80 |
path = os.path.join(basedir, fn) |
| 81 |
if os.path.isdir(path): |
| 82 |
if fn in SKIP_MODULES: |
| 83 |
continue |
| 84 |
pkg_init = os.path.join(path, '__init__.py') |
| 85 |
if os.path.exists(pkg_init): |
| 86 |
yield pkg_init, modpath + fn |
| 87 |
for p, m in self.walk_modules(path, modpath + fn + "."): |
| 88 |
yield p, m |
| 89 |
continue |
| 90 |
|
| 91 |
if fn == '__init__.py': |
| 92 |
continue |
| 93 |
if not fn.endswith('.py'): |
| 94 |
continue |
| 95 |
modname = fn.removesuffix('.py') |
| 96 |
if modname in SKIP_MODULES: |
| 97 |
continue |
| 98 |
yield path, modpath + modname |
| 99 |
|
| 100 |
def test_all(self): |
| 101 |
# List of denied modules and packages |
| 102 |
denylist = set([ |
| 103 |
# Will raise a SyntaxError when compiling the exec statement |
| 104 |
'__future__', |
| 105 |
]) |
| 106 |
|
| 107 |
# In case _socket fails to build, make this test fail more gracefully |
| 108 |
# than an AttributeError somewhere deep in concurrent.futures, email |
| 109 |
# or unittest. |
| 110 |
import _socket # noqa: F401 |
| 111 |
|
| 112 |
ignored = [] |
| 113 |
failed_imports = [] |
| 114 |
lib_dir = os.path.dirname(os.path.dirname(__file__)) |
| 115 |
for path, modname in self.walk_modules(lib_dir, ""): |
| 116 |
m = modname |
| 117 |
denied = False |
| 118 |
while m: |
| 119 |
if m in denylist: |
| 120 |
denied = True |
| 121 |
break |
| 122 |
m = m.rpartition('.')[0] |
| 123 |
if denied: |
| 124 |
continue |
| 125 |
if support.verbose: |
| 126 |
print(f"Check {modname}", flush=True) |
| 127 |
try: |
| 128 |
# This heuristic speeds up the process by removing, de facto, |
| 129 |
# most test modules (and avoiding the auto-executing ones). |
| 130 |
with open(path, "rb") as f: |
| 131 |
if b"__all__" not in f.read(): |
| 132 |
raise NoAll(modname) |
| 133 |
self.check_all(modname) |
| 134 |
except NoAll: |
| 135 |
ignored.append(modname) |
| 136 |
except FailedImport: |
| 137 |
failed_imports.append(modname) |
| 138 |
|
| 139 |
if support.verbose: |
| 140 |
print('Following modules have no __all__ and have been ignored:', |
| 141 |
ignored) |
| 142 |
print('Following modules failed to be imported:', failed_imports) |
| 143 |
|
| 144 |
|
| 145 |
if __name__ == "__main__": |
| 146 |
unittest.main() |