blob: 077941a79b28822bd8c5902e26f2475c927426fc [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 Petersonf9214692009-07-02 17:19:22 +000025 def assertNoWarning(self, _, recorder):
26 self.assertEqual(len(recorder.warnings), 0)
27
Benjamin Peterson2fe3ef82008-06-08 02:05:33 +000028 def test_backquote(self):
29 expected = 'backquote not supported in 3.x; use repr()'
Nick Coghland2e09382008-09-11 12:11:06 +000030 with check_warnings() as w:
Benjamin Peterson2fe3ef82008-06-08 02:05:33 +000031 exec "`2`" in {}
32 self.assertWarning(None, w, expected)
33
Benjamin Peterson440847c2009-11-19 23:01:36 +000034 def test_paren_arg_names(self):
35 expected = 'parenthesized argument names are invalid in 3.x'
36 def check(s):
37 exec s in {}
38 self.assertWarning(None, w, expected)
39 with check_warnings() as w:
40 check("def f((x)): pass")
41 check("def f((((x))), (y)): pass")
42 check("def f((x), (((y))), m=32): pass")
43 # Something like def f((a, (b))): pass will raise the tuple
44 # unpacking warning.
45
Benjamin Petersond5efd202008-06-08 22:52:37 +000046 def test_bool_assign(self):
47 # So we don't screw up our globals
48 def safe_exec(expr):
49 def f(**kwargs): pass
50 exec expr in {'f' : f}
51
52 expected = "assignment to True or False is forbidden in 3.x"
Nick Coghland2e09382008-09-11 12:11:06 +000053 with check_warnings() as w:
Benjamin Petersond5efd202008-06-08 22:52:37 +000054 safe_exec("True = False")
55 self.assertWarning(None, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +000056 w.reset()
Benjamin Petersond5efd202008-06-08 22:52:37 +000057 safe_exec("False = True")
58 self.assertWarning(None, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +000059 w.reset()
Benjamin Petersond5efd202008-06-08 22:52:37 +000060 try:
61 safe_exec("obj.False = True")
62 except NameError: pass
63 self.assertWarning(None, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +000064 w.reset()
Benjamin Petersond5efd202008-06-08 22:52:37 +000065 try:
66 safe_exec("obj.True = False")
67 except NameError: pass
68 self.assertWarning(None, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +000069 w.reset()
Benjamin Petersond5efd202008-06-08 22:52:37 +000070 safe_exec("def False(): pass")
71 self.assertWarning(None, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +000072 w.reset()
Benjamin Petersond5efd202008-06-08 22:52:37 +000073 safe_exec("def True(): pass")
74 self.assertWarning(None, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +000075 w.reset()
Benjamin Petersond5efd202008-06-08 22:52:37 +000076 safe_exec("class False: pass")
77 self.assertWarning(None, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +000078 w.reset()
Benjamin Petersond5efd202008-06-08 22:52:37 +000079 safe_exec("class True: 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 safe_exec("def f(True=43): pass")
83 self.assertWarning(None, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +000084 w.reset()
Benjamin Petersond5efd202008-06-08 22:52:37 +000085 safe_exec("def f(False=None): pass")
86 self.assertWarning(None, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +000087 w.reset()
Benjamin Petersond5efd202008-06-08 22:52:37 +000088 safe_exec("f(False=True)")
89 self.assertWarning(None, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +000090 w.reset()
Benjamin Petersond5efd202008-06-08 22:52:37 +000091 safe_exec("f(True=1)")
92 self.assertWarning(None, w, expected)
93
94
Steven Bethardae42f332008-03-18 17:26:10 +000095 def test_type_inequality_comparisons(self):
Georg Brandld5b635f2008-03-25 08:29:14 +000096 expected = 'type inequality comparisons not supported in 3.x'
Nick Coghland2e09382008-09-11 12:11:06 +000097 with check_warnings() as w:
Steven Bethardae42f332008-03-18 17:26:10 +000098 self.assertWarning(int < str, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +000099 w.reset()
Steven Bethardae42f332008-03-18 17:26:10 +0000100 self.assertWarning(type < object, w, expected)
101
102 def test_object_inequality_comparisons(self):
Georg Brandld5b635f2008-03-25 08:29:14 +0000103 expected = 'comparing unequal types not supported in 3.x'
Nick Coghland2e09382008-09-11 12:11:06 +0000104 with check_warnings() as w:
Steven Bethardae42f332008-03-18 17:26:10 +0000105 self.assertWarning(str < [], w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000106 w.reset()
Steven Bethardae42f332008-03-18 17:26:10 +0000107 self.assertWarning(object() < (1, 2), w, expected)
108
109 def test_dict_inequality_comparisons(self):
Georg Brandld5b635f2008-03-25 08:29:14 +0000110 expected = 'dict inequality comparisons not supported in 3.x'
Nick Coghland2e09382008-09-11 12:11:06 +0000111 with check_warnings() as w:
Steven Bethardae42f332008-03-18 17:26:10 +0000112 self.assertWarning({} < {2:3}, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000113 w.reset()
Steven Bethardae42f332008-03-18 17:26:10 +0000114 self.assertWarning({} <= {}, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000115 w.reset()
Steven Bethardae42f332008-03-18 17:26:10 +0000116 self.assertWarning({} > {2:3}, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000117 w.reset()
Steven Bethardae42f332008-03-18 17:26:10 +0000118 self.assertWarning({2:3} >= {}, w, expected)
119
120 def test_cell_inequality_comparisons(self):
Georg Brandld5b635f2008-03-25 08:29:14 +0000121 expected = 'cell comparisons not supported in 3.x'
Steven Bethardae42f332008-03-18 17:26:10 +0000122 def f(x):
123 def g():
124 return x
125 return g
126 cell0, = f(0).func_closure
127 cell1, = f(1).func_closure
Nick Coghland2e09382008-09-11 12:11:06 +0000128 with check_warnings() as w:
Steven Bethardae42f332008-03-18 17:26:10 +0000129 self.assertWarning(cell0 == cell1, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000130 w.reset()
Steven Bethardae42f332008-03-18 17:26:10 +0000131 self.assertWarning(cell0 < cell1, w, expected)
132
Steven Bethard6a644f92008-03-18 22:08:20 +0000133 def test_code_inequality_comparisons(self):
Georg Brandld5b635f2008-03-25 08:29:14 +0000134 expected = 'code inequality comparisons not supported in 3.x'
Steven Bethard6a644f92008-03-18 22:08:20 +0000135 def f(x):
136 pass
137 def g(x):
138 pass
Nick Coghland2e09382008-09-11 12:11:06 +0000139 with check_warnings() as w:
Steven Bethard6a644f92008-03-18 22:08:20 +0000140 self.assertWarning(f.func_code < g.func_code, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000141 w.reset()
Steven Bethard6a644f92008-03-18 22:08:20 +0000142 self.assertWarning(f.func_code <= g.func_code, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000143 w.reset()
Steven Bethard6a644f92008-03-18 22:08:20 +0000144 self.assertWarning(f.func_code >= g.func_code, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000145 w.reset()
Steven Bethard6a644f92008-03-18 22:08:20 +0000146 self.assertWarning(f.func_code > g.func_code, w, expected)
147
148 def test_builtin_function_or_method_comparisons(self):
149 expected = ('builtin_function_or_method '
Benjamin Petersonf9214692009-07-02 17:19:22 +0000150 'order comparisons not supported in 3.x')
Steven Bethard6a644f92008-03-18 22:08:20 +0000151 func = eval
152 meth = {}.get
Nick Coghland2e09382008-09-11 12:11:06 +0000153 with check_warnings() as w:
Steven Bethard6a644f92008-03-18 22:08:20 +0000154 self.assertWarning(func < meth, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000155 w.reset()
Steven Bethard6a644f92008-03-18 22:08:20 +0000156 self.assertWarning(func > meth, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000157 w.reset()
Steven Bethard6a644f92008-03-18 22:08:20 +0000158 self.assertWarning(meth <= func, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000159 w.reset()
Steven Bethard6a644f92008-03-18 22:08:20 +0000160 self.assertWarning(meth >= func, w, expected)
Benjamin Petersonf9214692009-07-02 17:19:22 +0000161 w.reset()
162 self.assertNoWarning(meth == func, w)
163 self.assertNoWarning(meth != func, w)
164 lam = lambda x: x
165 self.assertNoWarning(lam == func, w)
166 self.assertNoWarning(lam != func, w)
Steven Bethard6a644f92008-03-18 22:08:20 +0000167
Raymond Hettinger05387862008-03-19 17:45:19 +0000168 def test_sort_cmp_arg(self):
Georg Brandld5b635f2008-03-25 08:29:14 +0000169 expected = "the cmp argument is not supported in 3.x"
Raymond Hettinger05387862008-03-19 17:45:19 +0000170 lst = range(5)
171 cmp = lambda x,y: -1
172
Nick Coghland2e09382008-09-11 12:11:06 +0000173 with check_warnings() as w:
Raymond Hettinger05387862008-03-19 17:45:19 +0000174 self.assertWarning(lst.sort(cmp=cmp), w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000175 w.reset()
Raymond Hettinger05387862008-03-19 17:45:19 +0000176 self.assertWarning(sorted(lst, cmp=cmp), w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000177 w.reset()
Raymond Hettinger05387862008-03-19 17:45:19 +0000178 self.assertWarning(lst.sort(cmp), w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000179 w.reset()
Raymond Hettinger05387862008-03-19 17:45:19 +0000180 self.assertWarning(sorted(lst, cmp), w, expected)
181
Georg Brandl5a444242008-03-21 20:11:46 +0000182 def test_sys_exc_clear(self):
Georg Brandld5b635f2008-03-25 08:29:14 +0000183 expected = 'sys.exc_clear() not supported in 3.x; use except clauses'
Nick Coghland2e09382008-09-11 12:11:06 +0000184 with check_warnings() as w:
Georg Brandl5a444242008-03-21 20:11:46 +0000185 self.assertWarning(sys.exc_clear(), w, expected)
186
Georg Brandl07e56812008-03-21 20:21:46 +0000187 def test_methods_members(self):
188 expected = '__members__ and __methods__ not supported in 3.x'
189 class C:
190 __methods__ = ['a']
191 __members__ = ['b']
192 c = C()
Nick Coghland2e09382008-09-11 12:11:06 +0000193 with check_warnings() as w:
Georg Brandl07e56812008-03-21 20:21:46 +0000194 self.assertWarning(dir(c), w, expected)
195
Georg Brandl65bb42d2008-03-21 20:38:24 +0000196 def test_softspace(self):
197 expected = 'file.softspace not supported in 3.x'
198 with file(__file__) as f:
Nick Coghland2e09382008-09-11 12:11:06 +0000199 with check_warnings() as w:
Georg Brandl65bb42d2008-03-21 20:38:24 +0000200 self.assertWarning(f.softspace, w, expected)
201 def set():
202 f.softspace = 0
Nick Coghland2e09382008-09-11 12:11:06 +0000203 with check_warnings() as w:
Georg Brandl65bb42d2008-03-21 20:38:24 +0000204 self.assertWarning(set(), w, expected)
205
Benjamin Peterson712ee922008-08-24 18:10:20 +0000206 def test_slice_methods(self):
207 class Spam(object):
208 def __getslice__(self, i, j): pass
209 def __setslice__(self, i, j, what): pass
210 def __delslice__(self, i, j): pass
211 class Egg:
212 def __getslice__(self, i, h): pass
213 def __setslice__(self, i, j, what): pass
214 def __delslice__(self, i, j): pass
215
216 expected = "in 3.x, __{0}slice__ has been removed; use __{0}item__"
217
218 for obj in (Spam(), Egg()):
Nick Coghland2e09382008-09-11 12:11:06 +0000219 with check_warnings() as w:
Benjamin Peterson712ee922008-08-24 18:10:20 +0000220 self.assertWarning(obj[1:2], w, expected.format('get'))
Nick Coghland2e09382008-09-11 12:11:06 +0000221 w.reset()
Benjamin Peterson712ee922008-08-24 18:10:20 +0000222 del obj[3:4]
223 self.assertWarning(None, w, expected.format('del'))
Nick Coghland2e09382008-09-11 12:11:06 +0000224 w.reset()
Benjamin Peterson712ee922008-08-24 18:10:20 +0000225 obj[4:5] = "eggs"
226 self.assertWarning(None, w, expected.format('set'))
227
Benjamin Petersonf4fcdb62008-06-08 23:00:00 +0000228 def test_tuple_parameter_unpacking(self):
229 expected = "tuple parameter unpacking has been removed in 3.x"
Nick Coghland2e09382008-09-11 12:11:06 +0000230 with check_warnings() as w:
Benjamin Petersonf4fcdb62008-06-08 23:00:00 +0000231 exec "def f((a, b)): pass"
232 self.assertWarning(None, w, expected)
233
Georg Brandl80055f62008-03-25 07:56:27 +0000234 def test_buffer(self):
Nick Coghlan48361f52008-08-11 15:45:58 +0000235 expected = 'buffer() not supported in 3.x'
Nick Coghland2e09382008-09-11 12:11:06 +0000236 with check_warnings() as w:
Georg Brandl80055f62008-03-25 07:56:27 +0000237 self.assertWarning(buffer('a'), w, expected)
238
Georg Brandla9916b52008-05-17 22:11:54 +0000239 def test_file_xreadlines(self):
240 expected = ("f.xreadlines() not supported in 3.x, "
241 "try 'for line in f' instead")
242 with file(__file__) as f:
Nick Coghland2e09382008-09-11 12:11:06 +0000243 with check_warnings() as w:
Georg Brandla9916b52008-05-17 22:11:54 +0000244 self.assertWarning(f.xreadlines(), w, expected)
245
Nick Coghlan48361f52008-08-11 15:45:58 +0000246 def test_hash_inheritance(self):
Nick Coghland2e09382008-09-11 12:11:06 +0000247 with check_warnings() as w:
Nick Coghlan48361f52008-08-11 15:45:58 +0000248 # With object as the base class
249 class WarnOnlyCmp(object):
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(object):
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(object):
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(object):
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 # With an intermediate class in the heirarchy
274 class DefinesAllThree(object):
275 def __cmp__(self, other): pass
276 def __eq__(self, other): pass
277 def __hash__(self): pass
278 class WarnOnlyCmp(DefinesAllThree):
279 def __cmp__(self, other): pass
Nick Coghland2e09382008-09-11 12:11:06 +0000280 self.assertEqual(len(w.warnings), 1)
Nick Coghlan48361f52008-08-11 15:45:58 +0000281 self.assertWarning(None, w,
282 "Overriding __cmp__ blocks inheritance of __hash__ in 3.x")
Nick Coghland2e09382008-09-11 12:11:06 +0000283 w.reset()
Nick Coghlan48361f52008-08-11 15:45:58 +0000284 class WarnOnlyEq(DefinesAllThree):
285 def __eq__(self, other): pass
Nick Coghland2e09382008-09-11 12:11:06 +0000286 self.assertEqual(len(w.warnings), 1)
Nick Coghlan48361f52008-08-11 15:45:58 +0000287 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 WarnCmpAndEq(DefinesAllThree):
291 def __cmp__(self, other): pass
292 def __eq__(self, other): pass
Nick Coghland2e09382008-09-11 12:11:06 +0000293 self.assertEqual(len(w.warnings), 2)
294 self.assertWarning(None, w.warnings[0],
Nick Coghlan48361f52008-08-11 15:45:58 +0000295 "Overriding __cmp__ blocks inheritance of __hash__ in 3.x")
296 self.assertWarning(None, w,
297 "Overriding __eq__ blocks inheritance of __hash__ in 3.x")
Nick Coghland2e09382008-09-11 12:11:06 +0000298 w.reset()
Nick Coghlan48361f52008-08-11 15:45:58 +0000299 class NoWarningOnlyHash(DefinesAllThree):
300 def __hash__(self): pass
Nick Coghland2e09382008-09-11 12:11:06 +0000301 self.assertEqual(len(w.warnings), 0)
Nick Coghlan48361f52008-08-11 15:45:58 +0000302
Georg Brandl07e56812008-03-21 20:21:46 +0000303
Brett Cannone5d2cba2008-05-06 23:23:34 +0000304class TestStdlibRemovals(unittest.TestCase):
305
Brett Cannon3c759142008-05-09 05:25:37 +0000306 # test.testall not tested as it executes all unit tests as an
307 # import side-effect.
Brett Cannon4c1f8812008-05-10 02:27:04 +0000308 all_platforms = ('audiodev', 'imputil', 'mutex', 'user', 'new', 'rexec',
Brett Cannon1e8fba72008-07-18 19:30:22 +0000309 'Bastion', 'compiler', 'dircache', 'mimetools',
310 'fpformat', 'ihooks', 'mhlib', 'statvfs', 'htmllib',
311 'sgmllib', 'rfc822', 'sunaudio')
Brett Cannon54c77aa2008-05-14 21:08:41 +0000312 inclusive_platforms = {'irix' : ('pure', 'AL', 'al', 'CD', 'cd', 'cddb',
Brett Cannon044616a2008-05-15 02:33:55 +0000313 'cdplayer', 'CL', 'cl', 'DEVICE', 'GL',
Brett Cannon75ba4652008-05-15 03:23:17 +0000314 'gl', 'ERRNO', 'FILE', 'FL', 'flp', 'fl',
Brett Cannond8c41ec2008-05-15 03:41:55 +0000315 'fm', 'GET', 'GLWS', 'imgfile', 'IN',
Brett Cannoncd2de082008-05-15 03:51:21 +0000316 'IOCTL', 'jpeg', 'panel', 'panelparser',
Brett Cannon74a596c2008-05-15 04:17:35 +0000317 'readcd', 'SV', 'torgb', 'WAIT'),
Benjamin Peterson23681932008-05-12 21:42:13 +0000318 'darwin' : ('autoGIL', 'Carbon', 'OSATerminology',
Brett Cannonea785fb2008-05-14 01:09:40 +0000319 'icglue', 'Nav', 'MacOS', 'aepack',
320 'aetools', 'aetypes', 'applesingle',
321 'appletrawmain', 'appletrunner',
322 'argvemulator', 'bgenlocations',
Benjamin Peterson23681932008-05-12 21:42:13 +0000323 'EasyDialogs', 'macerrors', 'macostools',
324 'findertools', 'FrameWork', 'ic',
325 'gensuitemodule', 'icopen', 'macresource',
326 'MiniAEFrame', 'pimp', 'PixMapWrapper',
Brett Cannonea785fb2008-05-14 01:09:40 +0000327 'terminalcommand', 'videoreader',
328 '_builtinSuites', 'CodeWarrior',
329 'Explorer', 'Finder', 'Netscape',
330 'StdSuites', 'SystemEvents', 'Terminal',
331 'cfmfile', 'bundlebuilder', 'buildtools',
Benjamin Petersona6864e02008-07-14 17:42:17 +0000332 'ColorPicker', 'Audio_mac'),
Brett Cannon22248172008-05-16 00:10:24 +0000333 'sunos5' : ('sunaudiodev', 'SUNAUDIODEV'),
334 }
Brett Cannonac861b52008-05-12 03:45:59 +0000335 optional_modules = ('bsddb185', 'Canvas', 'dl', 'linuxaudiodev', 'imageop',
Antoine Pitrou0b074572010-01-08 19:21:34 +0000336 'sv', 'bsddb', 'dbhash')
Brett Cannone5d2cba2008-05-06 23:23:34 +0000337
Brett Cannon9ac39742008-05-09 22:51:58 +0000338 def check_removal(self, module_name, optional=False):
Brett Cannone5d2cba2008-05-06 23:23:34 +0000339 """Make sure the specified module, when imported, raises a
340 DeprecationWarning and specifies itself in the message."""
Brett Cannon672237d2008-09-09 00:49:16 +0000341 with nested(CleanImport(module_name), warnings.catch_warnings()):
Nick Coghland2e09382008-09-11 12:11:06 +0000342 # XXX: This is not quite enough for extension modules - those
343 # won't rerun their init code even with CleanImport.
344 # You can see this easily by running the whole test suite with -3
Benjamin Petersona6864e02008-07-14 17:42:17 +0000345 warnings.filterwarnings("error", ".+ removed",
346 DeprecationWarning, __name__)
347 try:
348 __import__(module_name, level=0)
349 except DeprecationWarning as exc:
350 self.assert_(module_name in exc.args[0],
351 "%s warning didn't contain module name"
352 % module_name)
353 except ImportError:
354 if not optional:
355 self.fail("Non-optional module {0} raised an "
356 "ImportError.".format(module_name))
357 else:
358 self.fail("DeprecationWarning not raised for {0}"
359 .format(module_name))
Brett Cannone5d2cba2008-05-06 23:23:34 +0000360
361 def test_platform_independent_removals(self):
362 # Make sure that the modules that are available on all platforms raise
363 # the proper DeprecationWarning.
364 for module_name in self.all_platforms:
365 self.check_removal(module_name)
366
Brett Cannon9ac39742008-05-09 22:51:58 +0000367 def test_platform_specific_removals(self):
368 # Test the removal of platform-specific modules.
369 for module_name in self.inclusive_platforms.get(sys.platform, []):
370 self.check_removal(module_name, optional=True)
371
Brett Cannon768d44f2008-05-10 02:47:54 +0000372 def test_optional_module_removals(self):
373 # Test the removal of modules that may or may not be built.
374 for module_name in self.optional_modules:
375 self.check_removal(module_name, optional=True)
376
Benjamin Peterson0893a0a2008-05-09 00:27:01 +0000377 def test_os_path_walk(self):
378 msg = "In 3.x, os.path.walk is removed in favor of os.walk."
379 def dumbo(where, names, args): pass
380 for path_mod in ("ntpath", "macpath", "os2emxpath", "posixpath"):
381 mod = __import__(path_mod)
Nick Coghland2e09382008-09-11 12:11:06 +0000382 reset_module_registry(mod)
383 with check_warnings() as w:
Benjamin Peterson1d310232008-05-27 01:42:29 +0000384 mod.walk("crashers", dumbo, None)
Nick Coghland2e09382008-09-11 12:11:06 +0000385 self.assertEquals(str(w.message), msg)
Benjamin Peterson0893a0a2008-05-09 00:27:01 +0000386
Benjamin Peterson3aa84a72008-05-26 19:41:53 +0000387 def test_commands_members(self):
388 import commands
Nick Coghland2e09382008-09-11 12:11:06 +0000389 # commands module tests may have already triggered this warning
390 reset_module_registry(commands)
Benjamin Peterson3aa84a72008-05-26 19:41:53 +0000391 members = {"mk2arg" : 2, "mkarg" : 1, "getstatus" : 1}
392 for name, arg_count in members.items():
Brett Cannon672237d2008-09-09 00:49:16 +0000393 with warnings.catch_warnings():
Benjamin Peterson3aa84a72008-05-26 19:41:53 +0000394 warnings.filterwarnings("error")
395 func = getattr(commands, name)
396 self.assertRaises(DeprecationWarning, func, *([None]*arg_count))
397
Benjamin Peterson541f7da2008-08-18 02:12:23 +0000398 def test_reduce_move(self):
399 from operator import add
Nick Coghland2e09382008-09-11 12:11:06 +0000400 # reduce tests may have already triggered this warning
401 reset_module_registry(unittest)
Brett Cannon672237d2008-09-09 00:49:16 +0000402 with warnings.catch_warnings():
Benjamin Peterson541f7da2008-08-18 02:12:23 +0000403 warnings.filterwarnings("error", "reduce")
404 self.assertRaises(DeprecationWarning, reduce, add, range(10))
405
Brett Cannonabb34fe2008-05-29 05:08:50 +0000406 def test_mutablestring_removal(self):
407 # UserString.MutableString has been removed in 3.0.
408 import UserString
Nick Coghland2e09382008-09-11 12:11:06 +0000409 # UserString tests may have already triggered this warning
410 reset_module_registry(UserString)
Brett Cannon672237d2008-09-09 00:49:16 +0000411 with warnings.catch_warnings():
Brett Cannonabb34fe2008-05-29 05:08:50 +0000412 warnings.filterwarnings("error", ".*MutableString",
413 DeprecationWarning)
414 self.assertRaises(DeprecationWarning, UserString.MutableString)
415
Brett Cannone5d2cba2008-05-06 23:23:34 +0000416
Steven Bethardae42f332008-03-18 17:26:10 +0000417def test_main():
Nick Coghland2e09382008-09-11 12:11:06 +0000418 with check_warnings():
Benjamin Peterson1d310232008-05-27 01:42:29 +0000419 warnings.simplefilter("always")
420 run_unittest(TestPy3KWarnings,
421 TestStdlibRemovals)
Steven Bethardae42f332008-03-18 17:26:10 +0000422
423if __name__ == '__main__':
424 test_main()