blob: 768be285f9707a0f13f62e0be6ed30f0bd011cb5 [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
Brett Cannon977eb022008-03-19 17:37:43 +00007if not sys.py3kwarning:
8 raise TestSkipped('%s must be run with the -3 flag' % __name__)
9
Steven Bethardae42f332008-03-18 17:26:10 +000010
11class TestPy3KWarnings(unittest.TestCase):
12
Benjamin Peterson2fe3ef82008-06-08 02:05:33 +000013 def test_backquote(self):
14 expected = 'backquote not supported in 3.x; use repr()'
15 with catch_warning() as w:
16 exec "`2`" in {}
17 self.assertWarning(None, w, expected)
18
Benjamin Petersond5efd202008-06-08 22:52:37 +000019 def test_bool_assign(self):
20 # So we don't screw up our globals
21 def safe_exec(expr):
22 def f(**kwargs): pass
23 exec expr in {'f' : f}
24
25 expected = "assignment to True or False is forbidden in 3.x"
26 with catch_warning() as w:
27 safe_exec("True = False")
28 self.assertWarning(None, w, expected)
29 with catch_warning() as w:
30 safe_exec("False = True")
31 self.assertWarning(None, w, expected)
32 with catch_warning() as w:
33 try:
34 safe_exec("obj.False = True")
35 except NameError: pass
36 self.assertWarning(None, w, expected)
37 with catch_warning() as w:
38 try:
39 safe_exec("obj.True = False")
40 except NameError: pass
41 self.assertWarning(None, w, expected)
42 with catch_warning() as w:
43 safe_exec("def False(): pass")
44 self.assertWarning(None, w, expected)
45 with catch_warning() as w:
46 safe_exec("def True(): pass")
47 self.assertWarning(None, w, expected)
48 with catch_warning() as w:
49 safe_exec("class False: pass")
50 self.assertWarning(None, w, expected)
51 with catch_warning() as w:
52 safe_exec("class True: pass")
53 self.assertWarning(None, w, expected)
54 with catch_warning() as w:
55 safe_exec("def f(True=43): pass")
56 self.assertWarning(None, w, expected)
57 with catch_warning() as w:
58 safe_exec("def f(False=None): pass")
59 self.assertWarning(None, w, expected)
60 with catch_warning() as w:
61 safe_exec("f(False=True)")
62 self.assertWarning(None, w, expected)
63 with catch_warning() as w:
64 safe_exec("f(True=1)")
65 self.assertWarning(None, w, expected)
66
67
Steven Bethardae42f332008-03-18 17:26:10 +000068 def test_type_inequality_comparisons(self):
Georg Brandld5b635f2008-03-25 08:29:14 +000069 expected = 'type inequality comparisons not supported in 3.x'
Steven Bethardae42f332008-03-18 17:26:10 +000070 with catch_warning() as w:
71 self.assertWarning(int < str, w, expected)
72 with catch_warning() as w:
73 self.assertWarning(type < object, w, expected)
74
75 def test_object_inequality_comparisons(self):
Georg Brandld5b635f2008-03-25 08:29:14 +000076 expected = 'comparing unequal types not supported in 3.x'
Steven Bethardae42f332008-03-18 17:26:10 +000077 with catch_warning() as w:
78 self.assertWarning(str < [], w, expected)
79 with catch_warning() as w:
80 self.assertWarning(object() < (1, 2), w, expected)
81
82 def test_dict_inequality_comparisons(self):
Georg Brandld5b635f2008-03-25 08:29:14 +000083 expected = 'dict inequality comparisons not supported in 3.x'
Steven Bethardae42f332008-03-18 17:26:10 +000084 with catch_warning() as w:
85 self.assertWarning({} < {2:3}, w, expected)
86 with catch_warning() as w:
87 self.assertWarning({} <= {}, w, expected)
88 with catch_warning() as w:
89 self.assertWarning({} > {2:3}, w, expected)
90 with catch_warning() as w:
91 self.assertWarning({2:3} >= {}, w, expected)
92
93 def test_cell_inequality_comparisons(self):
Georg Brandld5b635f2008-03-25 08:29:14 +000094 expected = 'cell comparisons not supported in 3.x'
Steven Bethardae42f332008-03-18 17:26:10 +000095 def f(x):
96 def g():
97 return x
98 return g
99 cell0, = f(0).func_closure
100 cell1, = f(1).func_closure
101 with catch_warning() as w:
102 self.assertWarning(cell0 == cell1, w, expected)
103 with catch_warning() as w:
104 self.assertWarning(cell0 < cell1, w, expected)
105
Steven Bethard6a644f92008-03-18 22:08:20 +0000106 def test_code_inequality_comparisons(self):
Georg Brandld5b635f2008-03-25 08:29:14 +0000107 expected = 'code inequality comparisons not supported in 3.x'
Steven Bethard6a644f92008-03-18 22:08:20 +0000108 def f(x):
109 pass
110 def g(x):
111 pass
112 with catch_warning() as w:
113 self.assertWarning(f.func_code < g.func_code, w, expected)
114 with catch_warning() as w:
115 self.assertWarning(f.func_code <= g.func_code, w, expected)
116 with catch_warning() as w:
117 self.assertWarning(f.func_code >= g.func_code, w, expected)
118 with catch_warning() as w:
119 self.assertWarning(f.func_code > g.func_code, w, expected)
120
121 def test_builtin_function_or_method_comparisons(self):
122 expected = ('builtin_function_or_method '
Georg Brandld5b635f2008-03-25 08:29:14 +0000123 'inequality comparisons not supported in 3.x')
Steven Bethard6a644f92008-03-18 22:08:20 +0000124 func = eval
125 meth = {}.get
126 with catch_warning() as w:
127 self.assertWarning(func < meth, w, expected)
128 with catch_warning() as w:
129 self.assertWarning(func > meth, w, expected)
130 with catch_warning() as w:
131 self.assertWarning(meth <= func, w, expected)
132 with catch_warning() as w:
133 self.assertWarning(meth >= func, w, expected)
134
Steven Bethardae42f332008-03-18 17:26:10 +0000135 def assertWarning(self, _, warning, expected_message):
136 self.assertEqual(str(warning.message), expected_message)
137
Raymond Hettinger05387862008-03-19 17:45:19 +0000138 def test_sort_cmp_arg(self):
Georg Brandld5b635f2008-03-25 08:29:14 +0000139 expected = "the cmp argument is not supported in 3.x"
Raymond Hettinger05387862008-03-19 17:45:19 +0000140 lst = range(5)
141 cmp = lambda x,y: -1
142
143 with catch_warning() as w:
144 self.assertWarning(lst.sort(cmp=cmp), w, expected)
145 with catch_warning() as w:
146 self.assertWarning(sorted(lst, cmp=cmp), w, expected)
147 with catch_warning() as w:
148 self.assertWarning(lst.sort(cmp), w, expected)
149 with catch_warning() as w:
150 self.assertWarning(sorted(lst, cmp), w, expected)
151
Georg Brandl5a444242008-03-21 20:11:46 +0000152 def test_sys_exc_clear(self):
Georg Brandld5b635f2008-03-25 08:29:14 +0000153 expected = 'sys.exc_clear() not supported in 3.x; use except clauses'
Georg Brandl5a444242008-03-21 20:11:46 +0000154 with catch_warning() as w:
155 self.assertWarning(sys.exc_clear(), w, expected)
156
Georg Brandl07e56812008-03-21 20:21:46 +0000157 def test_methods_members(self):
158 expected = '__members__ and __methods__ not supported in 3.x'
159 class C:
160 __methods__ = ['a']
161 __members__ = ['b']
162 c = C()
163 with catch_warning() as w:
164 self.assertWarning(dir(c), w, expected)
165
Georg Brandl65bb42d2008-03-21 20:38:24 +0000166 def test_softspace(self):
167 expected = 'file.softspace not supported in 3.x'
168 with file(__file__) as f:
169 with catch_warning() as w:
170 self.assertWarning(f.softspace, w, expected)
171 def set():
172 f.softspace = 0
173 with catch_warning() as w:
174 self.assertWarning(set(), w, expected)
175
Benjamin Petersonf4fcdb62008-06-08 23:00:00 +0000176 def test_tuple_parameter_unpacking(self):
177 expected = "tuple parameter unpacking has been removed in 3.x"
178 with catch_warning() as w:
179 exec "def f((a, b)): pass"
180 self.assertWarning(None, w, expected)
181
Georg Brandl80055f62008-03-25 07:56:27 +0000182 def test_buffer(self):
Georg Brandld5b635f2008-03-25 08:29:14 +0000183 expected = 'buffer() not supported in 3.x; use memoryview()'
Georg Brandl80055f62008-03-25 07:56:27 +0000184 with catch_warning() as w:
185 self.assertWarning(buffer('a'), w, expected)
186
Georg Brandla9916b52008-05-17 22:11:54 +0000187 def test_file_xreadlines(self):
188 expected = ("f.xreadlines() not supported in 3.x, "
189 "try 'for line in f' instead")
190 with file(__file__) as f:
191 with catch_warning() as w:
192 self.assertWarning(f.xreadlines(), w, expected)
193
Georg Brandl07e56812008-03-21 20:21:46 +0000194
Brett Cannone5d2cba2008-05-06 23:23:34 +0000195class TestStdlibRemovals(unittest.TestCase):
196
Brett Cannon3c759142008-05-09 05:25:37 +0000197 # test.testall not tested as it executes all unit tests as an
198 # import side-effect.
Brett Cannon4c1f8812008-05-10 02:27:04 +0000199 all_platforms = ('audiodev', 'imputil', 'mutex', 'user', 'new', 'rexec',
Brett Cannon27508d42008-05-10 22:45:07 +0000200 'Bastion', 'compiler', 'dircache', 'fpformat',
Georg Brandlac19d852008-06-01 21:19:14 +0000201 'ihooks', 'mhlib', 'statvfs', 'htmllib', 'sgmllib')
Brett Cannon54c77aa2008-05-14 21:08:41 +0000202 inclusive_platforms = {'irix' : ('pure', 'AL', 'al', 'CD', 'cd', 'cddb',
Brett Cannon044616a2008-05-15 02:33:55 +0000203 'cdplayer', 'CL', 'cl', 'DEVICE', 'GL',
Brett Cannon75ba4652008-05-15 03:23:17 +0000204 'gl', 'ERRNO', 'FILE', 'FL', 'flp', 'fl',
Brett Cannond8c41ec2008-05-15 03:41:55 +0000205 'fm', 'GET', 'GLWS', 'imgfile', 'IN',
Brett Cannoncd2de082008-05-15 03:51:21 +0000206 'IOCTL', 'jpeg', 'panel', 'panelparser',
Brett Cannon74a596c2008-05-15 04:17:35 +0000207 'readcd', 'SV', 'torgb', 'WAIT'),
Benjamin Peterson23681932008-05-12 21:42:13 +0000208 'darwin' : ('autoGIL', 'Carbon', 'OSATerminology',
Brett Cannonea785fb2008-05-14 01:09:40 +0000209 'icglue', 'Nav', 'MacOS', 'aepack',
210 'aetools', 'aetypes', 'applesingle',
211 'appletrawmain', 'appletrunner',
212 'argvemulator', 'bgenlocations',
Benjamin Peterson23681932008-05-12 21:42:13 +0000213 'EasyDialogs', 'macerrors', 'macostools',
214 'findertools', 'FrameWork', 'ic',
215 'gensuitemodule', 'icopen', 'macresource',
216 'MiniAEFrame', 'pimp', 'PixMapWrapper',
Brett Cannonea785fb2008-05-14 01:09:40 +0000217 'terminalcommand', 'videoreader',
218 '_builtinSuites', 'CodeWarrior',
219 'Explorer', 'Finder', 'Netscape',
220 'StdSuites', 'SystemEvents', 'Terminal',
221 'cfmfile', 'bundlebuilder', 'buildtools',
Brett Cannon22248172008-05-16 00:10:24 +0000222 'ColorPicker'),
223 'sunos5' : ('sunaudiodev', 'SUNAUDIODEV'),
224 }
Brett Cannonac861b52008-05-12 03:45:59 +0000225 optional_modules = ('bsddb185', 'Canvas', 'dl', 'linuxaudiodev', 'imageop',
Alexandre Vassalotti3c4971c2008-05-16 19:14:31 +0000226 'sv', 'cPickle')
Brett Cannone5d2cba2008-05-06 23:23:34 +0000227
Brett Cannon9ac39742008-05-09 22:51:58 +0000228 def check_removal(self, module_name, optional=False):
Brett Cannone5d2cba2008-05-06 23:23:34 +0000229 """Make sure the specified module, when imported, raises a
230 DeprecationWarning and specifies itself in the message."""
Alexandre Vassalottieb83f702008-05-11 07:06:04 +0000231 with CleanImport(module_name):
Brett Cannonddf7a422008-05-10 03:16:38 +0000232 with catch_warning(record=False) as w:
Brett Cannone5d2cba2008-05-06 23:23:34 +0000233 warnings.filterwarnings("error", ".+ removed",
234 DeprecationWarning)
235 try:
236 __import__(module_name, level=0)
237 except DeprecationWarning as exc:
Benjamin Petersonab1fb9f2008-05-12 22:26:05 +0000238 self.assert_(module_name in exc.args[0],
239 "%s warning didn't contain module name"
240 % module_name)
Brett Cannon9ac39742008-05-09 22:51:58 +0000241 except ImportError:
242 if not optional:
Benjamin Petersonbbb09372008-05-13 00:09:46 +0000243 self.fail("Non-optional module {0} raised an "
244 "ImportError.".format(module_name))
Brett Cannone5d2cba2008-05-06 23:23:34 +0000245 else:
Benjamin Petersonbbb09372008-05-13 00:09:46 +0000246 self.fail("DeprecationWarning not raised for {0}"
247 .format(module_name))
Brett Cannone5d2cba2008-05-06 23:23:34 +0000248
249 def test_platform_independent_removals(self):
250 # Make sure that the modules that are available on all platforms raise
251 # the proper DeprecationWarning.
252 for module_name in self.all_platforms:
253 self.check_removal(module_name)
254
Brett Cannon9ac39742008-05-09 22:51:58 +0000255 def test_platform_specific_removals(self):
256 # Test the removal of platform-specific modules.
257 for module_name in self.inclusive_platforms.get(sys.platform, []):
258 self.check_removal(module_name, optional=True)
259
Brett Cannon768d44f2008-05-10 02:47:54 +0000260 def test_optional_module_removals(self):
261 # Test the removal of modules that may or may not be built.
262 for module_name in self.optional_modules:
263 self.check_removal(module_name, optional=True)
264
Benjamin Peterson0893a0a2008-05-09 00:27:01 +0000265 def test_os_path_walk(self):
266 msg = "In 3.x, os.path.walk is removed in favor of os.walk."
267 def dumbo(where, names, args): pass
268 for path_mod in ("ntpath", "macpath", "os2emxpath", "posixpath"):
269 mod = __import__(path_mod)
270 with catch_warning() as w:
Benjamin Peterson1d310232008-05-27 01:42:29 +0000271 mod.walk("crashers", dumbo, None)
Benjamin Peterson0893a0a2008-05-09 00:27:01 +0000272 self.assertEquals(str(w.message), msg)
273
Benjamin Peterson3aa84a72008-05-26 19:41:53 +0000274 def test_commands_members(self):
275 import commands
276 members = {"mk2arg" : 2, "mkarg" : 1, "getstatus" : 1}
277 for name, arg_count in members.items():
278 with catch_warning(record=False):
279 warnings.filterwarnings("error")
280 func = getattr(commands, name)
281 self.assertRaises(DeprecationWarning, func, *([None]*arg_count))
282
Brett Cannonabb34fe2008-05-29 05:08:50 +0000283 def test_mutablestring_removal(self):
284 # UserString.MutableString has been removed in 3.0.
285 import UserString
286 with catch_warning(record=False):
287 warnings.filterwarnings("error", ".*MutableString",
288 DeprecationWarning)
289 self.assertRaises(DeprecationWarning, UserString.MutableString)
290
Brett Cannone5d2cba2008-05-06 23:23:34 +0000291
Steven Bethardae42f332008-03-18 17:26:10 +0000292def test_main():
Benjamin Peterson1d310232008-05-27 01:42:29 +0000293 with catch_warning(record=True):
294 warnings.simplefilter("always")
295 run_unittest(TestPy3KWarnings,
296 TestStdlibRemovals)
Steven Bethardae42f332008-03-18 17:26:10 +0000297
298if __name__ == '__main__':
299 test_main()