11import unittest
2- from test .support import run_unittest
2+ from test import support
3+ import os
34import sys
45import warnings
56
67
8+ class NoAll (RuntimeError ):
9+ pass
10+
11+ class FailedImport (RuntimeError ):
12+ pass
13+
14+
715class AllTest (unittest .TestCase ):
816
917 def check_all (self , modname ):
@@ -13,153 +21,97 @@ def check_all(self, modname):
1321 DeprecationWarning )
1422 try :
1523 exec ("import %s" % modname , names )
16- except ImportError :
24+ except :
1725 # Silent fail here seems the best route since some modules
18- # may not be available in all environments.
19- return
20- self .assertTrue (hasattr (sys .modules [modname ], "__all__" ),
21- "%s has no __all__ attribute" % modname )
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 )
2231 names = {}
23- exec ("from %s import *" % modname , names )
32+ try :
33+ exec ("from %s import *" % modname , names )
34+ except Exception as e :
35+ # Include the module name in the exception string
36+ self .fail ("__all__ failure in {}: {}: {}" .format (
37+ modname , e .__class__ .__name__ , e ))
2438 if "__builtins__" in names :
2539 del names ["__builtins__" ]
2640 keys = set (names )
2741 all = set (sys .modules [modname ].__all__ )
2842 self .assertEqual (keys , all )
2943
44+ def walk_modules (self , basedir , modpath ):
45+ for fn in sorted (os .listdir (basedir )):
46+ path = os .path .join (basedir , fn )
47+ if os .path .isdir (path ):
48+ pkg_init = os .path .join (path , '__init__.py' )
49+ if os .path .exists (pkg_init ):
50+ yield pkg_init , modpath + fn
51+ for p , m in self .walk_modules (path , modpath + fn + "." ):
52+ yield p , m
53+ continue
54+ if not fn .endswith ('.py' ) or fn == '__init__.py' :
55+ continue
56+ yield path , modpath + fn [:- 3 ]
57+
3058 def test_all (self ):
59+ # Blacklisted modules and packages
60+ blacklist = set ([
61+ # Will raise a SyntaxError when compiling the exec statement
62+ '__future__' ,
63+ ])
64+
3165 if not sys .platform .startswith ('java' ):
3266 # In case _socket fails to build, make this test fail more gracefully
3367 # than an AttributeError somewhere deep in CGIHTTPServer.
3468 import _socket
3569
36- self .check_all ("http.server" )
37- self .check_all ("configparser" )
38- self .check_all ("http.cookies" )
39- self .check_all ("queue" )
40- self .check_all ("socketserver" )
41- self .check_all ("aifc" )
42- self .check_all ("base64" )
43- self .check_all ("bdb" )
44- self .check_all ("binhex" )
45- self .check_all ("calendar" )
46- self .check_all ("collections" )
47- self .check_all ("cgi" )
48- self .check_all ("cmd" )
49- self .check_all ("code" )
50- self .check_all ("codecs" )
51- self .check_all ("codeop" )
52- self .check_all ("colorsys" )
53- self .check_all ("compileall" )
54- self .check_all ("copy" )
55- self .check_all ("copyreg" )
56- self .check_all ("csv" )
57- self .check_all ("dbm.bsd" )
58- self .check_all ("decimal" )
59- self .check_all ("difflib" )
60- self .check_all ("dircache" )
61- self .check_all ("dis" )
62- self .check_all ("doctest" )
63- self .check_all ("_dummy_thread" )
64- self .check_all ("dummy_threading" )
65- self .check_all ("filecmp" )
66- self .check_all ("fileinput" )
67- self .check_all ("fnmatch" )
68- self .check_all ("fpformat" )
69- self .check_all ("ftplib" )
70- self .check_all ("getopt" )
71- self .check_all ("getpass" )
72- self .check_all ("gettext" )
73- self .check_all ("glob" )
74- self .check_all ("gzip" )
75- self .check_all ("heapq" )
76- self .check_all ("http.client" )
77- self .check_all ("ihooks" )
78- self .check_all ("io" )
79- self .check_all ("_pyio" )
80- self .check_all ("imaplib" )
81- self .check_all ("imghdr" )
82- self .check_all ("keyword" )
83- self .check_all ("linecache" )
84- self .check_all ("locale" )
85- self .check_all ("logging" )
86- self .check_all ("macpath" )
87- self .check_all ("macurl2path" )
88- self .check_all ("mailbox" )
89- self .check_all ("mailcap" )
90- self .check_all ("mhlib" )
91- self .check_all ("mimetypes" )
92- self .check_all ("multifile" )
93- self .check_all ("netrc" )
94- self .check_all ("nntplib" )
95- self .check_all ("ntpath" )
96- self .check_all ("opcode" )
97- self .check_all ("optparse" )
98- self .check_all ("os" )
99- self .check_all ("os2emxpath" )
100- self .check_all ("pdb" )
101- self .check_all ("pickle" )
102- self .check_all ("pickletools" )
103- self .check_all ("pipes" )
104- self .check_all ("poplib" )
105- self .check_all ("posixpath" )
106- self .check_all ("pprint" )
107- self .check_all ("profile" )
108- self .check_all ("pstats" )
109- self .check_all ("pty" )
110- self .check_all ("py_compile" )
111- self .check_all ("pyclbr" )
112- self .check_all ("quopri" )
113- self .check_all ("random" )
114- self .check_all ("re" )
115- self .check_all ("reprlib" )
116- self .check_all ("rlcompleter" )
117- self .check_all ("urllib.robotparser" )
118- self .check_all ("sched" )
119- self .check_all ("shelve" )
120- self .check_all ("shlex" )
121- self .check_all ("shutil" )
122- self .check_all ("smtpd" )
123- self .check_all ("smtplib" )
124- self .check_all ("sndhdr" )
125- self .check_all ("socket" )
126- self .check_all ("_strptime" )
127- self .check_all ("symtable" )
128- self .check_all ("tabnanny" )
129- self .check_all ("tarfile" )
130- self .check_all ("telnetlib" )
131- self .check_all ("tempfile" )
132- self .check_all ("test.support" )
133- self .check_all ("textwrap" )
134- self .check_all ("threading" )
135- self .check_all ("timeit" )
136- self .check_all ("tokenize" )
137- self .check_all ("traceback" )
138- self .check_all ("tty" )
139- self .check_all ("unittest" )
140- self .check_all ("uu" )
141- self .check_all ("warnings" )
142- self .check_all ("wave" )
143- self .check_all ("weakref" )
144- self .check_all ("webbrowser" )
145- self .check_all ("xdrlib" )
146- self .check_all ("zipfile" )
147-
14870 # rlcompleter needs special consideration; it import readline which
14971 # initializes GNU readline which calls setlocale(LC_CTYPE, "")... :-(
15072 try :
151- self .check_all ("rlcompleter" )
152- finally :
73+ import rlcompleter
74+ import locale
75+ except ImportError :
76+ pass
77+ else :
78+ locale .setlocale (locale .LC_CTYPE , 'C' )
79+
80+ ignored = []
81+ failed_imports = []
82+ lib_dir = os .path .dirname (os .path .dirname (__file__ ))
83+ for path , modname in self .walk_modules (lib_dir , "" ):
84+ m = modname
85+ blacklisted = False
86+ while m :
87+ if m in blacklist :
88+ blacklisted = True
89+ break
90+ m = m .rpartition ('.' )[0 ]
91+ if blacklisted :
92+ continue
93+ if support .verbose :
94+ print (modname )
15395 try :
154- import locale
155- except ImportError :
156- pass
157- else :
158- locale .setlocale (locale .LC_CTYPE , 'C' )
96+ # This heuristic speeds up the process by removing, de facto,
97+ # most test modules (and avoiding the auto-executing ones).
98+ with open (path , "rb" ) as f :
99+ if b"__all__" not in f .read ():
100+ raise NoAll (modname )
101+ self .check_all (modname )
102+ except NoAll :
103+ ignored .append (modname )
104+ except FailedImport :
105+ failed_imports .append (modname )
106+
107+ if support .verbose :
108+ print ('Following modules have no __all__ and have been ignored:' ,
109+ ignored )
110+ print ('Following modules failed to be imported:' , failed_imports )
159111
160112
161113def test_main ():
162- run_unittest (AllTest )
114+ support . run_unittest (AllTest )
163115
164116if __name__ == "__main__" :
165117 test_main ()
0 commit comments