1 from __future__
import print_function
4 from test
import test_support
as support
10 bsddb
= support
.import_module('bsddb', deprecated
=True)
11 except unittest
.SkipTest
:
15 class NoAll(RuntimeError):
18 class FailedImport(RuntimeError):
22 class AllTest(unittest
.TestCase
):
24 def check_all(self
, modname
):
26 with support
.check_warnings((".* (module|package)",
27 DeprecationWarning), quiet
=True):
29 exec "import %s" % modname
in names
31 # Silent fail here seems the best route since some modules
32 # may not be available or not initialize properly in all
34 raise FailedImport(modname
)
35 if not hasattr(sys
.modules
[modname
], "__all__"):
39 exec "from %s import *" % modname
in names
40 except Exception as e
:
41 # Include the module name in the exception string
42 self
.fail("__all__ failure in {}: {}: {}".format(
43 modname
, e
.__class
__.__name
__, e
))
44 if "__builtins__" in names
:
45 del names
["__builtins__"]
47 all
= set(sys
.modules
[modname
].__all
__)
48 self
.assertEqual(keys
, all
)
50 def walk_modules(self
, basedir
, modpath
):
51 for fn
in sorted(os
.listdir(basedir
)):
52 path
= os
.path
.join(basedir
, fn
)
53 if os
.path
.isdir(path
):
54 pkg_init
= os
.path
.join(path
, '__init__.py')
55 if os
.path
.exists(pkg_init
):
56 yield pkg_init
, modpath
+ fn
57 for p
, m
in self
.walk_modules(path
, modpath
+ fn
+ "."):
60 if not fn
.endswith('.py') or fn
== '__init__.py':
62 yield path
, modpath
+ fn
[:-3]
65 # Blacklisted modules and packages
67 # Will raise a SyntaxError when compiling the exec statement
71 if not sys
.platform
.startswith('java'):
72 # In case _socket fails to build, make this test fail more gracefully
73 # than an AttributeError somewhere deep in CGIHTTPServer.
76 # rlcompleter needs special consideration; it import readline which
77 # initializes GNU readline which calls setlocale(LC_CTYPE, "")... :-(
84 locale
.setlocale(locale
.LC_CTYPE
, 'C')
88 lib_dir
= os
.path
.dirname(os
.path
.dirname(__file__
))
89 for path
, modname
in self
.walk_modules(lib_dir
, ""):
96 m
= m
.rpartition('.')[0]
102 # This heuristic speeds up the process by removing, de facto,
103 # most test modules (and avoiding the auto-executing ones).
104 with
open(path
, "rb") as f
:
105 if "__all__" not in f
.read():
107 self
.check_all(modname
)
109 ignored
.append(modname
)
111 failed_imports
.append(modname
)
114 print('Following modules have no __all__ and have been ignored:',
116 print('Following modules failed to be imported:', failed_imports
)
120 support
.run_unittest(AllTest
)
122 if __name__
== "__main__":