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