blob: 67b2538f01a502d50b2f9bc78a6ae2da3cfd27a7 [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
Benjamin Peterson2fe3ef82008-06-08 02:05:33 +000015 def test_backquote(self):
16 expected = 'backquote not supported in 3.x; use repr()'
17 with catch_warning() as w:
18 exec "`2`" in {}
19 self.assertWarning(None, w, expected)
20
Benjamin Petersond5efd202008-06-08 22:52:37 +000021 def test_bool_assign(self):
22 # So we don't screw up our globals
23 def safe_exec(expr):
24 def f(**kwargs): pass
25 exec expr in {'f' : f}
26
27 expected = "assignment to True or False is forbidden in 3.x"
28 with catch_warning() as w:
29 safe_exec("True = False")
30 self.assertWarning(None, w, expected)
Benjamin Petersond5efd202008-06-08 22:52:37 +000031 safe_exec("False = True")
32 self.assertWarning(None, w, expected)
Benjamin Petersond5efd202008-06-08 22:52:37 +000033 try:
34 safe_exec("obj.False = True")
35 except NameError: pass
36 self.assertWarning(None, w, expected)
Benjamin Petersond5efd202008-06-08 22:52:37 +000037 try:
38 safe_exec("obj.True = False")
39 except NameError: pass
40 self.assertWarning(None, w, expected)
Benjamin Petersond5efd202008-06-08 22:52:37 +000041 safe_exec("def False(): pass")
42 self.assertWarning(None, w, expected)
Benjamin Petersond5efd202008-06-08 22:52:37 +000043 safe_exec("def True(): pass")
44 self.assertWarning(None, w, expected)
Benjamin Petersond5efd202008-06-08 22:52:37 +000045 safe_exec("class False: pass")
46 self.assertWarning(None, w, expected)
Benjamin Petersond5efd202008-06-08 22:52:37 +000047 safe_exec("class True: pass")
48 self.assertWarning(None, w, expected)
Benjamin Petersond5efd202008-06-08 22:52:37 +000049 safe_exec("def f(True=43): pass")
50 self.assertWarning(None, w, expected)
Benjamin Petersond5efd202008-06-08 22:52:37 +000051 safe_exec("def f(False=None): pass")
52 self.assertWarning(None, w, expected)
Benjamin Petersond5efd202008-06-08 22:52:37 +000053 safe_exec("f(False=True)")
54 self.assertWarning(None, w, expected)
Benjamin Petersond5efd202008-06-08 22:52:37 +000055 safe_exec("f(True=1)")
56 self.assertWarning(None, w, expected)
57
58
Steven Bethardae42f332008-03-18 17:26:10 +000059 def test_type_inequality_comparisons(self):
Georg Brandld5b635f2008-03-25 08:29:14 +000060 expected = 'type inequality comparisons not supported in 3.x'
Steven Bethardae42f332008-03-18 17:26:10 +000061 with catch_warning() as w:
62 self.assertWarning(int < str, w, expected)
Steven Bethardae42f332008-03-18 17:26:10 +000063 self.assertWarning(type < object, w, expected)
64
65 def test_object_inequality_comparisons(self):
Georg Brandld5b635f2008-03-25 08:29:14 +000066 expected = 'comparing unequal types not supported in 3.x'
Steven Bethardae42f332008-03-18 17:26:10 +000067 with catch_warning() as w:
68 self.assertWarning(str < [], w, expected)
Steven Bethardae42f332008-03-18 17:26:10 +000069 self.assertWarning(object() < (1, 2), w, expected)
70
71 def test_dict_inequality_comparisons(self):
Georg Brandld5b635f2008-03-25 08:29:14 +000072 expected = 'dict inequality comparisons not supported in 3.x'
Steven Bethardae42f332008-03-18 17:26:10 +000073 with catch_warning() as w:
74 self.assertWarning({} < {2:3}, w, expected)
Steven Bethardae42f332008-03-18 17:26:10 +000075 self.assertWarning({} <= {}, w, expected)
Steven Bethardae42f332008-03-18 17:26:10 +000076 self.assertWarning({} > {2:3}, w, expected)
Steven Bethardae42f332008-03-18 17:26:10 +000077 self.assertWarning({2:3} >= {}, w, expected)
78
79 def test_cell_inequality_comparisons(self):
Georg Brandld5b635f2008-03-25 08:29:14 +000080 expected = 'cell comparisons not supported in 3.x'
Steven Bethardae42f332008-03-18 17:26:10 +000081 def f(x):
82 def g():
83 return x
84 return g
85 cell0, = f(0).func_closure
86 cell1, = f(1).func_closure
87 with catch_warning() as w:
88 self.assertWarning(cell0 == cell1, w, expected)
Steven Bethardae42f332008-03-18 17:26:10 +000089 self.assertWarning(cell0 < cell1, w, expected)
90
Steven Bethard6a644f92008-03-18 22:08:20 +000091 def test_code_inequality_comparisons(self):
Georg Brandld5b635f2008-03-25 08:29:14 +000092 expected = 'code inequality comparisons not supported in 3.x'
Steven Bethard6a644f92008-03-18 22:08:20 +000093 def f(x):
94 pass
95 def g(x):
96 pass
97 with catch_warning() as w:
98 self.assertWarning(f.func_code < g.func_code, w, expected)
Steven Bethard6a644f92008-03-18 22:08:20 +000099 self.assertWarning(f.func_code <= g.func_code, w, expected)
Steven Bethard6a644f92008-03-18 22:08:20 +0000100 self.assertWarning(f.func_code >= g.func_code, w, expected)
Steven Bethard6a644f92008-03-18 22:08:20 +0000101 self.assertWarning(f.func_code > g.func_code, w, expected)
102
103 def test_builtin_function_or_method_comparisons(self):
104 expected = ('builtin_function_or_method '
Georg Brandld5b635f2008-03-25 08:29:14 +0000105 'inequality comparisons not supported in 3.x')
Steven Bethard6a644f92008-03-18 22:08:20 +0000106 func = eval
107 meth = {}.get
108 with catch_warning() as w:
109 self.assertWarning(func < meth, w, expected)
Steven Bethard6a644f92008-03-18 22:08:20 +0000110 self.assertWarning(func > meth, w, expected)
Steven Bethard6a644f92008-03-18 22:08:20 +0000111 self.assertWarning(meth <= func, w, expected)
Steven Bethard6a644f92008-03-18 22:08:20 +0000112 self.assertWarning(meth >= func, w, expected)
113
Steven Bethardae42f332008-03-18 17:26:10 +0000114 def assertWarning(self, _, warning, expected_message):
115 self.assertEqual(str(warning.message), expected_message)
116
Raymond Hettinger05387862008-03-19 17:45:19 +0000117 def test_sort_cmp_arg(self):
Georg Brandld5b635f2008-03-25 08:29:14 +0000118 expected = "the cmp argument is not supported in 3.x"
Raymond Hettinger05387862008-03-19 17:45:19 +0000119 lst = range(5)
120 cmp = lambda x,y: -1
121
122 with catch_warning() as w:
123 self.assertWarning(lst.sort(cmp=cmp), w, expected)
Raymond Hettinger05387862008-03-19 17:45:19 +0000124 self.assertWarning(sorted(lst, cmp=cmp), w, expected)
Raymond Hettinger05387862008-03-19 17:45:19 +0000125 self.assertWarning(lst.sort(cmp), w, expected)
Raymond Hettinger05387862008-03-19 17:45:19 +0000126 self.assertWarning(sorted(lst, cmp), w, expected)
127
Georg Brandl5a444242008-03-21 20:11:46 +0000128 def test_sys_exc_clear(self):
Georg Brandld5b635f2008-03-25 08:29:14 +0000129 expected = 'sys.exc_clear() not supported in 3.x; use except clauses'
Georg Brandl5a444242008-03-21 20:11:46 +0000130 with catch_warning() as w:
131 self.assertWarning(sys.exc_clear(), w, expected)
132
Georg Brandl07e56812008-03-21 20:21:46 +0000133 def test_methods_members(self):
134 expected = '__members__ and __methods__ not supported in 3.x'
135 class C:
136 __methods__ = ['a']
137 __members__ = ['b']
138 c = C()
139 with catch_warning() as w:
140 self.assertWarning(dir(c), w, expected)
141
Georg Brandl65bb42d2008-03-21 20:38:24 +0000142 def test_softspace(self):
143 expected = 'file.softspace not supported in 3.x'
144 with file(__file__) as f:
145 with catch_warning() as w:
146 self.assertWarning(f.softspace, w, expected)
147 def set():
148 f.softspace = 0
149 with catch_warning() as w:
150 self.assertWarning(set(), w, expected)
151
Benjamin Petersonf4fcdb62008-06-08 23:00:00 +0000152 def test_tuple_parameter_unpacking(self):
153 expected = "tuple parameter unpacking has been removed in 3.x"
154 with catch_warning() as w:
155 exec "def f((a, b)): pass"
156 self.assertWarning(None, w, expected)
157
Georg Brandl80055f62008-03-25 07:56:27 +0000158 def test_buffer(self):
Georg Brandld5b635f2008-03-25 08:29:14 +0000159 expected = 'buffer() not supported in 3.x; use memoryview()'
Georg Brandl80055f62008-03-25 07:56:27 +0000160 with catch_warning() as w:
161 self.assertWarning(buffer('a'), w, expected)
162
Georg Brandla9916b52008-05-17 22:11:54 +0000163 def test_file_xreadlines(self):
164 expected = ("f.xreadlines() not supported in 3.x, "
165 "try 'for line in f' instead")
166 with file(__file__) as f:
167 with catch_warning() as w:
168 self.assertWarning(f.xreadlines(), w, expected)
169
Georg Brandl07e56812008-03-21 20:21:46 +0000170
Brett Cannone5d2cba2008-05-06 23:23:34 +0000171class TestStdlibRemovals(unittest.TestCase):
172
Brett Cannon3c759142008-05-09 05:25:37 +0000173 # test.testall not tested as it executes all unit tests as an
174 # import side-effect.
Brett Cannon4c1f8812008-05-10 02:27:04 +0000175 all_platforms = ('audiodev', 'imputil', 'mutex', 'user', 'new', 'rexec',
Brett Cannon1e8fba72008-07-18 19:30:22 +0000176 'Bastion', 'compiler', 'dircache', 'mimetools',
177 'fpformat', 'ihooks', 'mhlib', 'statvfs', 'htmllib',
178 'sgmllib', 'rfc822', 'sunaudio')
Brett Cannon54c77aa2008-05-14 21:08:41 +0000179 inclusive_platforms = {'irix' : ('pure', 'AL', 'al', 'CD', 'cd', 'cddb',
Brett Cannon044616a2008-05-15 02:33:55 +0000180 'cdplayer', 'CL', 'cl', 'DEVICE', 'GL',
Brett Cannon75ba4652008-05-15 03:23:17 +0000181 'gl', 'ERRNO', 'FILE', 'FL', 'flp', 'fl',
Brett Cannond8c41ec2008-05-15 03:41:55 +0000182 'fm', 'GET', 'GLWS', 'imgfile', 'IN',
Brett Cannoncd2de082008-05-15 03:51:21 +0000183 'IOCTL', 'jpeg', 'panel', 'panelparser',
Brett Cannon74a596c2008-05-15 04:17:35 +0000184 'readcd', 'SV', 'torgb', 'WAIT'),
Benjamin Peterson23681932008-05-12 21:42:13 +0000185 'darwin' : ('autoGIL', 'Carbon', 'OSATerminology',
Brett Cannonea785fb2008-05-14 01:09:40 +0000186 'icglue', 'Nav', 'MacOS', 'aepack',
187 'aetools', 'aetypes', 'applesingle',
188 'appletrawmain', 'appletrunner',
189 'argvemulator', 'bgenlocations',
Benjamin Peterson23681932008-05-12 21:42:13 +0000190 'EasyDialogs', 'macerrors', 'macostools',
191 'findertools', 'FrameWork', 'ic',
192 'gensuitemodule', 'icopen', 'macresource',
193 'MiniAEFrame', 'pimp', 'PixMapWrapper',
Brett Cannonea785fb2008-05-14 01:09:40 +0000194 'terminalcommand', 'videoreader',
195 '_builtinSuites', 'CodeWarrior',
196 'Explorer', 'Finder', 'Netscape',
197 'StdSuites', 'SystemEvents', 'Terminal',
198 'cfmfile', 'bundlebuilder', 'buildtools',
Benjamin Petersona6864e02008-07-14 17:42:17 +0000199 'ColorPicker', 'Audio_mac'),
Brett Cannon22248172008-05-16 00:10:24 +0000200 'sunos5' : ('sunaudiodev', 'SUNAUDIODEV'),
201 }
Brett Cannonac861b52008-05-12 03:45:59 +0000202 optional_modules = ('bsddb185', 'Canvas', 'dl', 'linuxaudiodev', 'imageop',
Alexandre Vassalotti3c4971c2008-05-16 19:14:31 +0000203 'sv', 'cPickle')
Brett Cannone5d2cba2008-05-06 23:23:34 +0000204
Brett Cannon9ac39742008-05-09 22:51:58 +0000205 def check_removal(self, module_name, optional=False):
Brett Cannone5d2cba2008-05-06 23:23:34 +0000206 """Make sure the specified module, when imported, raises a
207 DeprecationWarning and specifies itself in the message."""
Benjamin Petersona6864e02008-07-14 17:42:17 +0000208 with nested(CleanImport(module_name), catch_warning(record=False)):
209 warnings.filterwarnings("error", ".+ removed",
210 DeprecationWarning, __name__)
211 try:
212 __import__(module_name, level=0)
213 except DeprecationWarning as exc:
214 self.assert_(module_name in exc.args[0],
215 "%s warning didn't contain module name"
216 % module_name)
217 except ImportError:
218 if not optional:
219 self.fail("Non-optional module {0} raised an "
220 "ImportError.".format(module_name))
221 else:
222 self.fail("DeprecationWarning not raised for {0}"
223 .format(module_name))
Brett Cannone5d2cba2008-05-06 23:23:34 +0000224
225 def test_platform_independent_removals(self):
226 # Make sure that the modules that are available on all platforms raise
227 # the proper DeprecationWarning.
228 for module_name in self.all_platforms:
229 self.check_removal(module_name)
230
Brett Cannon9ac39742008-05-09 22:51:58 +0000231 def test_platform_specific_removals(self):
232 # Test the removal of platform-specific modules.
233 for module_name in self.inclusive_platforms.get(sys.platform, []):
234 self.check_removal(module_name, optional=True)
235
Brett Cannon768d44f2008-05-10 02:47:54 +0000236 def test_optional_module_removals(self):
237 # Test the removal of modules that may or may not be built.
238 for module_name in self.optional_modules:
239 self.check_removal(module_name, optional=True)
240
Benjamin Peterson0893a0a2008-05-09 00:27:01 +0000241 def test_os_path_walk(self):
242 msg = "In 3.x, os.path.walk is removed in favor of os.walk."
243 def dumbo(where, names, args): pass
244 for path_mod in ("ntpath", "macpath", "os2emxpath", "posixpath"):
245 mod = __import__(path_mod)
246 with catch_warning() as w:
Benjamin Peterson1d310232008-05-27 01:42:29 +0000247 mod.walk("crashers", dumbo, None)
Benjamin Peterson0893a0a2008-05-09 00:27:01 +0000248 self.assertEquals(str(w.message), msg)
249
Benjamin Peterson3aa84a72008-05-26 19:41:53 +0000250 def test_commands_members(self):
251 import commands
252 members = {"mk2arg" : 2, "mkarg" : 1, "getstatus" : 1}
253 for name, arg_count in members.items():
254 with catch_warning(record=False):
255 warnings.filterwarnings("error")
256 func = getattr(commands, name)
257 self.assertRaises(DeprecationWarning, func, *([None]*arg_count))
258
Brett Cannonabb34fe2008-05-29 05:08:50 +0000259 def test_mutablestring_removal(self):
260 # UserString.MutableString has been removed in 3.0.
261 import UserString
262 with catch_warning(record=False):
263 warnings.filterwarnings("error", ".*MutableString",
264 DeprecationWarning)
265 self.assertRaises(DeprecationWarning, UserString.MutableString)
266
Brett Cannone5d2cba2008-05-06 23:23:34 +0000267
Steven Bethardae42f332008-03-18 17:26:10 +0000268def test_main():
Nick Coghlan38469e22008-07-13 12:23:47 +0000269 with catch_warning():
Benjamin Peterson1d310232008-05-27 01:42:29 +0000270 warnings.simplefilter("always")
271 run_unittest(TestPy3KWarnings,
272 TestStdlibRemovals)
Steven Bethardae42f332008-03-18 17:26:10 +0000273
274if __name__ == '__main__':
275 test_main()