~ubuntu-branches/ubuntu/lucid/python2.6/lucid

« back to all changes in this revision

Viewing changes to Lib/test/test___all__.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2010-03-11 13:30:19 UTC
  • mto: (10.1.13 sid)
  • mto: This revision was merged to the branch mainline in revision 44.
  • Revision ID: james.westby@ubuntu.com-20100311133019-sblbooa3uqrkoe70
Tags: upstream-2.6.5~rc2
ImportĀ upstreamĀ versionĀ 2.6.5~rc2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from __future__ import print_function
 
2
 
1
3
import unittest
2
 
from test.test_support import run_unittest
 
4
from test import test_support as support
 
5
import os
3
6
import sys
4
7
import warnings
5
8
 
6
9
 
 
10
class NoAll(RuntimeError):
 
11
    pass
 
12
 
 
13
class FailedImport(RuntimeError):
 
14
    pass
 
15
 
7
16
 
8
17
class AllTest(unittest.TestCase):
9
18
 
14
23
                                    DeprecationWarning)
15
24
            try:
16
25
                exec "import %s" % modname in names
17
 
            except ImportError:
 
26
            except:
18
27
                # Silent fail here seems the best route since some modules
19
 
                # may not be available in all environments.
20
 
                return
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
 
29
                # environments.
 
30
                raise FailedImport(modname)
 
31
        if not hasattr(sys.modules[modname], "__all__"):
 
32
            raise NoAll(modname)
23
33
        names = {}
24
 
        exec "from %s import *" % modname in names
 
34
        try:
 
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__"]
27
42
        keys = set(names)
28
43
        all = set(sys.modules[modname].__all__)
29
44
        self.assertEqual(keys, all)
30
45
 
 
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 + "."):
 
54
                        yield p, m
 
55
                continue
 
56
            if not fn.endswith('.py') or fn == '__init__.py':
 
57
                continue
 
58
            yield path, modpath + fn[:-3]
 
59
 
31
60
    def test_all(self):
 
61
        # Blacklisted modules and packages
 
62
        blacklist = set([
 
63
            # Will raise a SyntaxError when compiling the exec statement
 
64
            '__future__',
 
65
        ])
 
66
 
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.
35
70
            import _socket
36
71
 
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")
52
 
        self.check_all("bdb")
53
 
        self.check_all("binhex")
54
 
        self.check_all("calendar")
55
 
        self.check_all("cgi")
56
 
        self.check_all("cmd")
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")
65
 
        self.check_all("csv")
66
 
        self.check_all("dbhash")
67
 
        self.check_all("decimal")
68
 
        self.check_all("difflib")
69
 
        self.check_all("dircache")
70
 
        self.check_all("dis")
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")
109
 
        self.check_all("os")
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")
126
 
        self.check_all("re")
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")
159
 
        self.check_all("uu")
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")
166
 
 
167
72
        # rlcompleter needs special consideration; it import readline which
168
73
        # initializes GNU readline which calls setlocale(LC_CTYPE, "")... :-(
169
74
        try:
170
 
            self.check_all("rlcompleter")
171
 
        finally:
 
75
            import rlcompleter
 
76
            import locale
 
77
        except ImportError:
 
78
            pass
 
79
        else:
 
80
            locale.setlocale(locale.LC_CTYPE, 'C')
 
81
 
 
82
        ignored = []
 
83
        failed_imports = []
 
84
        lib_dir = os.path.dirname(os.path.dirname(__file__))
 
85
        for path, modname in self.walk_modules(lib_dir, ""):
 
86
            m = modname
 
87
            blacklisted = False
 
88
            while m:
 
89
                if m in blacklist:
 
90
                    blacklisted = True
 
91
                    break
 
92
                m = m.rpartition('.')[0]
 
93
            if blacklisted:
 
94
                continue
 
95
            if support.verbose:
 
96
                print(modname)
172
97
            try:
173
 
                import locale
174
 
            except ImportError:
175
 
                pass
176
 
            else:
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():
 
102
                        raise NoAll(modname)
 
103
                    self.check_all(modname)
 
104
            except NoAll:
 
105
                ignored.append(modname)
 
106
            except FailedImport:
 
107
                failed_imports.append(modname)
 
108
 
 
109
        if support.verbose:
 
110
            print('Following modules have no __all__ and have been ignored:',
 
111
                  ignored)
 
112
            print('Following modules failed to be imported:', failed_imports)
178
113
 
179
114
 
180
115
def test_main():
181
 
    run_unittest(AllTest)
 
116
    support.run_unittest(AllTest)
182
117
 
183
118
if __name__ == "__main__":
184
119
    test_main()