blob: c35e4b48f206ad64621c195eb9685b08c49ad68a [file] [log] [blame]
Steven Bethardae42f332008-03-18 17:26:10 +00001import unittest
Brett Cannon977eb022008-03-19 17:37:43 +00002import sys
Benjamin Petersonbec087f2009-03-26 21:10:30 +00003from test.test_support import check_warnings, CleanImport, run_unittest
Steven Bethardae42f332008-03-18 17:26:10 +00004import warnings
5
Benjamin Petersona6864e02008-07-14 17:42:17 +00006from contextlib import nested
7
Brett Cannon977eb022008-03-19 17:37:43 +00008if not sys.py3kwarning:
Benjamin Petersonbec087f2009-03-26 21:10:30 +00009 raise unittest.SkipTest('%s must be run with the -3 flag' % __name__)
Brett Cannon977eb022008-03-19 17:37:43 +000010
Nick Coghland2e09382008-09-11 12:11:06 +000011def reset_module_registry(module):
12 try:
13 registry = module.__warningregistry__
14 except AttributeError:
15 pass
16 else:
17 registry.clear()
Steven Bethardae42f332008-03-18 17:26:10 +000018
19class TestPy3KWarnings(unittest.TestCase):
20
Nick Coghlan48361f52008-08-11 15:45:58 +000021 def assertWarning(self, _, warning, expected_message):
Nick Coghland2e09382008-09-11 12:11:06 +000022 self.assertEqual(str(warning.message), expected_message)
Nick Coghlan48361f52008-08-11 15:45:58 +000023
Benjamin Peterson1bf47652009-07-02 17:06:17 +000024 def assertNoWarning(self, _, recorder):
25 self.assertEqual(len(recorder.warnings), 0)
26
Benjamin Peterson2fe3ef82008-06-08 02:05:33 +000027 def test_backquote(self):
28 expected = 'backquote not supported in 3.x; use repr()'
Nick Coghland2e09382008-09-11 12:11:06 +000029 with check_warnings() as w:
Benjamin Peterson2fe3ef82008-06-08 02:05:33 +000030 exec "`2`" in {}
31 self.assertWarning(None, w, expected)
32
Benjamin Peterson99a50232009-11-19 22:54:57 +000033 def test_paren_arg_names(self):
34 expected = 'parenthesized argument names are invalid in 3.x'
35 def check(s):
36 exec s in {}
37 self.assertWarning(None, w, expected)
38 with check_warnings() as w:
39 check("def f((x)): pass")
40 check("def f((((x))), (y)): pass")
41 check("def f((x), (((y))), m=32): pass")
42 # Something like def f((a, (b))): pass will raise the tuple
43 # unpacking warning.
44
Benjamin Peterson399b1fe2008-10-25 02:53:28 +000045 def test_forbidden_names(self):
Benjamin Petersond5efd202008-06-08 22:52:37 +000046 # So we don't screw up our globals
47 def safe_exec(expr):
48 def f(**kwargs): pass
49 exec expr in {'f' : f}
50
Benjamin Peterson399b1fe2008-10-25 02:53:28 +000051 tests = [("True", "assignment to True or False is forbidden in 3.x"),
52 ("False", "assignment to True or False is forbidden in 3.x"),
53 ("nonlocal", "nonlocal is a keyword in 3.x")]
Nick Coghland2e09382008-09-11 12:11:06 +000054 with check_warnings() as w:
Benjamin Peterson399b1fe2008-10-25 02:53:28 +000055 for keyword, expected in tests:
56 safe_exec("{0} = False".format(keyword))
57 self.assertWarning(None, w, expected)
58 w.reset()
59 try:
60 safe_exec("obj.{0} = True".format(keyword))
61 except NameError:
62 pass
63 self.assertWarning(None, w, expected)
64 w.reset()
65 safe_exec("def {0}(): pass".format(keyword))
66 self.assertWarning(None, w, expected)
67 w.reset()
68 safe_exec("class {0}: pass".format(keyword))
69 self.assertWarning(None, w, expected)
70 w.reset()
71 safe_exec("def f({0}=43): pass".format(keyword))
72 self.assertWarning(None, w, expected)
73 w.reset()
Benjamin Petersond5efd202008-06-08 22:52:37 +000074
75
Steven Bethardae42f332008-03-18 17:26:10 +000076 def test_type_inequality_comparisons(self):
Georg Brandld5b635f2008-03-25 08:29:14 +000077 expected = 'type 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(int < str, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +000080 w.reset()
Steven Bethardae42f332008-03-18 17:26:10 +000081 self.assertWarning(type < object, w, expected)
82
83 def test_object_inequality_comparisons(self):
Georg Brandld5b635f2008-03-25 08:29:14 +000084 expected = 'comparing unequal types not supported in 3.x'
Nick Coghland2e09382008-09-11 12:11:06 +000085 with check_warnings() as w:
Steven Bethardae42f332008-03-18 17:26:10 +000086 self.assertWarning(str < [], w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +000087 w.reset()
Steven Bethardae42f332008-03-18 17:26:10 +000088 self.assertWarning(object() < (1, 2), w, expected)
89
90 def test_dict_inequality_comparisons(self):
Georg Brandld5b635f2008-03-25 08:29:14 +000091 expected = 'dict inequality comparisons not supported in 3.x'
Nick Coghland2e09382008-09-11 12:11:06 +000092 with check_warnings() as w:
Steven Bethardae42f332008-03-18 17:26:10 +000093 self.assertWarning({} < {2:3}, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +000094 w.reset()
Steven Bethardae42f332008-03-18 17:26:10 +000095 self.assertWarning({} <= {}, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +000096 w.reset()
Steven Bethardae42f332008-03-18 17:26:10 +000097 self.assertWarning({} > {2:3}, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +000098 w.reset()
Steven Bethardae42f332008-03-18 17:26:10 +000099 self.assertWarning({2:3} >= {}, w, expected)
100
101 def test_cell_inequality_comparisons(self):
Georg Brandld5b635f2008-03-25 08:29:14 +0000102 expected = 'cell comparisons not supported in 3.x'
Steven Bethardae42f332008-03-18 17:26:10 +0000103 def f(x):
104 def g():
105 return x
106 return g
107 cell0, = f(0).func_closure
108 cell1, = f(1).func_closure
Nick Coghland2e09382008-09-11 12:11:06 +0000109 with check_warnings() as w:
Steven Bethardae42f332008-03-18 17:26:10 +0000110 self.assertWarning(cell0 == cell1, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000111 w.reset()
Steven Bethardae42f332008-03-18 17:26:10 +0000112 self.assertWarning(cell0 < cell1, w, expected)
113
Steven Bethard6a644f92008-03-18 22:08:20 +0000114 def test_code_inequality_comparisons(self):
Georg Brandld5b635f2008-03-25 08:29:14 +0000115 expected = 'code inequality comparisons not supported in 3.x'
Steven Bethard6a644f92008-03-18 22:08:20 +0000116 def f(x):
117 pass
118 def g(x):
119 pass
Nick Coghland2e09382008-09-11 12:11:06 +0000120 with check_warnings() as w:
Steven Bethard6a644f92008-03-18 22:08:20 +0000121 self.assertWarning(f.func_code < g.func_code, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000122 w.reset()
Steven Bethard6a644f92008-03-18 22:08:20 +0000123 self.assertWarning(f.func_code <= g.func_code, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000124 w.reset()
Steven Bethard6a644f92008-03-18 22:08:20 +0000125 self.assertWarning(f.func_code >= g.func_code, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000126 w.reset()
Steven Bethard6a644f92008-03-18 22:08:20 +0000127 self.assertWarning(f.func_code > g.func_code, w, expected)
128
129 def test_builtin_function_or_method_comparisons(self):
130 expected = ('builtin_function_or_method '
Benjamin Peterson1bf47652009-07-02 17:06:17 +0000131 'order comparisons not supported in 3.x')
Steven Bethard6a644f92008-03-18 22:08:20 +0000132 func = eval
133 meth = {}.get
Nick Coghland2e09382008-09-11 12:11:06 +0000134 with check_warnings() as w:
Steven Bethard6a644f92008-03-18 22:08:20 +0000135 self.assertWarning(func < meth, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000136 w.reset()
Steven Bethard6a644f92008-03-18 22:08:20 +0000137 self.assertWarning(func > meth, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000138 w.reset()
Steven Bethard6a644f92008-03-18 22:08:20 +0000139 self.assertWarning(meth <= func, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000140 w.reset()
Steven Bethard6a644f92008-03-18 22:08:20 +0000141 self.assertWarning(meth >= func, w, expected)
Benjamin Peterson1bf47652009-07-02 17:06:17 +0000142 w.reset()
143 self.assertNoWarning(meth == func, w)
144 self.assertNoWarning(meth != func, w)
145 lam = lambda x: x
146 self.assertNoWarning(lam == func, w)
147 self.assertNoWarning(lam != func, w)
Steven Bethard6a644f92008-03-18 22:08:20 +0000148
Benjamin Petersonf09925d2008-12-22 20:16:25 +0000149 def test_frame_attributes(self):
150 template = "%s has been removed in 3.x"
151 f = sys._getframe(0)
152 for attr in ("f_exc_traceback", "f_exc_value", "f_exc_type"):
153 expected = template % attr
154 with check_warnings() as w:
155 self.assertWarning(getattr(f, attr), w, expected)
156 w.reset()
157 self.assertWarning(setattr(f, attr, None), w, expected)
158
Raymond Hettinger05387862008-03-19 17:45:19 +0000159 def test_sort_cmp_arg(self):
Georg Brandld5b635f2008-03-25 08:29:14 +0000160 expected = "the cmp argument is not supported in 3.x"
Raymond Hettinger05387862008-03-19 17:45:19 +0000161 lst = range(5)
162 cmp = lambda x,y: -1
163
Nick Coghland2e09382008-09-11 12:11:06 +0000164 with check_warnings() as w:
Raymond Hettinger05387862008-03-19 17:45:19 +0000165 self.assertWarning(lst.sort(cmp=cmp), w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000166 w.reset()
Raymond Hettinger05387862008-03-19 17:45:19 +0000167 self.assertWarning(sorted(lst, cmp=cmp), w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000168 w.reset()
Raymond Hettinger05387862008-03-19 17:45:19 +0000169 self.assertWarning(lst.sort(cmp), w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000170 w.reset()
Raymond Hettinger05387862008-03-19 17:45:19 +0000171 self.assertWarning(sorted(lst, cmp), w, expected)
172
Georg Brandl5a444242008-03-21 20:11:46 +0000173 def test_sys_exc_clear(self):
Georg Brandld5b635f2008-03-25 08:29:14 +0000174 expected = 'sys.exc_clear() not supported in 3.x; use except clauses'
Nick Coghland2e09382008-09-11 12:11:06 +0000175 with check_warnings() as w:
Georg Brandl5a444242008-03-21 20:11:46 +0000176 self.assertWarning(sys.exc_clear(), w, expected)
177
Georg Brandl07e56812008-03-21 20:21:46 +0000178 def test_methods_members(self):
179 expected = '__members__ and __methods__ not supported in 3.x'
180 class C:
181 __methods__ = ['a']
182 __members__ = ['b']
183 c = C()
Nick Coghland2e09382008-09-11 12:11:06 +0000184 with check_warnings() as w:
Georg Brandl07e56812008-03-21 20:21:46 +0000185 self.assertWarning(dir(c), w, expected)
186
Georg Brandl65bb42d2008-03-21 20:38:24 +0000187 def test_softspace(self):
188 expected = 'file.softspace not supported in 3.x'
189 with file(__file__) as f:
Nick Coghland2e09382008-09-11 12:11:06 +0000190 with check_warnings() as w:
Georg Brandl65bb42d2008-03-21 20:38:24 +0000191 self.assertWarning(f.softspace, w, expected)
192 def set():
193 f.softspace = 0
Nick Coghland2e09382008-09-11 12:11:06 +0000194 with check_warnings() as w:
Georg Brandl65bb42d2008-03-21 20:38:24 +0000195 self.assertWarning(set(), w, expected)
196
Benjamin Peterson712ee922008-08-24 18:10:20 +0000197 def test_slice_methods(self):
198 class Spam(object):
199 def __getslice__(self, i, j): pass
200 def __setslice__(self, i, j, what): pass
201 def __delslice__(self, i, j): pass
202 class Egg:
203 def __getslice__(self, i, h): pass
204 def __setslice__(self, i, j, what): pass
205 def __delslice__(self, i, j): pass
206
207 expected = "in 3.x, __{0}slice__ has been removed; use __{0}item__"
208
209 for obj in (Spam(), Egg()):
Nick Coghland2e09382008-09-11 12:11:06 +0000210 with check_warnings() as w:
Benjamin Peterson712ee922008-08-24 18:10:20 +0000211 self.assertWarning(obj[1:2], w, expected.format('get'))
Nick Coghland2e09382008-09-11 12:11:06 +0000212 w.reset()
Benjamin Peterson712ee922008-08-24 18:10:20 +0000213 del obj[3:4]
214 self.assertWarning(None, w, expected.format('del'))
Nick Coghland2e09382008-09-11 12:11:06 +0000215 w.reset()
Benjamin Peterson712ee922008-08-24 18:10:20 +0000216 obj[4:5] = "eggs"
217 self.assertWarning(None, w, expected.format('set'))
218
Benjamin Petersonf4fcdb62008-06-08 23:00:00 +0000219 def test_tuple_parameter_unpacking(self):
220 expected = "tuple parameter unpacking has been removed in 3.x"
Nick Coghland2e09382008-09-11 12:11:06 +0000221 with check_warnings() as w:
Benjamin Petersonf4fcdb62008-06-08 23:00:00 +0000222 exec "def f((a, b)): pass"
223 self.assertWarning(None, w, expected)
224
Georg Brandl80055f62008-03-25 07:56:27 +0000225 def test_buffer(self):
Nick Coghlan48361f52008-08-11 15:45:58 +0000226 expected = 'buffer() not supported in 3.x'
Nick Coghland2e09382008-09-11 12:11:06 +0000227 with check_warnings() as w:
Georg Brandl80055f62008-03-25 07:56:27 +0000228 self.assertWarning(buffer('a'), w, expected)
229
Georg Brandla9916b52008-05-17 22:11:54 +0000230 def test_file_xreadlines(self):
231 expected = ("f.xreadlines() not supported in 3.x, "
232 "try 'for line in f' instead")
233 with file(__file__) as f:
Nick Coghland2e09382008-09-11 12:11:06 +0000234 with check_warnings() as w:
Georg Brandla9916b52008-05-17 22:11:54 +0000235 self.assertWarning(f.xreadlines(), w, expected)
236
Nick Coghlan48361f52008-08-11 15:45:58 +0000237 def test_hash_inheritance(self):
Nick Coghland2e09382008-09-11 12:11:06 +0000238 with check_warnings() as w:
Nick Coghlan48361f52008-08-11 15:45:58 +0000239 # With object as the base class
240 class WarnOnlyCmp(object):
241 def __cmp__(self, other): pass
Nick Coghland2e09382008-09-11 12:11:06 +0000242 self.assertEqual(len(w.warnings), 1)
Nick Coghlan48361f52008-08-11 15:45:58 +0000243 self.assertWarning(None, w,
244 "Overriding __cmp__ blocks inheritance of __hash__ in 3.x")
Nick Coghland2e09382008-09-11 12:11:06 +0000245 w.reset()
Nick Coghlan48361f52008-08-11 15:45:58 +0000246 class WarnOnlyEq(object):
247 def __eq__(self, other): pass
Nick Coghland2e09382008-09-11 12:11:06 +0000248 self.assertEqual(len(w.warnings), 1)
Nick Coghlan48361f52008-08-11 15:45:58 +0000249 self.assertWarning(None, w,
250 "Overriding __eq__ blocks inheritance of __hash__ in 3.x")
Nick Coghland2e09382008-09-11 12:11:06 +0000251 w.reset()
Nick Coghlan48361f52008-08-11 15:45:58 +0000252 class WarnCmpAndEq(object):
253 def __cmp__(self, other): pass
254 def __eq__(self, other): pass
Nick Coghland2e09382008-09-11 12:11:06 +0000255 self.assertEqual(len(w.warnings), 2)
256 self.assertWarning(None, w.warnings[0],
Nick Coghlan48361f52008-08-11 15:45:58 +0000257 "Overriding __cmp__ blocks inheritance of __hash__ in 3.x")
258 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 NoWarningOnlyHash(object):
262 def __hash__(self): pass
Nick Coghland2e09382008-09-11 12:11:06 +0000263 self.assertEqual(len(w.warnings), 0)
Nick Coghlan48361f52008-08-11 15:45:58 +0000264 # With an intermediate class in the heirarchy
265 class DefinesAllThree(object):
266 def __cmp__(self, other): pass
267 def __eq__(self, other): pass
268 def __hash__(self): pass
269 class WarnOnlyCmp(DefinesAllThree):
270 def __cmp__(self, other): pass
Nick Coghland2e09382008-09-11 12:11:06 +0000271 self.assertEqual(len(w.warnings), 1)
Nick Coghlan48361f52008-08-11 15:45:58 +0000272 self.assertWarning(None, w,
273 "Overriding __cmp__ blocks inheritance of __hash__ in 3.x")
Nick Coghland2e09382008-09-11 12:11:06 +0000274 w.reset()
Nick Coghlan48361f52008-08-11 15:45:58 +0000275 class WarnOnlyEq(DefinesAllThree):
276 def __eq__(self, other): pass
Nick Coghland2e09382008-09-11 12:11:06 +0000277 self.assertEqual(len(w.warnings), 1)
Nick Coghlan48361f52008-08-11 15:45:58 +0000278 self.assertWarning(None, w,
279 "Overriding __eq__ blocks inheritance of __hash__ in 3.x")
Nick Coghland2e09382008-09-11 12:11:06 +0000280 w.reset()
Nick Coghlan48361f52008-08-11 15:45:58 +0000281 class WarnCmpAndEq(DefinesAllThree):
282 def __cmp__(self, other): pass
283 def __eq__(self, other): pass
Nick Coghland2e09382008-09-11 12:11:06 +0000284 self.assertEqual(len(w.warnings), 2)
285 self.assertWarning(None, w.warnings[0],
Nick Coghlan48361f52008-08-11 15:45:58 +0000286 "Overriding __cmp__ blocks inheritance of __hash__ in 3.x")
287 self.assertWarning(None, w,
288 "Overriding __eq__ blocks inheritance of __hash__ in 3.x")
Nick Coghland2e09382008-09-11 12:11:06 +0000289 w.reset()
Nick Coghlan48361f52008-08-11 15:45:58 +0000290 class NoWarningOnlyHash(DefinesAllThree):
291 def __hash__(self): pass
Nick Coghland2e09382008-09-11 12:11:06 +0000292 self.assertEqual(len(w.warnings), 0)
Nick Coghlan48361f52008-08-11 15:45:58 +0000293
Alexandre Vassalotti0fe79912009-07-05 04:22:40 +0000294 def test_operator(self):
295 from operator import isCallable, sequenceIncludes
296
Alexandre Vassalotti16a02472009-07-05 04:25:46 +0000297 callable_warn = ("operator.isCallable() is not supported in 3.x. "
298 "Use hasattr(obj, '__call__').")
Alexandre Vassalotti0fe79912009-07-05 04:22:40 +0000299 seq_warn = ("operator.sequenceIncludes() is not supported "
300 "in 3.x. Use operator.contains().")
301 with check_warnings() as w:
302 self.assertWarning(isCallable(self), w, callable_warn)
303 w.reset()
304 self.assertWarning(sequenceIncludes(range(3), 2), w, seq_warn)
305
Georg Brandl07e56812008-03-21 20:21:46 +0000306
Brett Cannone5d2cba2008-05-06 23:23:34 +0000307class TestStdlibRemovals(unittest.TestCase):
308
Brett Cannon3c759142008-05-09 05:25:37 +0000309 # test.testall not tested as it executes all unit tests as an
310 # import side-effect.
Brett Cannon4c1f8812008-05-10 02:27:04 +0000311 all_platforms = ('audiodev', 'imputil', 'mutex', 'user', 'new', 'rexec',
Brett Cannon1e8fba72008-07-18 19:30:22 +0000312 'Bastion', 'compiler', 'dircache', 'mimetools',
313 'fpformat', 'ihooks', 'mhlib', 'statvfs', 'htmllib',
314 'sgmllib', 'rfc822', 'sunaudio')
Brett Cannon54c77aa2008-05-14 21:08:41 +0000315 inclusive_platforms = {'irix' : ('pure', 'AL', 'al', 'CD', 'cd', 'cddb',
Brett Cannon044616a2008-05-15 02:33:55 +0000316 'cdplayer', 'CL', 'cl', 'DEVICE', 'GL',
Brett Cannon75ba4652008-05-15 03:23:17 +0000317 'gl', 'ERRNO', 'FILE', 'FL', 'flp', 'fl',
Brett Cannond8c41ec2008-05-15 03:41:55 +0000318 'fm', 'GET', 'GLWS', 'imgfile', 'IN',
Brett Cannoncd2de082008-05-15 03:51:21 +0000319 'IOCTL', 'jpeg', 'panel', 'panelparser',
Brett Cannon74a596c2008-05-15 04:17:35 +0000320 'readcd', 'SV', 'torgb', 'WAIT'),
Benjamin Peterson23681932008-05-12 21:42:13 +0000321 'darwin' : ('autoGIL', 'Carbon', 'OSATerminology',
Brett Cannonea785fb2008-05-14 01:09:40 +0000322 'icglue', 'Nav', 'MacOS', 'aepack',
323 'aetools', 'aetypes', 'applesingle',
324 'appletrawmain', 'appletrunner',
325 'argvemulator', 'bgenlocations',
Benjamin Peterson23681932008-05-12 21:42:13 +0000326 'EasyDialogs', 'macerrors', 'macostools',
327 'findertools', 'FrameWork', 'ic',
328 'gensuitemodule', 'icopen', 'macresource',
329 'MiniAEFrame', 'pimp', 'PixMapWrapper',
Brett Cannonea785fb2008-05-14 01:09:40 +0000330 'terminalcommand', 'videoreader',
331 '_builtinSuites', 'CodeWarrior',
332 'Explorer', 'Finder', 'Netscape',
333 'StdSuites', 'SystemEvents', 'Terminal',
334 'cfmfile', 'bundlebuilder', 'buildtools',
Benjamin Petersona6864e02008-07-14 17:42:17 +0000335 'ColorPicker', 'Audio_mac'),
Brett Cannon22248172008-05-16 00:10:24 +0000336 'sunos5' : ('sunaudiodev', 'SUNAUDIODEV'),
337 }
Brett Cannonac861b52008-05-12 03:45:59 +0000338 optional_modules = ('bsddb185', 'Canvas', 'dl', 'linuxaudiodev', 'imageop',
Antoine Pitrou80157252010-01-08 19:20:17 +0000339 'sv', 'bsddb', 'dbhash')
Brett Cannone5d2cba2008-05-06 23:23:34 +0000340
Brett Cannon9ac39742008-05-09 22:51:58 +0000341 def check_removal(self, module_name, optional=False):
Brett Cannone5d2cba2008-05-06 23:23:34 +0000342 """Make sure the specified module, when imported, raises a
343 DeprecationWarning and specifies itself in the message."""
Brett Cannon672237d2008-09-09 00:49:16 +0000344 with nested(CleanImport(module_name), warnings.catch_warnings()):
Nick Coghland2e09382008-09-11 12:11:06 +0000345 # XXX: This is not quite enough for extension modules - those
346 # won't rerun their init code even with CleanImport.
347 # You can see this easily by running the whole test suite with -3
Benjamin Petersona6864e02008-07-14 17:42:17 +0000348 warnings.filterwarnings("error", ".+ removed",
349 DeprecationWarning, __name__)
350 try:
351 __import__(module_name, level=0)
352 except DeprecationWarning as exc:
Ezio Melottiaa980582010-01-23 23:04:36 +0000353 self.assertIn(module_name, exc.args[0],
354 "%s warning didn't contain module name"
355 % module_name)
Benjamin Petersona6864e02008-07-14 17:42:17 +0000356 except ImportError:
357 if not optional:
358 self.fail("Non-optional module {0} raised an "
359 "ImportError.".format(module_name))
360 else:
361 self.fail("DeprecationWarning not raised for {0}"
362 .format(module_name))
Brett Cannone5d2cba2008-05-06 23:23:34 +0000363
364 def test_platform_independent_removals(self):
365 # Make sure that the modules that are available on all platforms raise
366 # the proper DeprecationWarning.
367 for module_name in self.all_platforms:
368 self.check_removal(module_name)
369
Brett Cannon9ac39742008-05-09 22:51:58 +0000370 def test_platform_specific_removals(self):
371 # Test the removal of platform-specific modules.
372 for module_name in self.inclusive_platforms.get(sys.platform, []):
373 self.check_removal(module_name, optional=True)
374
Brett Cannon768d44f2008-05-10 02:47:54 +0000375 def test_optional_module_removals(self):
376 # Test the removal of modules that may or may not be built.
377 for module_name in self.optional_modules:
378 self.check_removal(module_name, optional=True)
379
Benjamin Peterson0893a0a2008-05-09 00:27:01 +0000380 def test_os_path_walk(self):
381 msg = "In 3.x, os.path.walk is removed in favor of os.walk."
382 def dumbo(where, names, args): pass
383 for path_mod in ("ntpath", "macpath", "os2emxpath", "posixpath"):
384 mod = __import__(path_mod)
Nick Coghland2e09382008-09-11 12:11:06 +0000385 reset_module_registry(mod)
386 with check_warnings() as w:
Benjamin Peterson1d310232008-05-27 01:42:29 +0000387 mod.walk("crashers", dumbo, None)
Nick Coghland2e09382008-09-11 12:11:06 +0000388 self.assertEquals(str(w.message), msg)
Benjamin Peterson0893a0a2008-05-09 00:27:01 +0000389
Benjamin Peterson541f7da2008-08-18 02:12:23 +0000390 def test_reduce_move(self):
391 from operator import add
Nick Coghland2e09382008-09-11 12:11:06 +0000392 # reduce tests may have already triggered this warning
393 reset_module_registry(unittest)
Brett Cannon672237d2008-09-09 00:49:16 +0000394 with warnings.catch_warnings():
Benjamin Peterson541f7da2008-08-18 02:12:23 +0000395 warnings.filterwarnings("error", "reduce")
396 self.assertRaises(DeprecationWarning, reduce, add, range(10))
397
Brett Cannonabb34fe2008-05-29 05:08:50 +0000398 def test_mutablestring_removal(self):
399 # UserString.MutableString has been removed in 3.0.
400 import UserString
Nick Coghland2e09382008-09-11 12:11:06 +0000401 # UserString tests may have already triggered this warning
402 reset_module_registry(UserString)
Brett Cannon672237d2008-09-09 00:49:16 +0000403 with warnings.catch_warnings():
Brett Cannonabb34fe2008-05-29 05:08:50 +0000404 warnings.filterwarnings("error", ".*MutableString",
405 DeprecationWarning)
406 self.assertRaises(DeprecationWarning, UserString.MutableString)
407
Brett Cannone5d2cba2008-05-06 23:23:34 +0000408
Steven Bethardae42f332008-03-18 17:26:10 +0000409def test_main():
Nick Coghland2e09382008-09-11 12:11:06 +0000410 with check_warnings():
Benjamin Peterson1d310232008-05-27 01:42:29 +0000411 warnings.simplefilter("always")
412 run_unittest(TestPy3KWarnings,
413 TestStdlibRemovals)
Steven Bethardae42f332008-03-18 17:26:10 +0000414
415if __name__ == '__main__':
416 test_main()