blob: 3a12251c601f4d728aaac67643d89570d623060c [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
Ezio Melottif9c2f4f2010-08-02 21:48:39 +000012try:
13 from test.test_support import __warningregistry__ as _registry
14except ImportError:
15 def check_deprecated_module(module_name):
16 return False
17else:
18 past_warnings = _registry.keys()
19 del _registry
20 def check_deprecated_module(module_name):
21 """Lookup the past warnings for module already loaded using
22 test_support.import_module(..., deprecated=True)
23 """
24 return any(module_name in msg and ' removed' in msg
25 and issubclass(cls, DeprecationWarning)
26 and (' module' in msg or ' package' in msg)
27 for (msg, cls, line) in past_warnings)
28
Nick Coghland2e09382008-09-11 12:11:06 +000029def reset_module_registry(module):
30 try:
31 registry = module.__warningregistry__
32 except AttributeError:
33 pass
34 else:
35 registry.clear()
Steven Bethardae42f332008-03-18 17:26:10 +000036
37class TestPy3KWarnings(unittest.TestCase):
38
Nick Coghlan48361f52008-08-11 15:45:58 +000039 def assertWarning(self, _, warning, expected_message):
Nick Coghland2e09382008-09-11 12:11:06 +000040 self.assertEqual(str(warning.message), expected_message)
Nick Coghlan48361f52008-08-11 15:45:58 +000041
Benjamin Petersonf9214692009-07-02 17:19:22 +000042 def assertNoWarning(self, _, recorder):
43 self.assertEqual(len(recorder.warnings), 0)
44
Benjamin Peterson2fe3ef82008-06-08 02:05:33 +000045 def test_backquote(self):
46 expected = 'backquote not supported in 3.x; use repr()'
Nick Coghland2e09382008-09-11 12:11:06 +000047 with check_warnings() as w:
Benjamin Peterson2fe3ef82008-06-08 02:05:33 +000048 exec "`2`" in {}
49 self.assertWarning(None, w, expected)
50
Benjamin Peterson440847c2009-11-19 23:01:36 +000051 def test_paren_arg_names(self):
52 expected = 'parenthesized argument names are invalid in 3.x'
53 def check(s):
54 exec s in {}
55 self.assertWarning(None, w, expected)
56 with check_warnings() as w:
57 check("def f((x)): pass")
58 check("def f((((x))), (y)): pass")
59 check("def f((x), (((y))), m=32): pass")
60 # Something like def f((a, (b))): pass will raise the tuple
61 # unpacking warning.
62
Benjamin Petersond5efd202008-06-08 22:52:37 +000063 def test_bool_assign(self):
64 # So we don't screw up our globals
65 def safe_exec(expr):
66 def f(**kwargs): pass
67 exec expr in {'f' : f}
68
69 expected = "assignment to True or False is forbidden in 3.x"
Nick Coghland2e09382008-09-11 12:11:06 +000070 with check_warnings() as w:
Benjamin Petersond5efd202008-06-08 22:52:37 +000071 safe_exec("True = False")
72 self.assertWarning(None, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +000073 w.reset()
Benjamin Petersond5efd202008-06-08 22:52:37 +000074 safe_exec("False = True")
75 self.assertWarning(None, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +000076 w.reset()
Benjamin Petersond5efd202008-06-08 22:52:37 +000077 try:
78 safe_exec("obj.False = True")
79 except NameError: pass
80 self.assertWarning(None, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +000081 w.reset()
Benjamin Petersond5efd202008-06-08 22:52:37 +000082 try:
83 safe_exec("obj.True = False")
84 except NameError: pass
85 self.assertWarning(None, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +000086 w.reset()
Benjamin Petersond5efd202008-06-08 22:52:37 +000087 safe_exec("def False(): pass")
88 self.assertWarning(None, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +000089 w.reset()
Benjamin Petersond5efd202008-06-08 22:52:37 +000090 safe_exec("def True(): pass")
91 self.assertWarning(None, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +000092 w.reset()
Benjamin Petersond5efd202008-06-08 22:52:37 +000093 safe_exec("class False: pass")
94 self.assertWarning(None, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +000095 w.reset()
Benjamin Petersond5efd202008-06-08 22:52:37 +000096 safe_exec("class True: pass")
97 self.assertWarning(None, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +000098 w.reset()
Benjamin Petersond5efd202008-06-08 22:52:37 +000099 safe_exec("def f(True=43): pass")
100 self.assertWarning(None, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000101 w.reset()
Benjamin Petersond5efd202008-06-08 22:52:37 +0000102 safe_exec("def f(False=None): pass")
103 self.assertWarning(None, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000104 w.reset()
Benjamin Petersond5efd202008-06-08 22:52:37 +0000105 safe_exec("f(False=True)")
106 self.assertWarning(None, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000107 w.reset()
Benjamin Petersond5efd202008-06-08 22:52:37 +0000108 safe_exec("f(True=1)")
109 self.assertWarning(None, w, expected)
110
111
Steven Bethardae42f332008-03-18 17:26:10 +0000112 def test_type_inequality_comparisons(self):
Georg Brandld5b635f2008-03-25 08:29:14 +0000113 expected = 'type inequality comparisons not supported in 3.x'
Nick Coghland2e09382008-09-11 12:11:06 +0000114 with check_warnings() as w:
Steven Bethardae42f332008-03-18 17:26:10 +0000115 self.assertWarning(int < str, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000116 w.reset()
Steven Bethardae42f332008-03-18 17:26:10 +0000117 self.assertWarning(type < object, w, expected)
118
119 def test_object_inequality_comparisons(self):
Georg Brandld5b635f2008-03-25 08:29:14 +0000120 expected = 'comparing unequal types not supported in 3.x'
Nick Coghland2e09382008-09-11 12:11:06 +0000121 with check_warnings() as w:
Steven Bethardae42f332008-03-18 17:26:10 +0000122 self.assertWarning(str < [], w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000123 w.reset()
Steven Bethardae42f332008-03-18 17:26:10 +0000124 self.assertWarning(object() < (1, 2), w, expected)
125
126 def test_dict_inequality_comparisons(self):
Georg Brandld5b635f2008-03-25 08:29:14 +0000127 expected = 'dict inequality comparisons not supported in 3.x'
Nick Coghland2e09382008-09-11 12:11:06 +0000128 with check_warnings() as w:
Steven Bethardae42f332008-03-18 17:26:10 +0000129 self.assertWarning({} < {2:3}, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000130 w.reset()
Steven Bethardae42f332008-03-18 17:26:10 +0000131 self.assertWarning({} <= {}, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000132 w.reset()
Steven Bethardae42f332008-03-18 17:26:10 +0000133 self.assertWarning({} > {2:3}, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000134 w.reset()
Steven Bethardae42f332008-03-18 17:26:10 +0000135 self.assertWarning({2:3} >= {}, w, expected)
136
137 def test_cell_inequality_comparisons(self):
Georg Brandld5b635f2008-03-25 08:29:14 +0000138 expected = 'cell comparisons not supported in 3.x'
Steven Bethardae42f332008-03-18 17:26:10 +0000139 def f(x):
140 def g():
141 return x
142 return g
143 cell0, = f(0).func_closure
144 cell1, = f(1).func_closure
Nick Coghland2e09382008-09-11 12:11:06 +0000145 with check_warnings() as w:
Steven Bethardae42f332008-03-18 17:26:10 +0000146 self.assertWarning(cell0 == cell1, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000147 w.reset()
Steven Bethardae42f332008-03-18 17:26:10 +0000148 self.assertWarning(cell0 < cell1, w, expected)
149
Steven Bethard6a644f92008-03-18 22:08:20 +0000150 def test_code_inequality_comparisons(self):
Georg Brandld5b635f2008-03-25 08:29:14 +0000151 expected = 'code inequality comparisons not supported in 3.x'
Steven Bethard6a644f92008-03-18 22:08:20 +0000152 def f(x):
153 pass
154 def g(x):
155 pass
Nick Coghland2e09382008-09-11 12:11:06 +0000156 with check_warnings() as w:
Steven Bethard6a644f92008-03-18 22:08:20 +0000157 self.assertWarning(f.func_code < g.func_code, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000158 w.reset()
Steven Bethard6a644f92008-03-18 22:08:20 +0000159 self.assertWarning(f.func_code <= g.func_code, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000160 w.reset()
Steven Bethard6a644f92008-03-18 22:08:20 +0000161 self.assertWarning(f.func_code >= g.func_code, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000162 w.reset()
Steven Bethard6a644f92008-03-18 22:08:20 +0000163 self.assertWarning(f.func_code > g.func_code, w, expected)
164
165 def test_builtin_function_or_method_comparisons(self):
166 expected = ('builtin_function_or_method '
Benjamin Petersonf9214692009-07-02 17:19:22 +0000167 'order comparisons not supported in 3.x')
Steven Bethard6a644f92008-03-18 22:08:20 +0000168 func = eval
169 meth = {}.get
Nick Coghland2e09382008-09-11 12:11:06 +0000170 with check_warnings() as w:
Steven Bethard6a644f92008-03-18 22:08:20 +0000171 self.assertWarning(func < meth, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000172 w.reset()
Steven Bethard6a644f92008-03-18 22:08:20 +0000173 self.assertWarning(func > meth, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000174 w.reset()
Steven Bethard6a644f92008-03-18 22:08:20 +0000175 self.assertWarning(meth <= func, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000176 w.reset()
Steven Bethard6a644f92008-03-18 22:08:20 +0000177 self.assertWarning(meth >= func, w, expected)
Benjamin Petersonf9214692009-07-02 17:19:22 +0000178 w.reset()
179 self.assertNoWarning(meth == func, w)
180 self.assertNoWarning(meth != func, w)
181 lam = lambda x: x
182 self.assertNoWarning(lam == func, w)
183 self.assertNoWarning(lam != func, w)
Steven Bethard6a644f92008-03-18 22:08:20 +0000184
Raymond Hettinger05387862008-03-19 17:45:19 +0000185 def test_sort_cmp_arg(self):
Georg Brandld5b635f2008-03-25 08:29:14 +0000186 expected = "the cmp argument is not supported in 3.x"
Raymond Hettinger05387862008-03-19 17:45:19 +0000187 lst = range(5)
188 cmp = lambda x,y: -1
189
Nick Coghland2e09382008-09-11 12:11:06 +0000190 with check_warnings() as w:
Raymond Hettinger05387862008-03-19 17:45:19 +0000191 self.assertWarning(lst.sort(cmp=cmp), w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000192 w.reset()
Raymond Hettinger05387862008-03-19 17:45:19 +0000193 self.assertWarning(sorted(lst, cmp=cmp), w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000194 w.reset()
Raymond Hettinger05387862008-03-19 17:45:19 +0000195 self.assertWarning(lst.sort(cmp), w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000196 w.reset()
Raymond Hettinger05387862008-03-19 17:45:19 +0000197 self.assertWarning(sorted(lst, cmp), w, expected)
198
Georg Brandl5a444242008-03-21 20:11:46 +0000199 def test_sys_exc_clear(self):
Georg Brandld5b635f2008-03-25 08:29:14 +0000200 expected = 'sys.exc_clear() not supported in 3.x; use except clauses'
Nick Coghland2e09382008-09-11 12:11:06 +0000201 with check_warnings() as w:
Georg Brandl5a444242008-03-21 20:11:46 +0000202 self.assertWarning(sys.exc_clear(), w, expected)
203
Georg Brandl07e56812008-03-21 20:21:46 +0000204 def test_methods_members(self):
205 expected = '__members__ and __methods__ not supported in 3.x'
206 class C:
207 __methods__ = ['a']
208 __members__ = ['b']
209 c = C()
Nick Coghland2e09382008-09-11 12:11:06 +0000210 with check_warnings() as w:
Georg Brandl07e56812008-03-21 20:21:46 +0000211 self.assertWarning(dir(c), w, expected)
212
Georg Brandl65bb42d2008-03-21 20:38:24 +0000213 def test_softspace(self):
214 expected = 'file.softspace not supported in 3.x'
215 with file(__file__) as f:
Nick Coghland2e09382008-09-11 12:11:06 +0000216 with check_warnings() as w:
Georg Brandl65bb42d2008-03-21 20:38:24 +0000217 self.assertWarning(f.softspace, w, expected)
218 def set():
219 f.softspace = 0
Nick Coghland2e09382008-09-11 12:11:06 +0000220 with check_warnings() as w:
Georg Brandl65bb42d2008-03-21 20:38:24 +0000221 self.assertWarning(set(), w, expected)
222
Benjamin Peterson712ee922008-08-24 18:10:20 +0000223 def test_slice_methods(self):
224 class Spam(object):
225 def __getslice__(self, i, j): pass
226 def __setslice__(self, i, j, what): pass
227 def __delslice__(self, i, j): pass
228 class Egg:
229 def __getslice__(self, i, h): pass
230 def __setslice__(self, i, j, what): pass
231 def __delslice__(self, i, j): pass
232
233 expected = "in 3.x, __{0}slice__ has been removed; use __{0}item__"
234
235 for obj in (Spam(), Egg()):
Nick Coghland2e09382008-09-11 12:11:06 +0000236 with check_warnings() as w:
Benjamin Peterson712ee922008-08-24 18:10:20 +0000237 self.assertWarning(obj[1:2], w, expected.format('get'))
Nick Coghland2e09382008-09-11 12:11:06 +0000238 w.reset()
Benjamin Peterson712ee922008-08-24 18:10:20 +0000239 del obj[3:4]
240 self.assertWarning(None, w, expected.format('del'))
Nick Coghland2e09382008-09-11 12:11:06 +0000241 w.reset()
Benjamin Peterson712ee922008-08-24 18:10:20 +0000242 obj[4:5] = "eggs"
243 self.assertWarning(None, w, expected.format('set'))
244
Benjamin Petersonf4fcdb62008-06-08 23:00:00 +0000245 def test_tuple_parameter_unpacking(self):
246 expected = "tuple parameter unpacking has been removed in 3.x"
Nick Coghland2e09382008-09-11 12:11:06 +0000247 with check_warnings() as w:
Benjamin Petersonf4fcdb62008-06-08 23:00:00 +0000248 exec "def f((a, b)): pass"
249 self.assertWarning(None, w, expected)
250
Georg Brandl80055f62008-03-25 07:56:27 +0000251 def test_buffer(self):
Nick Coghlan48361f52008-08-11 15:45:58 +0000252 expected = 'buffer() not supported in 3.x'
Nick Coghland2e09382008-09-11 12:11:06 +0000253 with check_warnings() as w:
Georg Brandl80055f62008-03-25 07:56:27 +0000254 self.assertWarning(buffer('a'), w, expected)
255
Georg Brandla9916b52008-05-17 22:11:54 +0000256 def test_file_xreadlines(self):
257 expected = ("f.xreadlines() not supported in 3.x, "
258 "try 'for line in f' instead")
259 with file(__file__) as f:
Nick Coghland2e09382008-09-11 12:11:06 +0000260 with check_warnings() as w:
Georg Brandla9916b52008-05-17 22:11:54 +0000261 self.assertWarning(f.xreadlines(), w, expected)
262
Nick Coghlan48361f52008-08-11 15:45:58 +0000263 def test_hash_inheritance(self):
Nick Coghland2e09382008-09-11 12:11:06 +0000264 with check_warnings() as w:
Nick Coghlan48361f52008-08-11 15:45:58 +0000265 # With object as the base class
266 class WarnOnlyCmp(object):
267 def __cmp__(self, other): pass
Mark Dickinson3f918112010-06-05 13:24:04 +0000268 self.assertEqual(len(w.warnings), 0)
Nick Coghland2e09382008-09-11 12:11:06 +0000269 w.reset()
Nick Coghlan48361f52008-08-11 15:45:58 +0000270 class WarnOnlyEq(object):
271 def __eq__(self, other): pass
Nick Coghland2e09382008-09-11 12:11:06 +0000272 self.assertEqual(len(w.warnings), 1)
Nick Coghlan48361f52008-08-11 15:45:58 +0000273 self.assertWarning(None, w,
274 "Overriding __eq__ blocks inheritance of __hash__ in 3.x")
Nick Coghland2e09382008-09-11 12:11:06 +0000275 w.reset()
Nick Coghlan48361f52008-08-11 15:45:58 +0000276 class WarnCmpAndEq(object):
277 def __cmp__(self, other): pass
278 def __eq__(self, other): pass
Mark Dickinson3f918112010-06-05 13:24:04 +0000279 self.assertEqual(len(w.warnings), 1)
Nick Coghlan48361f52008-08-11 15:45:58 +0000280 self.assertWarning(None, w,
281 "Overriding __eq__ blocks inheritance of __hash__ in 3.x")
Nick Coghland2e09382008-09-11 12:11:06 +0000282 w.reset()
Nick Coghlan48361f52008-08-11 15:45:58 +0000283 class NoWarningOnlyHash(object):
284 def __hash__(self): pass
Nick Coghland2e09382008-09-11 12:11:06 +0000285 self.assertEqual(len(w.warnings), 0)
Nick Coghlan48361f52008-08-11 15:45:58 +0000286 # With an intermediate class in the heirarchy
287 class DefinesAllThree(object):
288 def __cmp__(self, other): pass
289 def __eq__(self, other): pass
290 def __hash__(self): pass
291 class WarnOnlyCmp(DefinesAllThree):
292 def __cmp__(self, other): pass
Mark Dickinson3f918112010-06-05 13:24:04 +0000293 self.assertEqual(len(w.warnings), 0)
Nick Coghland2e09382008-09-11 12:11:06 +0000294 w.reset()
Nick Coghlan48361f52008-08-11 15:45:58 +0000295 class WarnOnlyEq(DefinesAllThree):
296 def __eq__(self, other): pass
Nick Coghland2e09382008-09-11 12:11:06 +0000297 self.assertEqual(len(w.warnings), 1)
Nick Coghlan48361f52008-08-11 15:45:58 +0000298 self.assertWarning(None, w,
299 "Overriding __eq__ blocks inheritance of __hash__ in 3.x")
Nick Coghland2e09382008-09-11 12:11:06 +0000300 w.reset()
Nick Coghlan48361f52008-08-11 15:45:58 +0000301 class WarnCmpAndEq(DefinesAllThree):
302 def __cmp__(self, other): pass
303 def __eq__(self, other): pass
Mark Dickinson3f918112010-06-05 13:24:04 +0000304 self.assertEqual(len(w.warnings), 1)
Nick Coghlan48361f52008-08-11 15:45:58 +0000305 self.assertWarning(None, w,
306 "Overriding __eq__ blocks inheritance of __hash__ in 3.x")
Nick Coghland2e09382008-09-11 12:11:06 +0000307 w.reset()
Nick Coghlan48361f52008-08-11 15:45:58 +0000308 class NoWarningOnlyHash(DefinesAllThree):
309 def __hash__(self): pass
Nick Coghland2e09382008-09-11 12:11:06 +0000310 self.assertEqual(len(w.warnings), 0)
Nick Coghlan48361f52008-08-11 15:45:58 +0000311
Georg Brandl07e56812008-03-21 20:21:46 +0000312
Brett Cannone5d2cba2008-05-06 23:23:34 +0000313class TestStdlibRemovals(unittest.TestCase):
314
Brett Cannon3c759142008-05-09 05:25:37 +0000315 # test.testall not tested as it executes all unit tests as an
316 # import side-effect.
Brett Cannon4c1f8812008-05-10 02:27:04 +0000317 all_platforms = ('audiodev', 'imputil', 'mutex', 'user', 'new', 'rexec',
Brett Cannon1e8fba72008-07-18 19:30:22 +0000318 'Bastion', 'compiler', 'dircache', 'mimetools',
319 'fpformat', 'ihooks', 'mhlib', 'statvfs', 'htmllib',
320 'sgmllib', 'rfc822', 'sunaudio')
Brett Cannon54c77aa2008-05-14 21:08:41 +0000321 inclusive_platforms = {'irix' : ('pure', 'AL', 'al', 'CD', 'cd', 'cddb',
Brett Cannon044616a2008-05-15 02:33:55 +0000322 'cdplayer', 'CL', 'cl', 'DEVICE', 'GL',
Brett Cannon75ba4652008-05-15 03:23:17 +0000323 'gl', 'ERRNO', 'FILE', 'FL', 'flp', 'fl',
Brett Cannond8c41ec2008-05-15 03:41:55 +0000324 'fm', 'GET', 'GLWS', 'imgfile', 'IN',
Brett Cannoncd2de082008-05-15 03:51:21 +0000325 'IOCTL', 'jpeg', 'panel', 'panelparser',
Brett Cannon74a596c2008-05-15 04:17:35 +0000326 'readcd', 'SV', 'torgb', 'WAIT'),
Benjamin Peterson23681932008-05-12 21:42:13 +0000327 'darwin' : ('autoGIL', 'Carbon', 'OSATerminology',
Brett Cannonea785fb2008-05-14 01:09:40 +0000328 'icglue', 'Nav', 'MacOS', 'aepack',
329 'aetools', 'aetypes', 'applesingle',
330 'appletrawmain', 'appletrunner',
331 'argvemulator', 'bgenlocations',
Benjamin Peterson23681932008-05-12 21:42:13 +0000332 'EasyDialogs', 'macerrors', 'macostools',
333 'findertools', 'FrameWork', 'ic',
334 'gensuitemodule', 'icopen', 'macresource',
335 'MiniAEFrame', 'pimp', 'PixMapWrapper',
Brett Cannonea785fb2008-05-14 01:09:40 +0000336 'terminalcommand', 'videoreader',
337 '_builtinSuites', 'CodeWarrior',
338 'Explorer', 'Finder', 'Netscape',
339 'StdSuites', 'SystemEvents', 'Terminal',
340 'cfmfile', 'bundlebuilder', 'buildtools',
Benjamin Petersona6864e02008-07-14 17:42:17 +0000341 'ColorPicker', 'Audio_mac'),
Brett Cannon22248172008-05-16 00:10:24 +0000342 'sunos5' : ('sunaudiodev', 'SUNAUDIODEV'),
343 }
Brett Cannonac861b52008-05-12 03:45:59 +0000344 optional_modules = ('bsddb185', 'Canvas', 'dl', 'linuxaudiodev', 'imageop',
Antoine Pitrou0b074572010-01-08 19:21:34 +0000345 'sv', 'bsddb', 'dbhash')
Brett Cannone5d2cba2008-05-06 23:23:34 +0000346
Brett Cannon9ac39742008-05-09 22:51:58 +0000347 def check_removal(self, module_name, optional=False):
Brett Cannone5d2cba2008-05-06 23:23:34 +0000348 """Make sure the specified module, when imported, raises a
349 DeprecationWarning and specifies itself in the message."""
Brett Cannon672237d2008-09-09 00:49:16 +0000350 with nested(CleanImport(module_name), warnings.catch_warnings()):
Ezio Melottif9c2f4f2010-08-02 21:48:39 +0000351 warnings.filterwarnings("error", ".+ (module|package) .+ removed",
352 DeprecationWarning, __name__)
353 warnings.filterwarnings("error", ".+ removed .+ (module|package)",
Benjamin Petersona6864e02008-07-14 17:42:17 +0000354 DeprecationWarning, __name__)
355 try:
356 __import__(module_name, level=0)
357 except DeprecationWarning as exc:
358 self.assert_(module_name in exc.args[0],
359 "%s warning didn't contain module name"
360 % module_name)
361 except ImportError:
362 if not optional:
363 self.fail("Non-optional module {0} raised an "
364 "ImportError.".format(module_name))
365 else:
Ezio Melottif9c2f4f2010-08-02 21:48:39 +0000366 # For extension modules, check the __warningregistry__.
367 # They won't rerun their init code even with CleanImport.
368 if not check_deprecated_module(module_name):
369 self.fail("DeprecationWarning not raised for {0}"
Benjamin Petersona6864e02008-07-14 17:42:17 +0000370 .format(module_name))
Brett Cannone5d2cba2008-05-06 23:23:34 +0000371
372 def test_platform_independent_removals(self):
373 # Make sure that the modules that are available on all platforms raise
374 # the proper DeprecationWarning.
375 for module_name in self.all_platforms:
376 self.check_removal(module_name)
377
Brett Cannon9ac39742008-05-09 22:51:58 +0000378 def test_platform_specific_removals(self):
379 # Test the removal of platform-specific modules.
380 for module_name in self.inclusive_platforms.get(sys.platform, []):
381 self.check_removal(module_name, optional=True)
382
Brett Cannon768d44f2008-05-10 02:47:54 +0000383 def test_optional_module_removals(self):
384 # Test the removal of modules that may or may not be built.
385 for module_name in self.optional_modules:
386 self.check_removal(module_name, optional=True)
387
Benjamin Peterson0893a0a2008-05-09 00:27:01 +0000388 def test_os_path_walk(self):
389 msg = "In 3.x, os.path.walk is removed in favor of os.walk."
390 def dumbo(where, names, args): pass
391 for path_mod in ("ntpath", "macpath", "os2emxpath", "posixpath"):
392 mod = __import__(path_mod)
Nick Coghland2e09382008-09-11 12:11:06 +0000393 reset_module_registry(mod)
394 with check_warnings() as w:
Benjamin Peterson1d310232008-05-27 01:42:29 +0000395 mod.walk("crashers", dumbo, None)
Nick Coghland2e09382008-09-11 12:11:06 +0000396 self.assertEquals(str(w.message), msg)
Benjamin Peterson0893a0a2008-05-09 00:27:01 +0000397
Benjamin Peterson3aa84a72008-05-26 19:41:53 +0000398 def test_commands_members(self):
399 import commands
Nick Coghland2e09382008-09-11 12:11:06 +0000400 # commands module tests may have already triggered this warning
401 reset_module_registry(commands)
Benjamin Peterson3aa84a72008-05-26 19:41:53 +0000402 members = {"mk2arg" : 2, "mkarg" : 1, "getstatus" : 1}
403 for name, arg_count in members.items():
Brett Cannon672237d2008-09-09 00:49:16 +0000404 with warnings.catch_warnings():
Benjamin Peterson3aa84a72008-05-26 19:41:53 +0000405 warnings.filterwarnings("error")
406 func = getattr(commands, name)
407 self.assertRaises(DeprecationWarning, func, *([None]*arg_count))
408
Benjamin Peterson541f7da2008-08-18 02:12:23 +0000409 def test_reduce_move(self):
410 from operator import add
Nick Coghland2e09382008-09-11 12:11:06 +0000411 # reduce tests may have already triggered this warning
412 reset_module_registry(unittest)
Brett Cannon672237d2008-09-09 00:49:16 +0000413 with warnings.catch_warnings():
Benjamin Peterson541f7da2008-08-18 02:12:23 +0000414 warnings.filterwarnings("error", "reduce")
415 self.assertRaises(DeprecationWarning, reduce, add, range(10))
416
Brett Cannonabb34fe2008-05-29 05:08:50 +0000417 def test_mutablestring_removal(self):
418 # UserString.MutableString has been removed in 3.0.
419 import UserString
Nick Coghland2e09382008-09-11 12:11:06 +0000420 # UserString tests may have already triggered this warning
421 reset_module_registry(UserString)
Brett Cannon672237d2008-09-09 00:49:16 +0000422 with warnings.catch_warnings():
Brett Cannonabb34fe2008-05-29 05:08:50 +0000423 warnings.filterwarnings("error", ".*MutableString",
424 DeprecationWarning)
425 self.assertRaises(DeprecationWarning, UserString.MutableString)
426
Brett Cannone5d2cba2008-05-06 23:23:34 +0000427
Steven Bethardae42f332008-03-18 17:26:10 +0000428def test_main():
Ezio Melottif9c2f4f2010-08-02 21:48:39 +0000429 run_unittest(TestPy3KWarnings,
430 TestStdlibRemovals)
Steven Bethardae42f332008-03-18 17:26:10 +0000431
432if __name__ == '__main__':
433 test_main()