14
23
DeprecationWarning)
16
25
exec "import %s" % modname in names
18
27
# Silent fail here seems the best route since some modules
19
# may not be available in all environments.
21
self.failUnless(hasattr(sys.modules[modname], "__all__"),
22
"%s has no __all__ attribute" % modname)
28
# may not be available or not initialize properly in all
30
raise FailedImport(modname)
31
if not hasattr(sys.modules[modname], "__all__"):
24
exec "from %s import *" % modname in names
35
exec "from %s import *" % modname in names
36
except Exception as e:
37
# Include the module name in the exception string
38
self.fail("__all__ failure in {0}: {1}: {2}".format(
39
modname, e.__class__.__name__, e))
25
40
if "__builtins__" in names:
26
41
del names["__builtins__"]
28
43
all = set(sys.modules[modname].__all__)
29
44
self.assertEqual(keys, all)
46
def walk_modules(self, basedir, modpath):
47
for fn in sorted(os.listdir(basedir)):
48
path = os.path.join(basedir, fn)
49
if os.path.isdir(path):
50
pkg_init = os.path.join(path, '__init__.py')
51
if os.path.exists(pkg_init):
52
yield pkg_init, modpath + fn
53
for p, m in self.walk_modules(path, modpath + fn + "."):
56
if not fn.endswith('.py') or fn == '__init__.py':
58
yield path, modpath + fn[:-3]
31
60
def test_all(self):
61
# Blacklisted modules and packages
63
# Will raise a SyntaxError when compiling the exec statement
32
67
if not sys.platform.startswith('java'):
33
68
# In case _socket fails to build, make this test fail more gracefully
34
69
# than an AttributeError somewhere deep in CGIHTTPServer.
37
self.check_all("BaseHTTPServer")
38
self.check_all("Bastion")
39
self.check_all("CGIHTTPServer")
40
self.check_all("ConfigParser")
41
self.check_all("Cookie")
42
self.check_all("MimeWriter")
43
self.check_all("Queue")
44
self.check_all("SimpleHTTPServer")
45
self.check_all("SocketServer")
46
self.check_all("StringIO")
47
self.check_all("UserString")
48
self.check_all("aifc")
49
self.check_all("atexit")
50
self.check_all("audiodev")
51
self.check_all("base64")
53
self.check_all("binhex")
54
self.check_all("calendar")
57
self.check_all("code")
58
self.check_all("codecs")
59
self.check_all("codeop")
60
self.check_all("colorsys")
61
self.check_all("commands")
62
self.check_all("compileall")
63
self.check_all("copy")
64
self.check_all("copy_reg")
66
self.check_all("dbhash")
67
self.check_all("decimal")
68
self.check_all("difflib")
69
self.check_all("dircache")
71
self.check_all("doctest")
72
self.check_all("dummy_thread")
73
self.check_all("dummy_threading")
74
self.check_all("filecmp")
75
self.check_all("fileinput")
76
self.check_all("fnmatch")
77
self.check_all("fpformat")
78
self.check_all("ftplib")
79
self.check_all("getopt")
80
self.check_all("getpass")
81
self.check_all("gettext")
82
self.check_all("glob")
83
self.check_all("gzip")
84
self.check_all("heapq")
85
self.check_all("htmllib")
86
self.check_all("httplib")
87
self.check_all("ihooks")
88
self.check_all("imaplib")
89
self.check_all("imghdr")
90
self.check_all("imputil")
91
self.check_all("keyword")
92
self.check_all("linecache")
93
self.check_all("locale")
94
self.check_all("logging")
95
self.check_all("macpath")
96
self.check_all("macurl2path")
97
self.check_all("mailbox")
98
self.check_all("mailcap")
99
self.check_all("mhlib")
100
self.check_all("mimetools")
101
self.check_all("mimetypes")
102
self.check_all("mimify")
103
self.check_all("multifile")
104
self.check_all("netrc")
105
self.check_all("nntplib")
106
self.check_all("ntpath")
107
self.check_all("opcode")
108
self.check_all("optparse")
110
self.check_all("os2emxpath")
111
self.check_all("pdb")
112
self.check_all("pickle")
113
self.check_all("pickletools")
114
self.check_all("pipes")
115
self.check_all("popen2")
116
self.check_all("poplib")
117
self.check_all("posixpath")
118
self.check_all("pprint")
119
self.check_all("profile")
120
self.check_all("pstats")
121
self.check_all("pty")
122
self.check_all("py_compile")
123
self.check_all("pyclbr")
124
self.check_all("quopri")
125
self.check_all("random")
127
self.check_all("repr")
128
self.check_all("rexec")
129
self.check_all("rfc822")
130
self.check_all("rlcompleter")
131
self.check_all("robotparser")
132
self.check_all("sched")
133
self.check_all("sets")
134
self.check_all("sgmllib")
135
self.check_all("shelve")
136
self.check_all("shlex")
137
self.check_all("shutil")
138
self.check_all("smtpd")
139
self.check_all("smtplib")
140
self.check_all("sndhdr")
141
self.check_all("socket")
142
self.check_all("_strptime")
143
self.check_all("symtable")
144
self.check_all("tabnanny")
145
self.check_all("tarfile")
146
self.check_all("telnetlib")
147
self.check_all("tempfile")
148
self.check_all("test.test_support")
149
self.check_all("textwrap")
150
self.check_all("threading")
151
self.check_all("timeit")
152
self.check_all("toaiff")
153
self.check_all("tokenize")
154
self.check_all("traceback")
155
self.check_all("tty")
156
self.check_all("unittest")
157
self.check_all("urllib")
158
self.check_all("urlparse")
160
self.check_all("warnings")
161
self.check_all("wave")
162
self.check_all("weakref")
163
self.check_all("webbrowser")
164
self.check_all("xdrlib")
165
self.check_all("zipfile")
167
72
# rlcompleter needs special consideration; it import readline which
168
73
# initializes GNU readline which calls setlocale(LC_CTYPE, "")... :-(
170
self.check_all("rlcompleter")
80
locale.setlocale(locale.LC_CTYPE, 'C')
84
lib_dir = os.path.dirname(os.path.dirname(__file__))
85
for path, modname in self.walk_modules(lib_dir, ""):
92
m = m.rpartition('.')[0]
177
locale.setlocale(locale.LC_CTYPE, 'C')
98
# This heuristic speeds up the process by removing, de facto,
99
# most test modules (and avoiding the auto-executing ones).
100
with open(path, "rb") as f:
101
if "__all__" not in f.read():
103
self.check_all(modname)
105
ignored.append(modname)
107
failed_imports.append(modname)
110
print('Following modules have no __all__ and have been ignored:',
112
print('Following modules failed to be imported:', failed_imports)
181
run_unittest(AllTest)
116
support.run_unittest(AllTest)
183
118
if __name__ == "__main__":