blob: 274e14716763c65c0353279aa5ba0e300e5bc35f [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 Petersonf4fcdb62008-06-08 23:00:00 +0000178 def test_tuple_parameter_unpacking(self):
179 expected = "tuple parameter unpacking has been removed in 3.x"
180 with catch_warning() as w:
181 exec "def f((a, b)): pass"
182 self.assertWarning(None, w, expected)
183
Georg Brandl80055f62008-03-25 07:56:27 +0000184 def test_buffer(self):
Nick Coghlan48361f52008-08-11 15:45:58 +0000185 expected = 'buffer() not supported in 3.x'
Georg Brandl80055f62008-03-25 07:56:27 +0000186 with catch_warning() as w:
187 self.assertWarning(buffer('a'), w, expected)
188
Georg Brandla9916b52008-05-17 22:11:54 +0000189 def test_file_xreadlines(self):
190 expected = ("f.xreadlines() not supported in 3.x, "
191 "try 'for line in f' instead")
192 with file(__file__) as f:
193 with catch_warning() as w:
194 self.assertWarning(f.xreadlines(), w, expected)
195
Nick Coghlan48361f52008-08-11 15:45:58 +0000196 def test_hash_inheritance(self):
197 with catch_warning() as w:
198 # With object as the base class
199 class WarnOnlyCmp(object):
200 def __cmp__(self, other): pass
201 self.assertEqual(len(w.warnings), 1)
202 self.assertWarning(None, w,
203 "Overriding __cmp__ blocks inheritance of __hash__ in 3.x")
204 w.reset()
205 class WarnOnlyEq(object):
206 def __eq__(self, other): pass
207 self.assertEqual(len(w.warnings), 1)
208 self.assertWarning(None, w,
209 "Overriding __eq__ blocks inheritance of __hash__ in 3.x")
210 w.reset()
211 class WarnCmpAndEq(object):
212 def __cmp__(self, other): pass
213 def __eq__(self, other): pass
214 self.assertEqual(len(w.warnings), 2)
215 self.assertWarning(None, w.warnings[-2],
216 "Overriding __cmp__ blocks inheritance of __hash__ in 3.x")
217 self.assertWarning(None, w,
218 "Overriding __eq__ blocks inheritance of __hash__ in 3.x")
219 w.reset()
220 class NoWarningOnlyHash(object):
221 def __hash__(self): pass
222 self.assertEqual(len(w.warnings), 0)
223 # With an intermediate class in the heirarchy
224 class DefinesAllThree(object):
225 def __cmp__(self, other): pass
226 def __eq__(self, other): pass
227 def __hash__(self): pass
228 class WarnOnlyCmp(DefinesAllThree):
229 def __cmp__(self, other): pass
230 self.assertEqual(len(w.warnings), 1)
231 self.assertWarning(None, w,
232 "Overriding __cmp__ blocks inheritance of __hash__ in 3.x")
233 w.reset()
234 class WarnOnlyEq(DefinesAllThree):
235 def __eq__(self, other): pass
236 self.assertEqual(len(w.warnings), 1)
237 self.assertWarning(None, w,
238 "Overriding __eq__ blocks inheritance of __hash__ in 3.x")
239 w.reset()
240 class WarnCmpAndEq(DefinesAllThree):
241 def __cmp__(self, other): pass
242 def __eq__(self, other): pass
243 self.assertEqual(len(w.warnings), 2)
244 self.assertWarning(None, w.warnings[-2],
245 "Overriding __cmp__ blocks inheritance of __hash__ in 3.x")
246 self.assertWarning(None, w,
247 "Overriding __eq__ blocks inheritance of __hash__ in 3.x")
248 w.reset()
249 class NoWarningOnlyHash(DefinesAllThree):
250 def __hash__(self): pass
251 self.assertEqual(len(w.warnings), 0)
252
Benjamin Peterson6ee1a312008-08-18 21:53:29 +0000253 def test_pep8ified_threading(self):
254 import threading
255
256 t = threading.Thread()
257 with catch_warning() as w:
258 msg = "isDaemon() is deprecated in favor of the " \
259 "Thread.daemon property"
260 self.assertWarning(t.isDaemon(), w, msg)
261 w.reset()
262 msg = "setDaemon() is deprecated in favor of the " \
263 "Thread.daemon property"
264 self.assertWarning(t.setDaemon(True), w, msg)
265 w.reset()
266 msg = "getName() is deprecated in favor of the " \
267 "Thread.name property"
268 self.assertWarning(t.getName(), w, msg)
269 w.reset()
270 msg = "setName() is deprecated in favor of the " \
271 "Thread.name property"
272 self.assertWarning(t.setName("name"), w, msg)
273 w.reset()
274 msg = "isAlive() is deprecated in favor of is_alive()"
275 self.assertWarning(t.isAlive(), w, msg)
276 w.reset()
277 e = threading.Event()
278 msg = "isSet() is deprecated in favor of is_set()"
279 self.assertWarning(e.isSet(), w, msg)
280 w.reset()
281 msg = "currentThread() is deprecated in favor of current_thread()"
282 self.assertWarning(threading.currentThread(), w, msg)
283 w.reset()
284 msg = "activeCount() is deprecated in favor of active_count()"
285 self.assertWarning(threading.activeCount(), w, msg)
286
Nick Coghlan48361f52008-08-11 15:45:58 +0000287
Georg Brandl07e56812008-03-21 20:21:46 +0000288
Brett Cannone5d2cba2008-05-06 23:23:34 +0000289class TestStdlibRemovals(unittest.TestCase):
290
Brett Cannon3c759142008-05-09 05:25:37 +0000291 # test.testall not tested as it executes all unit tests as an
292 # import side-effect.
Brett Cannon4c1f8812008-05-10 02:27:04 +0000293 all_platforms = ('audiodev', 'imputil', 'mutex', 'user', 'new', 'rexec',
Brett Cannon1e8fba72008-07-18 19:30:22 +0000294 'Bastion', 'compiler', 'dircache', 'mimetools',
295 'fpformat', 'ihooks', 'mhlib', 'statvfs', 'htmllib',
296 'sgmllib', 'rfc822', 'sunaudio')
Brett Cannon54c77aa2008-05-14 21:08:41 +0000297 inclusive_platforms = {'irix' : ('pure', 'AL', 'al', 'CD', 'cd', 'cddb',
Brett Cannon044616a2008-05-15 02:33:55 +0000298 'cdplayer', 'CL', 'cl', 'DEVICE', 'GL',
Brett Cannon75ba4652008-05-15 03:23:17 +0000299 'gl', 'ERRNO', 'FILE', 'FL', 'flp', 'fl',
Brett Cannond8c41ec2008-05-15 03:41:55 +0000300 'fm', 'GET', 'GLWS', 'imgfile', 'IN',
Brett Cannoncd2de082008-05-15 03:51:21 +0000301 'IOCTL', 'jpeg', 'panel', 'panelparser',
Brett Cannon74a596c2008-05-15 04:17:35 +0000302 'readcd', 'SV', 'torgb', 'WAIT'),
Benjamin Peterson23681932008-05-12 21:42:13 +0000303 'darwin' : ('autoGIL', 'Carbon', 'OSATerminology',
Brett Cannonea785fb2008-05-14 01:09:40 +0000304 'icglue', 'Nav', 'MacOS', 'aepack',
305 'aetools', 'aetypes', 'applesingle',
306 'appletrawmain', 'appletrunner',
307 'argvemulator', 'bgenlocations',
Benjamin Peterson23681932008-05-12 21:42:13 +0000308 'EasyDialogs', 'macerrors', 'macostools',
309 'findertools', 'FrameWork', 'ic',
310 'gensuitemodule', 'icopen', 'macresource',
311 'MiniAEFrame', 'pimp', 'PixMapWrapper',
Brett Cannonea785fb2008-05-14 01:09:40 +0000312 'terminalcommand', 'videoreader',
313 '_builtinSuites', 'CodeWarrior',
314 'Explorer', 'Finder', 'Netscape',
315 'StdSuites', 'SystemEvents', 'Terminal',
316 'cfmfile', 'bundlebuilder', 'buildtools',
Benjamin Petersona6864e02008-07-14 17:42:17 +0000317 'ColorPicker', 'Audio_mac'),
Brett Cannon22248172008-05-16 00:10:24 +0000318 'sunos5' : ('sunaudiodev', 'SUNAUDIODEV'),
319 }
Brett Cannonac861b52008-05-12 03:45:59 +0000320 optional_modules = ('bsddb185', 'Canvas', 'dl', 'linuxaudiodev', 'imageop',
Alexandre Vassalotti3c4971c2008-05-16 19:14:31 +0000321 'sv', 'cPickle')
Brett Cannone5d2cba2008-05-06 23:23:34 +0000322
Brett Cannon9ac39742008-05-09 22:51:58 +0000323 def check_removal(self, module_name, optional=False):
Brett Cannone5d2cba2008-05-06 23:23:34 +0000324 """Make sure the specified module, when imported, raises a
325 DeprecationWarning and specifies itself in the message."""
Benjamin Petersona6864e02008-07-14 17:42:17 +0000326 with nested(CleanImport(module_name), catch_warning(record=False)):
327 warnings.filterwarnings("error", ".+ removed",
328 DeprecationWarning, __name__)
329 try:
330 __import__(module_name, level=0)
331 except DeprecationWarning as exc:
332 self.assert_(module_name in exc.args[0],
333 "%s warning didn't contain module name"
334 % module_name)
335 except ImportError:
336 if not optional:
337 self.fail("Non-optional module {0} raised an "
338 "ImportError.".format(module_name))
339 else:
340 self.fail("DeprecationWarning not raised for {0}"
341 .format(module_name))
Brett Cannone5d2cba2008-05-06 23:23:34 +0000342
343 def test_platform_independent_removals(self):
344 # Make sure that the modules that are available on all platforms raise
345 # the proper DeprecationWarning.
346 for module_name in self.all_platforms:
347 self.check_removal(module_name)
348
Brett Cannon9ac39742008-05-09 22:51:58 +0000349 def test_platform_specific_removals(self):
350 # Test the removal of platform-specific modules.
351 for module_name in self.inclusive_platforms.get(sys.platform, []):
352 self.check_removal(module_name, optional=True)
353
Brett Cannon768d44f2008-05-10 02:47:54 +0000354 def test_optional_module_removals(self):
355 # Test the removal of modules that may or may not be built.
356 for module_name in self.optional_modules:
357 self.check_removal(module_name, optional=True)
358
Benjamin Peterson0893a0a2008-05-09 00:27:01 +0000359 def test_os_path_walk(self):
360 msg = "In 3.x, os.path.walk is removed in favor of os.walk."
361 def dumbo(where, names, args): pass
362 for path_mod in ("ntpath", "macpath", "os2emxpath", "posixpath"):
363 mod = __import__(path_mod)
364 with catch_warning() as w:
Benjamin Peterson1d310232008-05-27 01:42:29 +0000365 mod.walk("crashers", dumbo, None)
Benjamin Peterson0893a0a2008-05-09 00:27:01 +0000366 self.assertEquals(str(w.message), msg)
367
Benjamin Peterson3aa84a72008-05-26 19:41:53 +0000368 def test_commands_members(self):
369 import commands
370 members = {"mk2arg" : 2, "mkarg" : 1, "getstatus" : 1}
371 for name, arg_count in members.items():
372 with catch_warning(record=False):
373 warnings.filterwarnings("error")
374 func = getattr(commands, name)
375 self.assertRaises(DeprecationWarning, func, *([None]*arg_count))
376
Benjamin Peterson541f7da2008-08-18 02:12:23 +0000377 def test_reduce_move(self):
378 from operator import add
379 with catch_warning(record=False):
380 warnings.filterwarnings("error", "reduce")
381 self.assertRaises(DeprecationWarning, reduce, add, range(10))
382
Brett Cannonabb34fe2008-05-29 05:08:50 +0000383 def test_mutablestring_removal(self):
384 # UserString.MutableString has been removed in 3.0.
385 import UserString
386 with catch_warning(record=False):
387 warnings.filterwarnings("error", ".*MutableString",
388 DeprecationWarning)
389 self.assertRaises(DeprecationWarning, UserString.MutableString)
390
Brett Cannone5d2cba2008-05-06 23:23:34 +0000391
Steven Bethardae42f332008-03-18 17:26:10 +0000392def test_main():
Nick Coghlan38469e22008-07-13 12:23:47 +0000393 with catch_warning():
Benjamin Peterson1d310232008-05-27 01:42:29 +0000394 warnings.simplefilter("always")
395 run_unittest(TestPy3KWarnings,
396 TestStdlibRemovals)
Steven Bethardae42f332008-03-18 17:26:10 +0000397
398if __name__ == '__main__':
399 test_main()