annotate Lib/test/test___all__.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents b36cb4602e21
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
56783
a0ef4ef5fc88 Backporting since it would have helped us find a regression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56364
diff changeset
1 from __future__ import print_function
a0ef4ef5fc88 Backporting since it would have helped us find a regression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56364
diff changeset
2
28611
c71b9ba1f04c Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents: 25681
diff changeset
3 import unittest
56783
a0ef4ef5fc88 Backporting since it would have helped us find a regression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56364
diff changeset
4 from test import test_support as support
a0ef4ef5fc88 Backporting since it would have helped us find a regression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56364
diff changeset
5 import os
16070
b5345e99596b added __all__ lists to a number of Python modules
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
6 import sys
22954
9a58e97db447 Ignore more deprecation warnings.
Tim Peters <tim.peters@gmail.com>
parents: 21297
diff changeset
7
60283
4d84e3fa333a import bsddb more robustly
Benjamin Peterson <benjamin@python.org>
parents: 60282
diff changeset
8 # Setup bsddb warnings
4d84e3fa333a import bsddb more robustly
Benjamin Peterson <benjamin@python.org>
parents: 60282
diff changeset
9 try:
62165
f5ce5e1bb23a Silence deprecation warning in test___all__ caused by an import bsddb.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60283
diff changeset
10 bsddb = support.import_module('bsddb', deprecated=True)
f5ce5e1bb23a Silence deprecation warning in test___all__ caused by an import bsddb.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 60283
diff changeset
11 except unittest.SkipTest:
60283
4d84e3fa333a import bsddb more robustly
Benjamin Peterson <benjamin@python.org>
parents: 60282
diff changeset
12 pass
22954
9a58e97db447 Ignore more deprecation warnings.
Tim Peters <tim.peters@gmail.com>
parents: 21297
diff changeset
13
46701
0c2e4b55f80b When testing a module's __all__, we really don't care if it is deprecated.
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
14
56783
a0ef4ef5fc88 Backporting since it would have helped us find a regression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56364
diff changeset
15 class NoAll(RuntimeError):
a0ef4ef5fc88 Backporting since it would have helped us find a regression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56364
diff changeset
16 pass
a0ef4ef5fc88 Backporting since it would have helped us find a regression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56364
diff changeset
17
a0ef4ef5fc88 Backporting since it would have helped us find a regression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56364
diff changeset
18 class FailedImport(RuntimeError):
a0ef4ef5fc88 Backporting since it would have helped us find a regression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56364
diff changeset
19 pass
a0ef4ef5fc88 Backporting since it would have helped us find a regression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56364
diff changeset
20
16070
b5345e99596b added __all__ lists to a number of Python modules
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
21
28611
c71b9ba1f04c Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents: 25681
diff changeset
22 class AllTest(unittest.TestCase):
c71b9ba1f04c Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents: 25681
diff changeset
23
c71b9ba1f04c Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents: 25681
diff changeset
24 def check_all(self, modname):
c71b9ba1f04c Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents: 25681
diff changeset
25 names = {}
60116
3748bca4d869 Replace catch_warnings with check_warnings when it makes sense. Use assertRaises context manager to simplify some tests.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 56444
diff changeset
26 with support.check_warnings((".* (module|package)",
3748bca4d869 Replace catch_warnings with check_warnings when it makes sense. Use assertRaises context manager to simplify some tests.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 56444
diff changeset
27 DeprecationWarning), quiet=True):
46701
0c2e4b55f80b When testing a module's __all__, we really don't care if it is deprecated.
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
28 try:
0c2e4b55f80b When testing a module's __all__, we really don't care if it is deprecated.
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
29 exec "import %s" % modname in names
56783
a0ef4ef5fc88 Backporting since it would have helped us find a regression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56364
diff changeset
30 except:
46701
0c2e4b55f80b When testing a module's __all__, we really don't care if it is deprecated.
Brett Cannon <bcannon@gmail.com>
parents: 45468
diff changeset
31 # Silent fail here seems the best route since some modules
56783
a0ef4ef5fc88 Backporting since it would have helped us find a regression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56364
diff changeset
32 # may not be available or not initialize properly in all
a0ef4ef5fc88 Backporting since it would have helped us find a regression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56364
diff changeset
33 # environments.
a0ef4ef5fc88 Backporting since it would have helped us find a regression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56364
diff changeset
34 raise FailedImport(modname)
a0ef4ef5fc88 Backporting since it would have helped us find a regression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56364
diff changeset
35 if not hasattr(sys.modules[modname], "__all__"):
a0ef4ef5fc88 Backporting since it would have helped us find a regression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56364
diff changeset
36 raise NoAll(modname)
28611
c71b9ba1f04c Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents: 25681
diff changeset
37 names = {}
56783
a0ef4ef5fc88 Backporting since it would have helped us find a regression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56364
diff changeset
38 try:
a0ef4ef5fc88 Backporting since it would have helped us find a regression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56364
diff changeset
39 exec "from %s import *" % modname in names
a0ef4ef5fc88 Backporting since it would have helped us find a regression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56364
diff changeset
40 except Exception as e:
a0ef4ef5fc88 Backporting since it would have helped us find a regression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56364
diff changeset
41 # Include the module name in the exception string
56444
a2f02a125feb Issue #7055: test___all__ now greedily detects all modules which have an
Antoine Pitrou <solipsis@pitrou.net>
parents: 56366
diff changeset
42 self.fail("__all__ failure in {}: {}: {}".format(
56783
a0ef4ef5fc88 Backporting since it would have helped us find a regression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56364
diff changeset
43 modname, e.__class__.__name__, e))
41380
586a7e877500 Stop using test_support.verify().
Collin Winter <collinw@gmail.com>
parents: 40614
diff changeset
44 if "__builtins__" in names:
28611
c71b9ba1f04c Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents: 25681
diff changeset
45 del names["__builtins__"]
30929
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents: 30875
diff changeset
46 keys = set(names)
54ff68e973ee * Migrate set() and frozenset() from the sandbox.
Raymond Hettinger <python@rcn.com>
parents: 30875
diff changeset
47 all = set(sys.modules[modname].__all__)
41380
586a7e877500 Stop using test_support.verify().
Collin Winter <collinw@gmail.com>
parents: 40614
diff changeset
48 self.assertEqual(keys, all)
16070
b5345e99596b added __all__ lists to a number of Python modules
Skip Montanaro <skip@pobox.com>
parents:
diff changeset
49
56783
a0ef4ef5fc88 Backporting since it would have helped us find a regression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56364
diff changeset
50 def walk_modules(self, basedir, modpath):
a0ef4ef5fc88 Backporting since it would have helped us find a regression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56364
diff changeset
51 for fn in sorted(os.listdir(basedir)):
a0ef4ef5fc88 Backporting since it would have helped us find a regression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56364
diff changeset
52 path = os.path.join(basedir, fn)
a0ef4ef5fc88 Backporting since it would have helped us find a regression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56364
diff changeset
53 if os.path.isdir(path):
a0ef4ef5fc88 Backporting since it would have helped us find a regression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56364
diff changeset
54 pkg_init = os.path.join(path, '__init__.py')
a0ef4ef5fc88 Backporting since it would have helped us find a regression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56364
diff changeset
55 if os.path.exists(pkg_init):
a0ef4ef5fc88 Backporting since it would have helped us find a regression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56364
diff changeset
56 yield pkg_init, modpath + fn
a0ef4ef5fc88 Backporting since it would have helped us find a regression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56364
diff changeset
57 for p, m in self.walk_modules(path, modpath + fn + "."):
a0ef4ef5fc88 Backporting since it would have helped us find a regression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56364
diff changeset
58 yield p, m
a0ef4ef5fc88 Backporting since it would have helped us find a regression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56364
diff changeset
59 continue
a0ef4ef5fc88 Backporting since it would have helped us find a regression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56364
diff changeset
60 if not fn.endswith('.py') or fn == '__init__.py':
a0ef4ef5fc88 Backporting since it would have helped us find a regression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56364
diff changeset
61 continue
a0ef4ef5fc88 Backporting since it would have helped us find a regression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56364
diff changeset
62 yield path, modpath + fn[:-3]
a0ef4ef5fc88 Backporting since it would have helped us find a regression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56364
diff changeset
63
28611
c71b9ba1f04c Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents: 25681
diff changeset
64 def test_all(self):
56783
a0ef4ef5fc88 Backporting since it would have helped us find a regression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56364
diff changeset
65 # Blacklisted modules and packages
a0ef4ef5fc88 Backporting since it would have helped us find a regression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56364
diff changeset
66 blacklist = set([
a0ef4ef5fc88 Backporting since it would have helped us find a regression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56364
diff changeset
67 # Will raise a SyntaxError when compiling the exec statement
a0ef4ef5fc88 Backporting since it would have helped us find a regression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56364
diff changeset
68 '__future__',
a0ef4ef5fc88 Backporting since it would have helped us find a regression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56364
diff changeset
69 ])
a0ef4ef5fc88 Backporting since it would have helped us find a regression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56364
diff changeset
70
28611
c71b9ba1f04c Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents: 25681
diff changeset
71 if not sys.platform.startswith('java'):
c71b9ba1f04c Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents: 25681
diff changeset
72 # In case _socket fails to build, make this test fail more gracefully
c71b9ba1f04c Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents: 25681
diff changeset
73 # than an AttributeError somewhere deep in CGIHTTPServer.
c71b9ba1f04c Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents: 25681
diff changeset
74 import _socket
19119
600a9a2b7a18 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 18994
diff changeset
75
28611
c71b9ba1f04c Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents: 25681
diff changeset
76 # rlcompleter needs special consideration; it import readline which
c71b9ba1f04c Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents: 25681
diff changeset
77 # initializes GNU readline which calls setlocale(LC_CTYPE, "")... :-(
c71b9ba1f04c Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents: 25681
diff changeset
78 try:
56783
a0ef4ef5fc88 Backporting since it would have helped us find a regression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56364
diff changeset
79 import rlcompleter
a0ef4ef5fc88 Backporting since it would have helped us find a regression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56364
diff changeset
80 import locale
a0ef4ef5fc88 Backporting since it would have helped us find a regression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56364
diff changeset
81 except ImportError:
a0ef4ef5fc88 Backporting since it would have helped us find a regression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56364
diff changeset
82 pass
a0ef4ef5fc88 Backporting since it would have helped us find a regression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56364
diff changeset
83 else:
a0ef4ef5fc88 Backporting since it would have helped us find a regression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56364
diff changeset
84 locale.setlocale(locale.LC_CTYPE, 'C')
a0ef4ef5fc88 Backporting since it would have helped us find a regression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56364
diff changeset
85
a0ef4ef5fc88 Backporting since it would have helped us find a regression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56364
diff changeset
86 ignored = []
a0ef4ef5fc88 Backporting since it would have helped us find a regression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56364
diff changeset
87 failed_imports = []
a0ef4ef5fc88 Backporting since it would have helped us find a regression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56364
diff changeset
88 lib_dir = os.path.dirname(os.path.dirname(__file__))
a0ef4ef5fc88 Backporting since it would have helped us find a regression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56364
diff changeset
89 for path, modname in self.walk_modules(lib_dir, ""):
a0ef4ef5fc88 Backporting since it would have helped us find a regression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56364
diff changeset
90 m = modname
a0ef4ef5fc88 Backporting since it would have helped us find a regression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56364
diff changeset
91 blacklisted = False
a0ef4ef5fc88 Backporting since it would have helped us find a regression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56364
diff changeset
92 while m:
a0ef4ef5fc88 Backporting since it would have helped us find a regression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56364
diff changeset
93 if m in blacklist:
a0ef4ef5fc88 Backporting since it would have helped us find a regression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56364
diff changeset
94 blacklisted = True
a0ef4ef5fc88 Backporting since it would have helped us find a regression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56364
diff changeset
95 break
a0ef4ef5fc88 Backporting since it would have helped us find a regression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56364
diff changeset
96 m = m.rpartition('.')[0]
a0ef4ef5fc88 Backporting since it would have helped us find a regression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56364
diff changeset
97 if blacklisted:
a0ef4ef5fc88 Backporting since it would have helped us find a regression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56364
diff changeset
98 continue
a0ef4ef5fc88 Backporting since it would have helped us find a regression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56364
diff changeset
99 if support.verbose:
a0ef4ef5fc88 Backporting since it would have helped us find a regression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56364
diff changeset
100 print(modname)
28611
c71b9ba1f04c Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents: 25681
diff changeset
101 try:
56783
a0ef4ef5fc88 Backporting since it would have helped us find a regression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56364
diff changeset
102 # This heuristic speeds up the process by removing, de facto,
a0ef4ef5fc88 Backporting since it would have helped us find a regression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56364
diff changeset
103 # most test modules (and avoiding the auto-executing ones).
a0ef4ef5fc88 Backporting since it would have helped us find a regression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56364
diff changeset
104 with open(path, "rb") as f:
a0ef4ef5fc88 Backporting since it would have helped us find a regression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56364
diff changeset
105 if "__all__" not in f.read():
a0ef4ef5fc88 Backporting since it would have helped us find a regression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56364
diff changeset
106 raise NoAll(modname)
a0ef4ef5fc88 Backporting since it would have helped us find a regression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56364
diff changeset
107 self.check_all(modname)
a0ef4ef5fc88 Backporting since it would have helped us find a regression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56364
diff changeset
108 except NoAll:
a0ef4ef5fc88 Backporting since it would have helped us find a regression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56364
diff changeset
109 ignored.append(modname)
a0ef4ef5fc88 Backporting since it would have helped us find a regression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56364
diff changeset
110 except FailedImport:
a0ef4ef5fc88 Backporting since it would have helped us find a regression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56364
diff changeset
111 failed_imports.append(modname)
a0ef4ef5fc88 Backporting since it would have helped us find a regression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56364
diff changeset
112
a0ef4ef5fc88 Backporting since it would have helped us find a regression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56364
diff changeset
113 if support.verbose:
a0ef4ef5fc88 Backporting since it would have helped us find a regression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56364
diff changeset
114 print('Following modules have no __all__ and have been ignored:',
a0ef4ef5fc88 Backporting since it would have helped us find a regression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56364
diff changeset
115 ignored)
a0ef4ef5fc88 Backporting since it would have helped us find a regression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56364
diff changeset
116 print('Following modules failed to be imported:', failed_imports)
28611
c71b9ba1f04c Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents: 25681
diff changeset
117
c71b9ba1f04c Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents: 25681
diff changeset
118
c71b9ba1f04c Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents: 25681
diff changeset
119 def test_main():
56783
a0ef4ef5fc88 Backporting since it would have helped us find a regression.
Antoine Pitrou <solipsis@pitrou.net>
parents: 56364
diff changeset
120 support.run_unittest(AllTest)
28611
c71b9ba1f04c Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents: 25681
diff changeset
121
c71b9ba1f04c Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents: 25681
diff changeset
122 if __name__ == "__main__":
c71b9ba1f04c Port test_pwd.py to PyUnit. Check that getpwall() and
Walter Dörwald <walter@livinglogic.de>
parents: 25681
diff changeset
123 test_main()