blob: 1e368d5a6b2842b65efe661ac61d3d60e164000c [file] [log] [blame]
Steven Bethardae42f332008-03-18 17:26:10 +00001import unittest
Brett Cannon977eb022008-03-19 17:37:43 +00002import sys
Nick Coghland2e09382008-09-11 12:11:06 +00003from test.test_support import (check_warnings, CleanImport,
4 TestSkipped, run_unittest)
Steven Bethardae42f332008-03-18 17:26:10 +00005import warnings
6
Benjamin Petersona6864e02008-07-14 17:42:17 +00007from contextlib import nested
8
Brett Cannon977eb022008-03-19 17:37:43 +00009if not sys.py3kwarning:
10 raise TestSkipped('%s must be run with the -3 flag' % __name__)
11
Nick Coghland2e09382008-09-11 12:11:06 +000012def reset_module_registry(module):
13 try:
14 registry = module.__warningregistry__
15 except AttributeError:
16 pass
17 else:
18 registry.clear()
Steven Bethardae42f332008-03-18 17:26:10 +000019
20class TestPy3KWarnings(unittest.TestCase):
21
Nick Coghlan48361f52008-08-11 15:45:58 +000022 def assertWarning(self, _, warning, expected_message):
Nick Coghland2e09382008-09-11 12:11:06 +000023 self.assertEqual(str(warning.message), expected_message)
Nick Coghlan48361f52008-08-11 15:45:58 +000024
Benjamin Peterson2fe3ef82008-06-08 02:05:33 +000025 def test_backquote(self):
26 expected = 'backquote not supported in 3.x; use repr()'
Nick Coghland2e09382008-09-11 12:11:06 +000027 with check_warnings() as w:
Benjamin Peterson2fe3ef82008-06-08 02:05:33 +000028 exec "`2`" in {}
29 self.assertWarning(None, w, expected)
30
Benjamin Peterson399b1fe2008-10-25 02:53:28 +000031 def test_forbidden_names(self):
Benjamin Petersond5efd202008-06-08 22:52:37 +000032 # So we don't screw up our globals
33 def safe_exec(expr):
34 def f(**kwargs): pass
35 exec expr in {'f' : f}
36
Benjamin Peterson399b1fe2008-10-25 02:53:28 +000037 tests = [("True", "assignment to True or False is forbidden in 3.x"),
38 ("False", "assignment to True or False is forbidden in 3.x"),
39 ("nonlocal", "nonlocal is a keyword in 3.x")]
Nick Coghland2e09382008-09-11 12:11:06 +000040 with check_warnings() as w:
Benjamin Peterson399b1fe2008-10-25 02:53:28 +000041 for keyword, expected in tests:
42 safe_exec("{0} = False".format(keyword))
43 self.assertWarning(None, w, expected)
44 w.reset()
45 try:
46 safe_exec("obj.{0} = True".format(keyword))
47 except NameError:
48 pass
49 self.assertWarning(None, w, expected)
50 w.reset()
51 safe_exec("def {0}(): pass".format(keyword))
52 self.assertWarning(None, w, expected)
53 w.reset()
54 safe_exec("class {0}: pass".format(keyword))
55 self.assertWarning(None, w, expected)
56 w.reset()
57 safe_exec("def f({0}=43): pass".format(keyword))
58 self.assertWarning(None, w, expected)
59 w.reset()
Benjamin Petersond5efd202008-06-08 22:52:37 +000060
61
Steven Bethardae42f332008-03-18 17:26:10 +000062 def test_type_inequality_comparisons(self):
Georg Brandld5b635f2008-03-25 08:29:14 +000063 expected = 'type inequality comparisons not supported in 3.x'
Nick Coghland2e09382008-09-11 12:11:06 +000064 with check_warnings() as w:
Steven Bethardae42f332008-03-18 17:26:10 +000065 self.assertWarning(int < str, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +000066 w.reset()
Steven Bethardae42f332008-03-18 17:26:10 +000067 self.assertWarning(type < object, w, expected)
68
69 def test_object_inequality_comparisons(self):
Georg Brandld5b635f2008-03-25 08:29:14 +000070 expected = 'comparing unequal types not supported in 3.x'
Nick Coghland2e09382008-09-11 12:11:06 +000071 with check_warnings() as w:
Steven Bethardae42f332008-03-18 17:26:10 +000072 self.assertWarning(str < [], w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +000073 w.reset()
Steven Bethardae42f332008-03-18 17:26:10 +000074 self.assertWarning(object() < (1, 2), w, expected)
75
76 def test_dict_inequality_comparisons(self):
Georg Brandld5b635f2008-03-25 08:29:14 +000077 expected = 'dict inequality comparisons not supported in 3.x'
Nick Coghland2e09382008-09-11 12:11:06 +000078 with check_warnings() as w:
Steven Bethardae42f332008-03-18 17:26:10 +000079 self.assertWarning({} < {2:3}, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +000080 w.reset()
Steven Bethardae42f332008-03-18 17:26:10 +000081 self.assertWarning({} <= {}, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +000082 w.reset()
Steven Bethardae42f332008-03-18 17:26:10 +000083 self.assertWarning({} > {2:3}, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +000084 w.reset()
Steven Bethardae42f332008-03-18 17:26:10 +000085 self.assertWarning({2:3} >= {}, w, expected)
86
87 def test_cell_inequality_comparisons(self):
Georg Brandld5b635f2008-03-25 08:29:14 +000088 expected = 'cell comparisons not supported in 3.x'
Steven Bethardae42f332008-03-18 17:26:10 +000089 def f(x):
90 def g():
91 return x
92 return g
93 cell0, = f(0).func_closure
94 cell1, = f(1).func_closure
Nick Coghland2e09382008-09-11 12:11:06 +000095 with check_warnings() as w:
Steven Bethardae42f332008-03-18 17:26:10 +000096 self.assertWarning(cell0 == cell1, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +000097 w.reset()
Steven Bethardae42f332008-03-18 17:26:10 +000098 self.assertWarning(cell0 < cell1, w, expected)
99
Steven Bethard6a644f92008-03-18 22:08:20 +0000100 def test_code_inequality_comparisons(self):
Georg Brandld5b635f2008-03-25 08:29:14 +0000101 expected = 'code inequality comparisons not supported in 3.x'
Steven Bethard6a644f92008-03-18 22:08:20 +0000102 def f(x):
103 pass
104 def g(x):
105 pass
Nick Coghland2e09382008-09-11 12:11:06 +0000106 with check_warnings() as w:
Steven Bethard6a644f92008-03-18 22:08:20 +0000107 self.assertWarning(f.func_code < g.func_code, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000108 w.reset()
Steven Bethard6a644f92008-03-18 22:08:20 +0000109 self.assertWarning(f.func_code <= g.func_code, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000110 w.reset()
Steven Bethard6a644f92008-03-18 22:08:20 +0000111 self.assertWarning(f.func_code >= g.func_code, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000112 w.reset()
Steven Bethard6a644f92008-03-18 22:08:20 +0000113 self.assertWarning(f.func_code > g.func_code, w, expected)
114
115 def test_builtin_function_or_method_comparisons(self):
116 expected = ('builtin_function_or_method '
Georg Brandld5b635f2008-03-25 08:29:14 +0000117 'inequality comparisons not supported in 3.x')
Steven Bethard6a644f92008-03-18 22:08:20 +0000118 func = eval
119 meth = {}.get
Nick Coghland2e09382008-09-11 12:11:06 +0000120 with check_warnings() as w:
Steven Bethard6a644f92008-03-18 22:08:20 +0000121 self.assertWarning(func < meth, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000122 w.reset()
Steven Bethard6a644f92008-03-18 22:08:20 +0000123 self.assertWarning(func > meth, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000124 w.reset()
Steven Bethard6a644f92008-03-18 22:08:20 +0000125 self.assertWarning(meth <= func, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000126 w.reset()
Steven Bethard6a644f92008-03-18 22:08:20 +0000127 self.assertWarning(meth >= func, w, expected)
128
Benjamin Petersonf09925d2008-12-22 20:16:25 +0000129 def test_frame_attributes(self):
130 template = "%s has been removed in 3.x"
131 f = sys._getframe(0)
132 for attr in ("f_exc_traceback", "f_exc_value", "f_exc_type"):
133 expected = template % attr
134 with check_warnings() as w:
135 self.assertWarning(getattr(f, attr), w, expected)
136 w.reset()
137 self.assertWarning(setattr(f, attr, None), w, expected)
138
Raymond Hettinger05387862008-03-19 17:45:19 +0000139 def test_sort_cmp_arg(self):
Georg Brandld5b635f2008-03-25 08:29:14 +0000140 expected = "the cmp argument is not supported in 3.x"
Raymond Hettinger05387862008-03-19 17:45:19 +0000141 lst = range(5)
142 cmp = lambda x,y: -1
143
Nick Coghland2e09382008-09-11 12:11:06 +0000144 with check_warnings() as w:
Raymond Hettinger05387862008-03-19 17:45:19 +0000145 self.assertWarning(lst.sort(cmp=cmp), w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000146 w.reset()
Raymond Hettinger05387862008-03-19 17:45:19 +0000147 self.assertWarning(sorted(lst, cmp=cmp), w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000148 w.reset()
Raymond Hettinger05387862008-03-19 17:45:19 +0000149 self.assertWarning(lst.sort(cmp), w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000150 w.reset()
Raymond Hettinger05387862008-03-19 17:45:19 +0000151 self.assertWarning(sorted(lst, cmp), w, expected)
152
Georg Brandl5a444242008-03-21 20:11:46 +0000153 def test_sys_exc_clear(self):
Georg Brandld5b635f2008-03-25 08:29:14 +0000154 expected = 'sys.exc_clear() not supported in 3.x; use except clauses'
Nick Coghland2e09382008-09-11 12:11:06 +0000155 with check_warnings() as w:
Georg Brandl5a444242008-03-21 20:11:46 +0000156 self.assertWarning(sys.exc_clear(), w, expected)
157
Georg Brandl07e56812008-03-21 20:21:46 +0000158 def test_methods_members(self):
159 expected = '__members__ and __methods__ not supported in 3.x'
160 class C:
161 __methods__ = ['a']
162 __members__ = ['b']
163 c = C()
Nick Coghland2e09382008-09-11 12:11:06 +0000164 with check_warnings() as w:
Georg Brandl07e56812008-03-21 20:21:46 +0000165 self.assertWarning(dir(c), w, expected)
166
Georg Brandl65bb42d2008-03-21 20:38:24 +0000167 def test_softspace(self):
168 expected = 'file.softspace not supported in 3.x'
169 with file(__file__) as f:
Nick Coghland2e09382008-09-11 12:11:06 +0000170 with check_warnings() as w:
Georg Brandl65bb42d2008-03-21 20:38:24 +0000171 self.assertWarning(f.softspace, w, expected)
172 def set():
173 f.softspace = 0
Nick Coghland2e09382008-09-11 12:11:06 +0000174 with check_warnings() as w:
Georg Brandl65bb42d2008-03-21 20:38:24 +0000175 self.assertWarning(set(), w, expected)
176
Benjamin Peterson712ee922008-08-24 18:10:20 +0000177 def test_slice_methods(self):
178 class Spam(object):
179 def __getslice__(self, i, j): pass
180 def __setslice__(self, i, j, what): pass
181 def __delslice__(self, i, j): pass
182 class Egg:
183 def __getslice__(self, i, h): pass
184 def __setslice__(self, i, j, what): pass
185 def __delslice__(self, i, j): pass
186
187 expected = "in 3.x, __{0}slice__ has been removed; use __{0}item__"
188
189 for obj in (Spam(), Egg()):
Nick Coghland2e09382008-09-11 12:11:06 +0000190 with check_warnings() as w:
Benjamin Peterson712ee922008-08-24 18:10:20 +0000191 self.assertWarning(obj[1:2], w, expected.format('get'))
Nick Coghland2e09382008-09-11 12:11:06 +0000192 w.reset()
Benjamin Peterson712ee922008-08-24 18:10:20 +0000193 del obj[3:4]
194 self.assertWarning(None, w, expected.format('del'))
Nick Coghland2e09382008-09-11 12:11:06 +0000195 w.reset()
Benjamin Peterson712ee922008-08-24 18:10:20 +0000196 obj[4:5] = "eggs"
197 self.assertWarning(None, w, expected.format('set'))
198
Benjamin Petersonf4fcdb62008-06-08 23:00:00 +0000199 def test_tuple_parameter_unpacking(self):
200 expected = "tuple parameter unpacking has been removed in 3.x"
Nick Coghland2e09382008-09-11 12:11:06 +0000201 with check_warnings() as w:
Benjamin Petersonf4fcdb62008-06-08 23:00:00 +0000202 exec "def f((a, b)): pass"
203 self.assertWarning(None, w, expected)
204
Georg Brandl80055f62008-03-25 07:56:27 +0000205 def test_buffer(self):
Nick Coghlan48361f52008-08-11 15:45:58 +0000206 expected = 'buffer() not supported in 3.x'
Nick Coghland2e09382008-09-11 12:11:06 +0000207 with check_warnings() as w:
Georg Brandl80055f62008-03-25 07:56:27 +0000208 self.assertWarning(buffer('a'), w, expected)
209
Georg Brandla9916b52008-05-17 22:11:54 +0000210 def test_file_xreadlines(self):
211 expected = ("f.xreadlines() not supported in 3.x, "
212 "try 'for line in f' instead")
213 with file(__file__) as f:
Nick Coghland2e09382008-09-11 12:11:06 +0000214 with check_warnings() as w:
Georg Brandla9916b52008-05-17 22:11:54 +0000215 self.assertWarning(f.xreadlines(), w, expected)
216
Nick Coghlan48361f52008-08-11 15:45:58 +0000217 def test_hash_inheritance(self):
Nick Coghland2e09382008-09-11 12:11:06 +0000218 with check_warnings() as w:
Nick Coghlan48361f52008-08-11 15:45:58 +0000219 # With object as the base class
220 class WarnOnlyCmp(object):
221 def __cmp__(self, other): pass
Nick Coghland2e09382008-09-11 12:11:06 +0000222 self.assertEqual(len(w.warnings), 1)
Nick Coghlan48361f52008-08-11 15:45:58 +0000223 self.assertWarning(None, w,
224 "Overriding __cmp__ blocks inheritance of __hash__ in 3.x")
Nick Coghland2e09382008-09-11 12:11:06 +0000225 w.reset()
Nick Coghlan48361f52008-08-11 15:45:58 +0000226 class WarnOnlyEq(object):
227 def __eq__(self, other): pass
Nick Coghland2e09382008-09-11 12:11:06 +0000228 self.assertEqual(len(w.warnings), 1)
Nick Coghlan48361f52008-08-11 15:45:58 +0000229 self.assertWarning(None, w,
230 "Overriding __eq__ blocks inheritance of __hash__ in 3.x")
Nick Coghland2e09382008-09-11 12:11:06 +0000231 w.reset()
Nick Coghlan48361f52008-08-11 15:45:58 +0000232 class WarnCmpAndEq(object):
233 def __cmp__(self, other): pass
234 def __eq__(self, other): pass
Nick Coghland2e09382008-09-11 12:11:06 +0000235 self.assertEqual(len(w.warnings), 2)
236 self.assertWarning(None, w.warnings[0],
Nick Coghlan48361f52008-08-11 15:45:58 +0000237 "Overriding __cmp__ blocks inheritance of __hash__ in 3.x")
238 self.assertWarning(None, w,
239 "Overriding __eq__ blocks inheritance of __hash__ in 3.x")
Nick Coghland2e09382008-09-11 12:11:06 +0000240 w.reset()
Nick Coghlan48361f52008-08-11 15:45:58 +0000241 class NoWarningOnlyHash(object):
242 def __hash__(self): pass
Nick Coghland2e09382008-09-11 12:11:06 +0000243 self.assertEqual(len(w.warnings), 0)
Nick Coghlan48361f52008-08-11 15:45:58 +0000244 # With an intermediate class in the heirarchy
245 class DefinesAllThree(object):
246 def __cmp__(self, other): pass
247 def __eq__(self, other): pass
248 def __hash__(self): pass
249 class WarnOnlyCmp(DefinesAllThree):
250 def __cmp__(self, other): pass
Nick Coghland2e09382008-09-11 12:11:06 +0000251 self.assertEqual(len(w.warnings), 1)
Nick Coghlan48361f52008-08-11 15:45:58 +0000252 self.assertWarning(None, w,
253 "Overriding __cmp__ blocks inheritance of __hash__ in 3.x")
Nick Coghland2e09382008-09-11 12:11:06 +0000254 w.reset()
Nick Coghlan48361f52008-08-11 15:45:58 +0000255 class WarnOnlyEq(DefinesAllThree):
256 def __eq__(self, other): pass
Nick Coghland2e09382008-09-11 12:11:06 +0000257 self.assertEqual(len(w.warnings), 1)
Nick Coghlan48361f52008-08-11 15:45:58 +0000258 self.assertWarning(None, w,
259 "Overriding __eq__ blocks inheritance of __hash__ in 3.x")
Nick Coghland2e09382008-09-11 12:11:06 +0000260 w.reset()
Nick Coghlan48361f52008-08-11 15:45:58 +0000261 class WarnCmpAndEq(DefinesAllThree):
262 def __cmp__(self, other): pass
263 def __eq__(self, other): pass
Nick Coghland2e09382008-09-11 12:11:06 +0000264 self.assertEqual(len(w.warnings), 2)
265 self.assertWarning(None, w.warnings[0],
Nick Coghlan48361f52008-08-11 15:45:58 +0000266 "Overriding __cmp__ blocks inheritance of __hash__ in 3.x")
267 self.assertWarning(None, w,
268 "Overriding __eq__ blocks inheritance of __hash__ in 3.x")
Nick Coghland2e09382008-09-11 12:11:06 +0000269 w.reset()
Nick Coghlan48361f52008-08-11 15:45:58 +0000270 class NoWarningOnlyHash(DefinesAllThree):
271 def __hash__(self): pass
Nick Coghland2e09382008-09-11 12:11:06 +0000272 self.assertEqual(len(w.warnings), 0)
Nick Coghlan48361f52008-08-11 15:45:58 +0000273
Georg Brandl07e56812008-03-21 20:21:46 +0000274
Brett Cannone5d2cba2008-05-06 23:23:34 +0000275class TestStdlibRemovals(unittest.TestCase):
276
Brett Cannon3c759142008-05-09 05:25:37 +0000277 # test.testall not tested as it executes all unit tests as an
278 # import side-effect.
Brett Cannon4c1f8812008-05-10 02:27:04 +0000279 all_platforms = ('audiodev', 'imputil', 'mutex', 'user', 'new', 'rexec',
Brett Cannon1e8fba72008-07-18 19:30:22 +0000280 'Bastion', 'compiler', 'dircache', 'mimetools',
281 'fpformat', 'ihooks', 'mhlib', 'statvfs', 'htmllib',
282 'sgmllib', 'rfc822', 'sunaudio')
Brett Cannon54c77aa2008-05-14 21:08:41 +0000283 inclusive_platforms = {'irix' : ('pure', 'AL', 'al', 'CD', 'cd', 'cddb',
Brett Cannon044616a2008-05-15 02:33:55 +0000284 'cdplayer', 'CL', 'cl', 'DEVICE', 'GL',
Brett Cannon75ba4652008-05-15 03:23:17 +0000285 'gl', 'ERRNO', 'FILE', 'FL', 'flp', 'fl',
Brett Cannond8c41ec2008-05-15 03:41:55 +0000286 'fm', 'GET', 'GLWS', 'imgfile', 'IN',
Brett Cannoncd2de082008-05-15 03:51:21 +0000287 'IOCTL', 'jpeg', 'panel', 'panelparser',
Brett Cannon74a596c2008-05-15 04:17:35 +0000288 'readcd', 'SV', 'torgb', 'WAIT'),
Benjamin Peterson23681932008-05-12 21:42:13 +0000289 'darwin' : ('autoGIL', 'Carbon', 'OSATerminology',
Brett Cannonea785fb2008-05-14 01:09:40 +0000290 'icglue', 'Nav', 'MacOS', 'aepack',
291 'aetools', 'aetypes', 'applesingle',
292 'appletrawmain', 'appletrunner',
293 'argvemulator', 'bgenlocations',
Benjamin Peterson23681932008-05-12 21:42:13 +0000294 'EasyDialogs', 'macerrors', 'macostools',
295 'findertools', 'FrameWork', 'ic',
296 'gensuitemodule', 'icopen', 'macresource',
297 'MiniAEFrame', 'pimp', 'PixMapWrapper',
Brett Cannonea785fb2008-05-14 01:09:40 +0000298 'terminalcommand', 'videoreader',
299 '_builtinSuites', 'CodeWarrior',
300 'Explorer', 'Finder', 'Netscape',
301 'StdSuites', 'SystemEvents', 'Terminal',
302 'cfmfile', 'bundlebuilder', 'buildtools',
Benjamin Petersona6864e02008-07-14 17:42:17 +0000303 'ColorPicker', 'Audio_mac'),
Brett Cannon22248172008-05-16 00:10:24 +0000304 'sunos5' : ('sunaudiodev', 'SUNAUDIODEV'),
305 }
Brett Cannonac861b52008-05-12 03:45:59 +0000306 optional_modules = ('bsddb185', 'Canvas', 'dl', 'linuxaudiodev', 'imageop',
Brett Cannon32476fc2008-09-05 18:33:51 +0000307 'sv', 'cPickle', 'bsddb', 'dbhash')
Brett Cannone5d2cba2008-05-06 23:23:34 +0000308
Brett Cannon9ac39742008-05-09 22:51:58 +0000309 def check_removal(self, module_name, optional=False):
Brett Cannone5d2cba2008-05-06 23:23:34 +0000310 """Make sure the specified module, when imported, raises a
311 DeprecationWarning and specifies itself in the message."""
Brett Cannon672237d2008-09-09 00:49:16 +0000312 with nested(CleanImport(module_name), warnings.catch_warnings()):
Nick Coghland2e09382008-09-11 12:11:06 +0000313 # XXX: This is not quite enough for extension modules - those
314 # won't rerun their init code even with CleanImport.
315 # You can see this easily by running the whole test suite with -3
Benjamin Petersona6864e02008-07-14 17:42:17 +0000316 warnings.filterwarnings("error", ".+ removed",
317 DeprecationWarning, __name__)
318 try:
319 __import__(module_name, level=0)
320 except DeprecationWarning as exc:
321 self.assert_(module_name in exc.args[0],
322 "%s warning didn't contain module name"
323 % module_name)
324 except ImportError:
325 if not optional:
326 self.fail("Non-optional module {0} raised an "
327 "ImportError.".format(module_name))
328 else:
329 self.fail("DeprecationWarning not raised for {0}"
330 .format(module_name))
Brett Cannone5d2cba2008-05-06 23:23:34 +0000331
332 def test_platform_independent_removals(self):
333 # Make sure that the modules that are available on all platforms raise
334 # the proper DeprecationWarning.
335 for module_name in self.all_platforms:
336 self.check_removal(module_name)
337
Brett Cannon9ac39742008-05-09 22:51:58 +0000338 def test_platform_specific_removals(self):
339 # Test the removal of platform-specific modules.
340 for module_name in self.inclusive_platforms.get(sys.platform, []):
341 self.check_removal(module_name, optional=True)
342
Brett Cannon768d44f2008-05-10 02:47:54 +0000343 def test_optional_module_removals(self):
344 # Test the removal of modules that may or may not be built.
345 for module_name in self.optional_modules:
346 self.check_removal(module_name, optional=True)
347
Benjamin Peterson0893a0a2008-05-09 00:27:01 +0000348 def test_os_path_walk(self):
349 msg = "In 3.x, os.path.walk is removed in favor of os.walk."
350 def dumbo(where, names, args): pass
351 for path_mod in ("ntpath", "macpath", "os2emxpath", "posixpath"):
352 mod = __import__(path_mod)
Nick Coghland2e09382008-09-11 12:11:06 +0000353 reset_module_registry(mod)
354 with check_warnings() as w:
Benjamin Peterson1d310232008-05-27 01:42:29 +0000355 mod.walk("crashers", dumbo, None)
Nick Coghland2e09382008-09-11 12:11:06 +0000356 self.assertEquals(str(w.message), msg)
Benjamin Peterson0893a0a2008-05-09 00:27:01 +0000357
Benjamin Peterson3aa84a72008-05-26 19:41:53 +0000358 def test_commands_members(self):
359 import commands
Nick Coghland2e09382008-09-11 12:11:06 +0000360 # commands module tests may have already triggered this warning
361 reset_module_registry(commands)
Benjamin Peterson3aa84a72008-05-26 19:41:53 +0000362 members = {"mk2arg" : 2, "mkarg" : 1, "getstatus" : 1}
363 for name, arg_count in members.items():
Brett Cannon672237d2008-09-09 00:49:16 +0000364 with warnings.catch_warnings():
Benjamin Peterson3aa84a72008-05-26 19:41:53 +0000365 warnings.filterwarnings("error")
366 func = getattr(commands, name)
367 self.assertRaises(DeprecationWarning, func, *([None]*arg_count))
368
Benjamin Peterson541f7da2008-08-18 02:12:23 +0000369 def test_reduce_move(self):
370 from operator import add
Nick Coghland2e09382008-09-11 12:11:06 +0000371 # reduce tests may have already triggered this warning
372 reset_module_registry(unittest)
Brett Cannon672237d2008-09-09 00:49:16 +0000373 with warnings.catch_warnings():
Benjamin Peterson541f7da2008-08-18 02:12:23 +0000374 warnings.filterwarnings("error", "reduce")
375 self.assertRaises(DeprecationWarning, reduce, add, range(10))
376
Brett Cannonabb34fe2008-05-29 05:08:50 +0000377 def test_mutablestring_removal(self):
378 # UserString.MutableString has been removed in 3.0.
379 import UserString
Nick Coghland2e09382008-09-11 12:11:06 +0000380 # UserString tests may have already triggered this warning
381 reset_module_registry(UserString)
Brett Cannon672237d2008-09-09 00:49:16 +0000382 with warnings.catch_warnings():
Brett Cannonabb34fe2008-05-29 05:08:50 +0000383 warnings.filterwarnings("error", ".*MutableString",
384 DeprecationWarning)
385 self.assertRaises(DeprecationWarning, UserString.MutableString)
386
Brett Cannone5d2cba2008-05-06 23:23:34 +0000387
Steven Bethardae42f332008-03-18 17:26:10 +0000388def test_main():
Nick Coghland2e09382008-09-11 12:11:06 +0000389 with check_warnings():
Benjamin Peterson1d310232008-05-27 01:42:29 +0000390 warnings.simplefilter("always")
391 run_unittest(TestPy3KWarnings,
392 TestStdlibRemovals)
Steven Bethardae42f332008-03-18 17:26:10 +0000393
394if __name__ == '__main__':
395 test_main()