blob: 780de740676e5e845c93599a36716f730cc9f0d2 [file] [log] [blame]
Steven Bethardae42f332008-03-18 17:26:10 +00001import unittest
Brett Cannon977eb022008-03-19 17:37:43 +00002import sys
Alexandre Vassalottieb83f702008-05-11 07:06:04 +00003from test.test_support import (catch_warning, 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
Steven Bethardae42f332008-03-18 17:26:10 +000012
13class TestPy3KWarnings(unittest.TestCase):
14
Nick Coghlan48361f52008-08-11 15:45:58 +000015 def assertWarning(self, _, warning, expected_message):
16 self.assertEqual(str(warning.message), expected_message)
17
Benjamin Peterson2fe3ef82008-06-08 02:05:33 +000018 def test_backquote(self):
19 expected = 'backquote not supported in 3.x; use repr()'
20 with catch_warning() as w:
21 exec "`2`" in {}
22 self.assertWarning(None, w, expected)
23
Benjamin Petersond5efd202008-06-08 22:52:37 +000024 def test_bool_assign(self):
25 # So we don't screw up our globals
26 def safe_exec(expr):
27 def f(**kwargs): pass
28 exec expr in {'f' : f}
29
30 expected = "assignment to True or False is forbidden in 3.x"
31 with catch_warning() as w:
32 safe_exec("True = False")
33 self.assertWarning(None, w, expected)
Nick Coghlan48361f52008-08-11 15:45:58 +000034 w.reset()
Benjamin Petersond5efd202008-06-08 22:52:37 +000035 safe_exec("False = True")
36 self.assertWarning(None, w, expected)
Nick Coghlan48361f52008-08-11 15:45:58 +000037 w.reset()
Benjamin Petersond5efd202008-06-08 22:52:37 +000038 try:
39 safe_exec("obj.False = True")
40 except NameError: pass
41 self.assertWarning(None, w, expected)
Nick Coghlan48361f52008-08-11 15:45:58 +000042 w.reset()
Benjamin Petersond5efd202008-06-08 22:52:37 +000043 try:
44 safe_exec("obj.True = False")
45 except NameError: pass
46 self.assertWarning(None, w, expected)
Nick Coghlan48361f52008-08-11 15:45:58 +000047 w.reset()
Benjamin Petersond5efd202008-06-08 22:52:37 +000048 safe_exec("def False(): pass")
49 self.assertWarning(None, w, expected)
Nick Coghlan48361f52008-08-11 15:45:58 +000050 w.reset()
Benjamin Petersond5efd202008-06-08 22:52:37 +000051 safe_exec("def True(): pass")
52 self.assertWarning(None, w, expected)
Nick Coghlan48361f52008-08-11 15:45:58 +000053 w.reset()
Benjamin Petersond5efd202008-06-08 22:52:37 +000054 safe_exec("class False: pass")
55 self.assertWarning(None, w, expected)
Nick Coghlan48361f52008-08-11 15:45:58 +000056 w.reset()
Benjamin Petersond5efd202008-06-08 22:52:37 +000057 safe_exec("class True: pass")
58 self.assertWarning(None, w, expected)
Nick Coghlan48361f52008-08-11 15:45:58 +000059 w.reset()
Benjamin Petersond5efd202008-06-08 22:52:37 +000060 safe_exec("def f(True=43): pass")
61 self.assertWarning(None, w, expected)
Nick Coghlan48361f52008-08-11 15:45:58 +000062 w.reset()
Benjamin Petersond5efd202008-06-08 22:52:37 +000063 safe_exec("def f(False=None): pass")
64 self.assertWarning(None, w, expected)
Nick Coghlan48361f52008-08-11 15:45:58 +000065 w.reset()
Benjamin Petersond5efd202008-06-08 22:52:37 +000066 safe_exec("f(False=True)")
67 self.assertWarning(None, w, expected)
Nick Coghlan48361f52008-08-11 15:45:58 +000068 w.reset()
Benjamin Petersond5efd202008-06-08 22:52:37 +000069 safe_exec("f(True=1)")
70 self.assertWarning(None, w, expected)
71
72
Steven Bethardae42f332008-03-18 17:26:10 +000073 def test_type_inequality_comparisons(self):
Georg Brandld5b635f2008-03-25 08:29:14 +000074 expected = 'type inequality comparisons not supported in 3.x'
Steven Bethardae42f332008-03-18 17:26:10 +000075 with catch_warning() as w:
76 self.assertWarning(int < str, w, expected)
Nick Coghlan48361f52008-08-11 15:45:58 +000077 w.reset()
Steven Bethardae42f332008-03-18 17:26:10 +000078 self.assertWarning(type < object, w, expected)
79
80 def test_object_inequality_comparisons(self):
Georg Brandld5b635f2008-03-25 08:29:14 +000081 expected = 'comparing unequal types not supported in 3.x'
Steven Bethardae42f332008-03-18 17:26:10 +000082 with catch_warning() as w:
83 self.assertWarning(str < [], w, expected)
Nick Coghlan48361f52008-08-11 15:45:58 +000084 w.reset()
Steven Bethardae42f332008-03-18 17:26:10 +000085 self.assertWarning(object() < (1, 2), w, expected)
86
87 def test_dict_inequality_comparisons(self):
Georg Brandld5b635f2008-03-25 08:29:14 +000088 expected = 'dict inequality comparisons not supported in 3.x'
Steven Bethardae42f332008-03-18 17:26:10 +000089 with catch_warning() as w:
90 self.assertWarning({} < {2:3}, w, expected)
Nick Coghlan48361f52008-08-11 15:45:58 +000091 w.reset()
Steven Bethardae42f332008-03-18 17:26:10 +000092 self.assertWarning({} <= {}, w, expected)
Nick Coghlan48361f52008-08-11 15:45:58 +000093 w.reset()
Steven Bethardae42f332008-03-18 17:26:10 +000094 self.assertWarning({} > {2:3}, w, expected)
Nick Coghlan48361f52008-08-11 15:45:58 +000095 w.reset()
Steven Bethardae42f332008-03-18 17:26:10 +000096 self.assertWarning({2:3} >= {}, w, expected)
97
98 def test_cell_inequality_comparisons(self):
Georg Brandld5b635f2008-03-25 08:29:14 +000099 expected = 'cell comparisons not supported in 3.x'
Steven Bethardae42f332008-03-18 17:26:10 +0000100 def f(x):
101 def g():
102 return x
103 return g
104 cell0, = f(0).func_closure
105 cell1, = f(1).func_closure
106 with catch_warning() as w:
107 self.assertWarning(cell0 == cell1, w, expected)
Nick Coghlan48361f52008-08-11 15:45:58 +0000108 w.reset()
Steven Bethardae42f332008-03-18 17:26:10 +0000109 self.assertWarning(cell0 < cell1, w, expected)
110
Steven Bethard6a644f92008-03-18 22:08:20 +0000111 def test_code_inequality_comparisons(self):
Georg Brandld5b635f2008-03-25 08:29:14 +0000112 expected = 'code inequality comparisons not supported in 3.x'
Steven Bethard6a644f92008-03-18 22:08:20 +0000113 def f(x):
114 pass
115 def g(x):
116 pass
117 with catch_warning() as w:
118 self.assertWarning(f.func_code < g.func_code, w, expected)
Nick Coghlan48361f52008-08-11 15:45:58 +0000119 w.reset()
Steven Bethard6a644f92008-03-18 22:08:20 +0000120 self.assertWarning(f.func_code <= g.func_code, w, expected)
Nick Coghlan48361f52008-08-11 15:45:58 +0000121 w.reset()
Steven Bethard6a644f92008-03-18 22:08:20 +0000122 self.assertWarning(f.func_code >= g.func_code, w, expected)
Nick Coghlan48361f52008-08-11 15:45:58 +0000123 w.reset()
Steven Bethard6a644f92008-03-18 22:08:20 +0000124 self.assertWarning(f.func_code > g.func_code, w, expected)
125
126 def test_builtin_function_or_method_comparisons(self):
127 expected = ('builtin_function_or_method '
Georg Brandld5b635f2008-03-25 08:29:14 +0000128 'inequality comparisons not supported in 3.x')
Steven Bethard6a644f92008-03-18 22:08:20 +0000129 func = eval
130 meth = {}.get
131 with catch_warning() as w:
132 self.assertWarning(func < meth, w, expected)
Nick Coghlan48361f52008-08-11 15:45:58 +0000133 w.reset()
Steven Bethard6a644f92008-03-18 22:08:20 +0000134 self.assertWarning(func > meth, w, expected)
Nick Coghlan48361f52008-08-11 15:45:58 +0000135 w.reset()
Steven Bethard6a644f92008-03-18 22:08:20 +0000136 self.assertWarning(meth <= func, w, expected)
Nick Coghlan48361f52008-08-11 15:45:58 +0000137 w.reset()
Steven Bethard6a644f92008-03-18 22:08:20 +0000138 self.assertWarning(meth >= func, w, expected)
139
Raymond Hettinger05387862008-03-19 17:45:19 +0000140 def test_sort_cmp_arg(self):
Georg Brandld5b635f2008-03-25 08:29:14 +0000141 expected = "the cmp argument is not supported in 3.x"
Raymond Hettinger05387862008-03-19 17:45:19 +0000142 lst = range(5)
143 cmp = lambda x,y: -1
144
145 with catch_warning() as w:
146 self.assertWarning(lst.sort(cmp=cmp), w, expected)
Nick Coghlan48361f52008-08-11 15:45:58 +0000147 w.reset()
Raymond Hettinger05387862008-03-19 17:45:19 +0000148 self.assertWarning(sorted(lst, cmp=cmp), w, expected)
Nick Coghlan48361f52008-08-11 15:45:58 +0000149 w.reset()
Raymond Hettinger05387862008-03-19 17:45:19 +0000150 self.assertWarning(lst.sort(cmp), w, expected)
Nick Coghlan48361f52008-08-11 15:45:58 +0000151 w.reset()
Raymond Hettinger05387862008-03-19 17:45:19 +0000152 self.assertWarning(sorted(lst, cmp), w, expected)
153
Georg Brandl5a444242008-03-21 20:11:46 +0000154 def test_sys_exc_clear(self):
Georg Brandld5b635f2008-03-25 08:29:14 +0000155 expected = 'sys.exc_clear() not supported in 3.x; use except clauses'
Georg Brandl5a444242008-03-21 20:11:46 +0000156 with catch_warning() as w:
157 self.assertWarning(sys.exc_clear(), w, expected)
158
Georg Brandl07e56812008-03-21 20:21:46 +0000159 def test_methods_members(self):
160 expected = '__members__ and __methods__ not supported in 3.x'
161 class C:
162 __methods__ = ['a']
163 __members__ = ['b']
164 c = C()
165 with catch_warning() as w:
166 self.assertWarning(dir(c), w, expected)
167
Georg Brandl65bb42d2008-03-21 20:38:24 +0000168 def test_softspace(self):
169 expected = 'file.softspace not supported in 3.x'
170 with file(__file__) as f:
171 with catch_warning() as w:
172 self.assertWarning(f.softspace, w, expected)
173 def set():
174 f.softspace = 0
175 with catch_warning() as w:
176 self.assertWarning(set(), w, expected)
177
Benjamin Peterson712ee922008-08-24 18:10:20 +0000178 def test_slice_methods(self):
179 class Spam(object):
180 def __getslice__(self, i, j): pass
181 def __setslice__(self, i, j, what): pass
182 def __delslice__(self, i, j): pass
183 class Egg:
184 def __getslice__(self, i, h): pass
185 def __setslice__(self, i, j, what): pass
186 def __delslice__(self, i, j): pass
187
188 expected = "in 3.x, __{0}slice__ has been removed; use __{0}item__"
189
190 for obj in (Spam(), Egg()):
191 with catch_warning() as w:
192 self.assertWarning(obj[1:2], w, expected.format('get'))
193 w.reset()
194 del obj[3:4]
195 self.assertWarning(None, w, expected.format('del'))
196 w.reset()
197 obj[4:5] = "eggs"
198 self.assertWarning(None, w, expected.format('set'))
199
Benjamin Petersonf4fcdb62008-06-08 23:00:00 +0000200 def test_tuple_parameter_unpacking(self):
201 expected = "tuple parameter unpacking has been removed in 3.x"
202 with catch_warning() as w:
203 exec "def f((a, b)): pass"
204 self.assertWarning(None, w, expected)
205
Georg Brandl80055f62008-03-25 07:56:27 +0000206 def test_buffer(self):
Nick Coghlan48361f52008-08-11 15:45:58 +0000207 expected = 'buffer() not supported in 3.x'
Georg Brandl80055f62008-03-25 07:56:27 +0000208 with catch_warning() as w:
209 self.assertWarning(buffer('a'), w, expected)
210
Georg Brandla9916b52008-05-17 22:11:54 +0000211 def test_file_xreadlines(self):
212 expected = ("f.xreadlines() not supported in 3.x, "
213 "try 'for line in f' instead")
214 with file(__file__) as f:
215 with catch_warning() as w:
216 self.assertWarning(f.xreadlines(), w, expected)
217
Nick Coghlan48361f52008-08-11 15:45:58 +0000218 def test_hash_inheritance(self):
219 with catch_warning() as w:
220 # With object as the base class
221 class WarnOnlyCmp(object):
222 def __cmp__(self, other): pass
Brett Cannona0b74442008-09-03 22:45:11 +0000223 self.assertEqual(len(w), 1)
Nick Coghlan48361f52008-08-11 15:45:58 +0000224 self.assertWarning(None, w,
225 "Overriding __cmp__ blocks inheritance of __hash__ in 3.x")
226 w.reset()
227 class WarnOnlyEq(object):
228 def __eq__(self, other): pass
Brett Cannona0b74442008-09-03 22:45:11 +0000229 self.assertEqual(len(w), 1)
Nick Coghlan48361f52008-08-11 15:45:58 +0000230 self.assertWarning(None, w,
231 "Overriding __eq__ blocks inheritance of __hash__ in 3.x")
232 w.reset()
233 class WarnCmpAndEq(object):
234 def __cmp__(self, other): pass
235 def __eq__(self, other): pass
Brett Cannona0b74442008-09-03 22:45:11 +0000236 self.assertEqual(len(w), 2)
237 self.assertWarning(None, w[-2],
Nick Coghlan48361f52008-08-11 15:45:58 +0000238 "Overriding __cmp__ blocks inheritance of __hash__ in 3.x")
239 self.assertWarning(None, w,
240 "Overriding __eq__ blocks inheritance of __hash__ in 3.x")
241 w.reset()
242 class NoWarningOnlyHash(object):
243 def __hash__(self): pass
Brett Cannona0b74442008-09-03 22:45:11 +0000244 self.assertEqual(len(w), 0)
Nick Coghlan48361f52008-08-11 15:45:58 +0000245 # With an intermediate class in the heirarchy
246 class DefinesAllThree(object):
247 def __cmp__(self, other): pass
248 def __eq__(self, other): pass
249 def __hash__(self): pass
250 class WarnOnlyCmp(DefinesAllThree):
251 def __cmp__(self, other): pass
Brett Cannona0b74442008-09-03 22:45:11 +0000252 self.assertEqual(len(w), 1)
Nick Coghlan48361f52008-08-11 15:45:58 +0000253 self.assertWarning(None, w,
254 "Overriding __cmp__ blocks inheritance of __hash__ in 3.x")
255 w.reset()
256 class WarnOnlyEq(DefinesAllThree):
257 def __eq__(self, other): pass
Brett Cannona0b74442008-09-03 22:45:11 +0000258 self.assertEqual(len(w), 1)
Nick Coghlan48361f52008-08-11 15:45:58 +0000259 self.assertWarning(None, w,
260 "Overriding __eq__ blocks inheritance of __hash__ in 3.x")
261 w.reset()
262 class WarnCmpAndEq(DefinesAllThree):
263 def __cmp__(self, other): pass
264 def __eq__(self, other): pass
Brett Cannona0b74442008-09-03 22:45:11 +0000265 self.assertEqual(len(w), 2)
266 self.assertWarning(None, w[-2],
Nick Coghlan48361f52008-08-11 15:45:58 +0000267 "Overriding __cmp__ blocks inheritance of __hash__ in 3.x")
268 self.assertWarning(None, w,
269 "Overriding __eq__ blocks inheritance of __hash__ in 3.x")
270 w.reset()
271 class NoWarningOnlyHash(DefinesAllThree):
272 def __hash__(self): pass
Brett Cannona0b74442008-09-03 22:45:11 +0000273 self.assertEqual(len(w), 0)
Nick Coghlan48361f52008-08-11 15:45:58 +0000274
Georg Brandl07e56812008-03-21 20:21:46 +0000275
Brett Cannone5d2cba2008-05-06 23:23:34 +0000276class TestStdlibRemovals(unittest.TestCase):
277
Brett Cannon3c759142008-05-09 05:25:37 +0000278 # test.testall not tested as it executes all unit tests as an
279 # import side-effect.
Brett Cannon4c1f8812008-05-10 02:27:04 +0000280 all_platforms = ('audiodev', 'imputil', 'mutex', 'user', 'new', 'rexec',
Brett Cannon1e8fba72008-07-18 19:30:22 +0000281 'Bastion', 'compiler', 'dircache', 'mimetools',
282 'fpformat', 'ihooks', 'mhlib', 'statvfs', 'htmllib',
283 'sgmllib', 'rfc822', 'sunaudio')
Brett Cannon54c77aa2008-05-14 21:08:41 +0000284 inclusive_platforms = {'irix' : ('pure', 'AL', 'al', 'CD', 'cd', 'cddb',
Brett Cannon044616a2008-05-15 02:33:55 +0000285 'cdplayer', 'CL', 'cl', 'DEVICE', 'GL',
Brett Cannon75ba4652008-05-15 03:23:17 +0000286 'gl', 'ERRNO', 'FILE', 'FL', 'flp', 'fl',
Brett Cannond8c41ec2008-05-15 03:41:55 +0000287 'fm', 'GET', 'GLWS', 'imgfile', 'IN',
Brett Cannoncd2de082008-05-15 03:51:21 +0000288 'IOCTL', 'jpeg', 'panel', 'panelparser',
Brett Cannon74a596c2008-05-15 04:17:35 +0000289 'readcd', 'SV', 'torgb', 'WAIT'),
Benjamin Peterson23681932008-05-12 21:42:13 +0000290 'darwin' : ('autoGIL', 'Carbon', 'OSATerminology',
Brett Cannonea785fb2008-05-14 01:09:40 +0000291 'icglue', 'Nav', 'MacOS', 'aepack',
292 'aetools', 'aetypes', 'applesingle',
293 'appletrawmain', 'appletrunner',
294 'argvemulator', 'bgenlocations',
Benjamin Peterson23681932008-05-12 21:42:13 +0000295 'EasyDialogs', 'macerrors', 'macostools',
296 'findertools', 'FrameWork', 'ic',
297 'gensuitemodule', 'icopen', 'macresource',
298 'MiniAEFrame', 'pimp', 'PixMapWrapper',
Brett Cannonea785fb2008-05-14 01:09:40 +0000299 'terminalcommand', 'videoreader',
300 '_builtinSuites', 'CodeWarrior',
301 'Explorer', 'Finder', 'Netscape',
302 'StdSuites', 'SystemEvents', 'Terminal',
303 'cfmfile', 'bundlebuilder', 'buildtools',
Benjamin Petersona6864e02008-07-14 17:42:17 +0000304 'ColorPicker', 'Audio_mac'),
Brett Cannon22248172008-05-16 00:10:24 +0000305 'sunos5' : ('sunaudiodev', 'SUNAUDIODEV'),
306 }
Brett Cannonac861b52008-05-12 03:45:59 +0000307 optional_modules = ('bsddb185', 'Canvas', 'dl', 'linuxaudiodev', 'imageop',
Brett Cannon32476fc2008-09-05 18:33:51 +0000308 'sv', 'cPickle', 'bsddb', 'dbhash')
Brett Cannone5d2cba2008-05-06 23:23:34 +0000309
Brett Cannon9ac39742008-05-09 22:51:58 +0000310 def check_removal(self, module_name, optional=False):
Brett Cannone5d2cba2008-05-06 23:23:34 +0000311 """Make sure the specified module, when imported, raises a
312 DeprecationWarning and specifies itself in the message."""
Benjamin Petersona6864e02008-07-14 17:42:17 +0000313 with nested(CleanImport(module_name), catch_warning(record=False)):
314 warnings.filterwarnings("error", ".+ removed",
315 DeprecationWarning, __name__)
316 try:
317 __import__(module_name, level=0)
318 except DeprecationWarning as exc:
319 self.assert_(module_name in exc.args[0],
320 "%s warning didn't contain module name"
321 % module_name)
322 except ImportError:
323 if not optional:
324 self.fail("Non-optional module {0} raised an "
325 "ImportError.".format(module_name))
326 else:
327 self.fail("DeprecationWarning not raised for {0}"
328 .format(module_name))
Brett Cannone5d2cba2008-05-06 23:23:34 +0000329
330 def test_platform_independent_removals(self):
331 # Make sure that the modules that are available on all platforms raise
332 # the proper DeprecationWarning.
333 for module_name in self.all_platforms:
334 self.check_removal(module_name)
335
Brett Cannon9ac39742008-05-09 22:51:58 +0000336 def test_platform_specific_removals(self):
337 # Test the removal of platform-specific modules.
338 for module_name in self.inclusive_platforms.get(sys.platform, []):
339 self.check_removal(module_name, optional=True)
340
Brett Cannon768d44f2008-05-10 02:47:54 +0000341 def test_optional_module_removals(self):
342 # Test the removal of modules that may or may not be built.
343 for module_name in self.optional_modules:
344 self.check_removal(module_name, optional=True)
345
Benjamin Peterson0893a0a2008-05-09 00:27:01 +0000346 def test_os_path_walk(self):
347 msg = "In 3.x, os.path.walk is removed in favor of os.walk."
348 def dumbo(where, names, args): pass
349 for path_mod in ("ntpath", "macpath", "os2emxpath", "posixpath"):
350 mod = __import__(path_mod)
351 with catch_warning() as w:
Benjamin Peterson1d310232008-05-27 01:42:29 +0000352 mod.walk("crashers", dumbo, None)
Benjamin Peterson0893a0a2008-05-09 00:27:01 +0000353 self.assertEquals(str(w.message), msg)
354
Benjamin Peterson3aa84a72008-05-26 19:41:53 +0000355 def test_commands_members(self):
356 import commands
357 members = {"mk2arg" : 2, "mkarg" : 1, "getstatus" : 1}
358 for name, arg_count in members.items():
359 with catch_warning(record=False):
360 warnings.filterwarnings("error")
361 func = getattr(commands, name)
362 self.assertRaises(DeprecationWarning, func, *([None]*arg_count))
363
Benjamin Peterson541f7da2008-08-18 02:12:23 +0000364 def test_reduce_move(self):
365 from operator import add
366 with catch_warning(record=False):
367 warnings.filterwarnings("error", "reduce")
368 self.assertRaises(DeprecationWarning, reduce, add, range(10))
369
Brett Cannonabb34fe2008-05-29 05:08:50 +0000370 def test_mutablestring_removal(self):
371 # UserString.MutableString has been removed in 3.0.
372 import UserString
373 with catch_warning(record=False):
374 warnings.filterwarnings("error", ".*MutableString",
375 DeprecationWarning)
376 self.assertRaises(DeprecationWarning, UserString.MutableString)
377
Brett Cannone5d2cba2008-05-06 23:23:34 +0000378
Steven Bethardae42f332008-03-18 17:26:10 +0000379def test_main():
Nick Coghlan38469e22008-07-13 12:23:47 +0000380 with catch_warning():
Benjamin Peterson1d310232008-05-27 01:42:29 +0000381 warnings.simplefilter("always")
382 run_unittest(TestPy3KWarnings,
383 TestStdlibRemovals)
Steven Bethardae42f332008-03-18 17:26:10 +0000384
385if __name__ == '__main__':
386 test_main()