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): |
| 33 | try: |
| 34 | exec("from %s import *" % modname, names) |
| 35 | except Exception as e: |
| 36 | # Include the module name in the exception string |
| 37 | self.fail("__all__ failure in {}: {}: {}".format( |
| 38 | modname, e.__class__.__name__, e)) |
| 39 | if "__builtins__" in names: |
| 40 | del names["__builtins__"] |
| 41 | keys = set(names) |
| 42 | all_list = sys.modules[modname].__all__ |
| 43 | all_set = set(all_list) |
| 44 | self.assertCountEqual(all_set, all_list, "in module {}".format(modname)) |
| 45 | self.assertEqual(keys, all_set, "in module {}".format(modname)) |
Skip Montanaro | e99d5ea | 2001-01-20 19:54:20 +0000 | [diff] [blame] | 46 | |
Antoine Pitrou | 4de39cd | 2009-10-10 21:08:31 +0000 | [diff] [blame] | 47 | def walk_modules(self, basedir, modpath): |
| 48 | for fn in sorted(os.listdir(basedir)): |
| 49 | path = os.path.join(basedir, fn) |
| 50 | if os.path.isdir(path): |
| 51 | pkg_init = os.path.join(path, '__init__.py') |
| 52 | if os.path.exists(pkg_init): |
| 53 | yield pkg_init, modpath + fn |
| 54 | for p, m in self.walk_modules(path, modpath + fn + "."): |
| 55 | yield p, m |
| 56 | continue |
| 57 | if not fn.endswith('.py') or fn == '__init__.py': |
| 58 | continue |
| 59 | yield path, modpath + fn[:-3] |
| 60 | |
Walter Dörwald | b1ded1e | 2003-04-15 11:10:33 +0000 | [diff] [blame] | 61 | def test_all(self): |
Antoine Pitrou | 4de39cd | 2009-10-10 21:08:31 +0000 | [diff] [blame] | 62 | # Blacklisted modules and packages |
| 63 | blacklist = set([ |
| 64 | # Will raise a SyntaxError when compiling the exec statement |
| 65 | '__future__', |
| 66 | ]) |
| 67 | |
Walter Dörwald | b1ded1e | 2003-04-15 11:10:33 +0000 | [diff] [blame] | 68 | if not sys.platform.startswith('java'): |
| 69 | # In case _socket fails to build, make this test fail more gracefully |
| 70 | # than an AttributeError somewhere deep in CGIHTTPServer. |
| 71 | import _socket |
Tim Peters | ab9ba27 | 2001-08-09 21:40:30 +0000 | [diff] [blame] | 72 | |
Walter Dörwald | b1ded1e | 2003-04-15 11:10:33 +0000 | [diff] [blame] | 73 | # rlcompleter needs special consideration; it import readline which |
| 74 | # initializes GNU readline which calls setlocale(LC_CTYPE, "")... :-( |
Ezio Melotti | 8b23f5c | 2014-11-02 19:08:35 +0200 | [diff] [blame] | 75 | import locale |
| 76 | locale_tuple = locale.getlocale(locale.LC_CTYPE) |
Walter Dörwald | b1ded1e | 2003-04-15 11:10:33 +0000 | [diff] [blame] | 77 | try: |
Antoine Pitrou | 4de39cd | 2009-10-10 21:08:31 +0000 | [diff] [blame] | 78 | import rlcompleter |
Brett Cannon | 260fbe8 | 2013-07-04 18:16:15 -0400 | [diff] [blame] | 79 | except ImportError: |
Antoine Pitrou | 4de39cd | 2009-10-10 21:08:31 +0000 | [diff] [blame] | 80 | pass |
Ezio Melotti | 8b23f5c | 2014-11-02 19:08:35 +0200 | [diff] [blame] | 81 | finally: |
| 82 | locale.setlocale(locale.LC_CTYPE, locale_tuple) |
Antoine Pitrou | 4de39cd | 2009-10-10 21:08:31 +0000 | [diff] [blame] | 83 | |
| 84 | ignored = [] |
| 85 | failed_imports = [] |
| 86 | lib_dir = os.path.dirname(os.path.dirname(__file__)) |
| 87 | for path, modname in self.walk_modules(lib_dir, ""): |
| 88 | m = modname |
| 89 | blacklisted = False |
| 90 | while m: |
| 91 | if m in blacklist: |
| 92 | blacklisted = True |
| 93 | break |
| 94 | m = m.rpartition('.')[0] |
| 95 | if blacklisted: |
| 96 | continue |
| 97 | if support.verbose: |
| 98 | print(modname) |
Walter Dörwald | b1ded1e | 2003-04-15 11:10:33 +0000 | [diff] [blame] | 99 | try: |
Antoine Pitrou | 4de39cd | 2009-10-10 21:08:31 +0000 | [diff] [blame] | 100 | # This heuristic speeds up the process by removing, de facto, |
| 101 | # most test modules (and avoiding the auto-executing ones). |
| 102 | with open(path, "rb") as f: |
| 103 | if b"__all__" not in f.read(): |
| 104 | raise NoAll(modname) |
| 105 | self.check_all(modname) |
| 106 | except NoAll: |
| 107 | ignored.append(modname) |
| 108 | except FailedImport: |
| 109 | failed_imports.append(modname) |
| 110 | |
| 111 | if support.verbose: |
| 112 | print('Following modules have no __all__ and have been ignored:', |
| 113 | ignored) |
| 114 | print('Following modules failed to be imported:', failed_imports) |
Walter Dörwald | b1ded1e | 2003-04-15 11:10:33 +0000 | [diff] [blame] | 115 | |
| 116 | |
Walter Dörwald | b1ded1e | 2003-04-15 11:10:33 +0000 | [diff] [blame] | 117 | if __name__ == "__main__": |
Brett Cannon | c9a1bfe | 2013-06-12 20:12:30 -0400 | [diff] [blame] | 118 | unittest.main() |