blob: b4e4e9d7116d5e3609b853b5a94b5ed707a09c29 [file] [log] [blame]
Steven Bethardae42f332008-03-18 17:26:10 +00001import unittest
Brett Cannon977eb022008-03-19 17:37:43 +00002import sys
Florent Xicluna945a8ba2010-03-17 19:15:56 +00003from test.test_support import check_py3k_warnings, CleanImport, run_unittest
Steven Bethardae42f332008-03-18 17:26:10 +00004import warnings
Victor Stinnerccd62702015-09-03 10:46:17 +02005from test import test_support
Steven Bethardae42f332008-03-18 17:26:10 +00006
Brett Cannon977eb022008-03-19 17:37:43 +00007if not sys.py3kwarning:
Benjamin Petersonbec087f2009-03-26 21:10:30 +00008 raise unittest.SkipTest('%s must be run with the -3 flag' % __name__)
Brett Cannon977eb022008-03-19 17:37:43 +00009
Florent Xicluna4d42f2b2010-03-09 19:57:01 +000010try:
11 from test.test_support import __warningregistry__ as _registry
12except ImportError:
13 def check_deprecated_module(module_name):
14 return False
15else:
16 past_warnings = _registry.keys()
17 del _registry
18 def check_deprecated_module(module_name):
19 """Lookup the past warnings for module already loaded using
20 test_support.import_module(..., deprecated=True)
21 """
22 return any(module_name in msg and ' removed' in msg
23 and issubclass(cls, DeprecationWarning)
24 and (' module' in msg or ' package' in msg)
25 for (msg, cls, line) in past_warnings)
26
Nick Coghland2e09382008-09-11 12:11:06 +000027def reset_module_registry(module):
28 try:
29 registry = module.__warningregistry__
30 except AttributeError:
31 pass
32 else:
33 registry.clear()
Steven Bethardae42f332008-03-18 17:26:10 +000034
35class TestPy3KWarnings(unittest.TestCase):
36
Nick Coghlan48361f52008-08-11 15:45:58 +000037 def assertWarning(self, _, warning, expected_message):
Nick Coghland2e09382008-09-11 12:11:06 +000038 self.assertEqual(str(warning.message), expected_message)
Nick Coghlan48361f52008-08-11 15:45:58 +000039
Benjamin Peterson1bf47652009-07-02 17:06:17 +000040 def assertNoWarning(self, _, recorder):
41 self.assertEqual(len(recorder.warnings), 0)
42
Benjamin Peterson2fe3ef82008-06-08 02:05:33 +000043 def test_backquote(self):
44 expected = 'backquote not supported in 3.x; use repr()'
Florent Xicluna945a8ba2010-03-17 19:15:56 +000045 with check_py3k_warnings((expected, SyntaxWarning)):
Benjamin Peterson2fe3ef82008-06-08 02:05:33 +000046 exec "`2`" in {}
Benjamin Peterson2fe3ef82008-06-08 02:05:33 +000047
Benjamin Peterson99a50232009-11-19 22:54:57 +000048 def test_paren_arg_names(self):
49 expected = 'parenthesized argument names are invalid in 3.x'
50 def check(s):
Florent Xicluna945a8ba2010-03-17 19:15:56 +000051 with check_py3k_warnings((expected, SyntaxWarning)):
52 exec s in {}
53 check("def f((x)): pass")
54 check("def f((((x))), (y)): pass")
55 check("def f((x), (((y))), m=32): pass")
56 # Something like def f((a, (b))): pass will raise the tuple
57 # unpacking warning.
Benjamin Peterson99a50232009-11-19 22:54:57 +000058
Benjamin Peterson399b1fe2008-10-25 02:53:28 +000059 def test_forbidden_names(self):
Benjamin Petersond5efd202008-06-08 22:52:37 +000060 # So we don't screw up our globals
61 def safe_exec(expr):
62 def f(**kwargs): pass
63 exec expr in {'f' : f}
64
Benjamin Peterson399b1fe2008-10-25 02:53:28 +000065 tests = [("True", "assignment to True or False is forbidden in 3.x"),
66 ("False", "assignment to True or False is forbidden in 3.x"),
67 ("nonlocal", "nonlocal is a keyword in 3.x")]
Florent Xicluna945a8ba2010-03-17 19:15:56 +000068 with check_py3k_warnings(('', SyntaxWarning)) as w:
Benjamin Peterson399b1fe2008-10-25 02:53:28 +000069 for keyword, expected in tests:
70 safe_exec("{0} = False".format(keyword))
71 self.assertWarning(None, w, expected)
72 w.reset()
73 try:
74 safe_exec("obj.{0} = True".format(keyword))
75 except NameError:
76 pass
77 self.assertWarning(None, w, expected)
78 w.reset()
79 safe_exec("def {0}(): pass".format(keyword))
80 self.assertWarning(None, w, expected)
81 w.reset()
82 safe_exec("class {0}: pass".format(keyword))
83 self.assertWarning(None, w, expected)
84 w.reset()
85 safe_exec("def f({0}=43): pass".format(keyword))
86 self.assertWarning(None, w, expected)
87 w.reset()
Benjamin Petersond5efd202008-06-08 22:52:37 +000088
89
Steven Bethardae42f332008-03-18 17:26:10 +000090 def test_type_inequality_comparisons(self):
Georg Brandld5b635f2008-03-25 08:29:14 +000091 expected = 'type inequality comparisons not supported in 3.x'
Florent Xicluna945a8ba2010-03-17 19:15:56 +000092 with check_py3k_warnings() as w:
Steven Bethardae42f332008-03-18 17:26:10 +000093 self.assertWarning(int < str, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +000094 w.reset()
Steven Bethardae42f332008-03-18 17:26:10 +000095 self.assertWarning(type < object, w, expected)
96
97 def test_object_inequality_comparisons(self):
Georg Brandld5b635f2008-03-25 08:29:14 +000098 expected = 'comparing unequal types not supported in 3.x'
Florent Xicluna945a8ba2010-03-17 19:15:56 +000099 with check_py3k_warnings() as w:
Steven Bethardae42f332008-03-18 17:26:10 +0000100 self.assertWarning(str < [], w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000101 w.reset()
Steven Bethardae42f332008-03-18 17:26:10 +0000102 self.assertWarning(object() < (1, 2), w, expected)
103
104 def test_dict_inequality_comparisons(self):
Georg Brandld5b635f2008-03-25 08:29:14 +0000105 expected = 'dict inequality comparisons not supported in 3.x'
Florent Xicluna945a8ba2010-03-17 19:15:56 +0000106 with check_py3k_warnings() as w:
Steven Bethardae42f332008-03-18 17:26:10 +0000107 self.assertWarning({} < {2:3}, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000108 w.reset()
Steven Bethardae42f332008-03-18 17:26:10 +0000109 self.assertWarning({} <= {}, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000110 w.reset()
Steven Bethardae42f332008-03-18 17:26:10 +0000111 self.assertWarning({} > {2:3}, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000112 w.reset()
Steven Bethardae42f332008-03-18 17:26:10 +0000113 self.assertWarning({2:3} >= {}, w, expected)
114
115 def test_cell_inequality_comparisons(self):
Georg Brandld5b635f2008-03-25 08:29:14 +0000116 expected = 'cell comparisons not supported in 3.x'
Steven Bethardae42f332008-03-18 17:26:10 +0000117 def f(x):
118 def g():
119 return x
120 return g
121 cell0, = f(0).func_closure
122 cell1, = f(1).func_closure
Florent Xicluna945a8ba2010-03-17 19:15:56 +0000123 with check_py3k_warnings() as w:
Steven Bethardae42f332008-03-18 17:26:10 +0000124 self.assertWarning(cell0 == cell1, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000125 w.reset()
Steven Bethardae42f332008-03-18 17:26:10 +0000126 self.assertWarning(cell0 < cell1, w, expected)
127
Steven Bethard6a644f92008-03-18 22:08:20 +0000128 def test_code_inequality_comparisons(self):
Georg Brandld5b635f2008-03-25 08:29:14 +0000129 expected = 'code inequality comparisons not supported in 3.x'
Steven Bethard6a644f92008-03-18 22:08:20 +0000130 def f(x):
131 pass
132 def g(x):
133 pass
Florent Xicluna945a8ba2010-03-17 19:15:56 +0000134 with check_py3k_warnings() as w:
Steven Bethard6a644f92008-03-18 22:08:20 +0000135 self.assertWarning(f.func_code < g.func_code, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000136 w.reset()
Steven Bethard6a644f92008-03-18 22:08:20 +0000137 self.assertWarning(f.func_code <= g.func_code, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000138 w.reset()
Steven Bethard6a644f92008-03-18 22:08:20 +0000139 self.assertWarning(f.func_code >= g.func_code, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000140 w.reset()
Steven Bethard6a644f92008-03-18 22:08:20 +0000141 self.assertWarning(f.func_code > g.func_code, w, expected)
142
143 def test_builtin_function_or_method_comparisons(self):
144 expected = ('builtin_function_or_method '
Benjamin Peterson1bf47652009-07-02 17:06:17 +0000145 'order comparisons not supported in 3.x')
Steven Bethard6a644f92008-03-18 22:08:20 +0000146 func = eval
147 meth = {}.get
Florent Xicluna945a8ba2010-03-17 19:15:56 +0000148 with check_py3k_warnings() as w:
Steven Bethard6a644f92008-03-18 22:08:20 +0000149 self.assertWarning(func < meth, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000150 w.reset()
Steven Bethard6a644f92008-03-18 22:08:20 +0000151 self.assertWarning(func > meth, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000152 w.reset()
Steven Bethard6a644f92008-03-18 22:08:20 +0000153 self.assertWarning(meth <= func, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000154 w.reset()
Steven Bethard6a644f92008-03-18 22:08:20 +0000155 self.assertWarning(meth >= func, w, expected)
Benjamin Peterson1bf47652009-07-02 17:06:17 +0000156 w.reset()
157 self.assertNoWarning(meth == func, w)
158 self.assertNoWarning(meth != func, w)
159 lam = lambda x: x
160 self.assertNoWarning(lam == func, w)
161 self.assertNoWarning(lam != func, w)
Steven Bethard6a644f92008-03-18 22:08:20 +0000162
Benjamin Petersonf09925d2008-12-22 20:16:25 +0000163 def test_frame_attributes(self):
164 template = "%s has been removed in 3.x"
165 f = sys._getframe(0)
166 for attr in ("f_exc_traceback", "f_exc_value", "f_exc_type"):
167 expected = template % attr
Florent Xicluna945a8ba2010-03-17 19:15:56 +0000168 with check_py3k_warnings() as w:
Benjamin Petersonf09925d2008-12-22 20:16:25 +0000169 self.assertWarning(getattr(f, attr), w, expected)
170 w.reset()
171 self.assertWarning(setattr(f, attr, None), w, expected)
172
Raymond Hettinger05387862008-03-19 17:45:19 +0000173 def test_sort_cmp_arg(self):
Georg Brandld5b635f2008-03-25 08:29:14 +0000174 expected = "the cmp argument is not supported in 3.x"
Raymond Hettinger05387862008-03-19 17:45:19 +0000175 lst = range(5)
176 cmp = lambda x,y: -1
177
Florent Xicluna945a8ba2010-03-17 19:15:56 +0000178 with check_py3k_warnings() as w:
Raymond Hettinger05387862008-03-19 17:45:19 +0000179 self.assertWarning(lst.sort(cmp=cmp), w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000180 w.reset()
Raymond Hettinger05387862008-03-19 17:45:19 +0000181 self.assertWarning(sorted(lst, cmp=cmp), w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000182 w.reset()
Raymond Hettinger05387862008-03-19 17:45:19 +0000183 self.assertWarning(lst.sort(cmp), w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000184 w.reset()
Raymond Hettinger05387862008-03-19 17:45:19 +0000185 self.assertWarning(sorted(lst, cmp), w, expected)
186
Georg Brandl5a444242008-03-21 20:11:46 +0000187 def test_sys_exc_clear(self):
Georg Brandld5b635f2008-03-25 08:29:14 +0000188 expected = 'sys.exc_clear() not supported in 3.x; use except clauses'
Florent Xicluna945a8ba2010-03-17 19:15:56 +0000189 with check_py3k_warnings() as w:
Georg Brandl5a444242008-03-21 20:11:46 +0000190 self.assertWarning(sys.exc_clear(), w, expected)
191
Georg Brandl07e56812008-03-21 20:21:46 +0000192 def test_methods_members(self):
193 expected = '__members__ and __methods__ not supported in 3.x'
194 class C:
195 __methods__ = ['a']
196 __members__ = ['b']
197 c = C()
Florent Xicluna945a8ba2010-03-17 19:15:56 +0000198 with check_py3k_warnings() as w:
Georg Brandl07e56812008-03-21 20:21:46 +0000199 self.assertWarning(dir(c), w, expected)
200
Georg Brandl65bb42d2008-03-21 20:38:24 +0000201 def test_softspace(self):
202 expected = 'file.softspace not supported in 3.x'
203 with file(__file__) as f:
Florent Xicluna945a8ba2010-03-17 19:15:56 +0000204 with check_py3k_warnings() as w:
Georg Brandl65bb42d2008-03-21 20:38:24 +0000205 self.assertWarning(f.softspace, w, expected)
206 def set():
207 f.softspace = 0
Florent Xicluna945a8ba2010-03-17 19:15:56 +0000208 with check_py3k_warnings() as w:
Georg Brandl65bb42d2008-03-21 20:38:24 +0000209 self.assertWarning(set(), w, expected)
210
Benjamin Peterson712ee922008-08-24 18:10:20 +0000211 def test_slice_methods(self):
212 class Spam(object):
213 def __getslice__(self, i, j): pass
214 def __setslice__(self, i, j, what): pass
215 def __delslice__(self, i, j): pass
216 class Egg:
217 def __getslice__(self, i, h): pass
218 def __setslice__(self, i, j, what): pass
219 def __delslice__(self, i, j): pass
220
221 expected = "in 3.x, __{0}slice__ has been removed; use __{0}item__"
222
223 for obj in (Spam(), Egg()):
Florent Xicluna945a8ba2010-03-17 19:15:56 +0000224 with check_py3k_warnings() as w:
Benjamin Peterson712ee922008-08-24 18:10:20 +0000225 self.assertWarning(obj[1:2], w, expected.format('get'))
Nick Coghland2e09382008-09-11 12:11:06 +0000226 w.reset()
Benjamin Peterson712ee922008-08-24 18:10:20 +0000227 del obj[3:4]
228 self.assertWarning(None, w, expected.format('del'))
Nick Coghland2e09382008-09-11 12:11:06 +0000229 w.reset()
Benjamin Peterson712ee922008-08-24 18:10:20 +0000230 obj[4:5] = "eggs"
231 self.assertWarning(None, w, expected.format('set'))
232
Benjamin Petersonf4fcdb62008-06-08 23:00:00 +0000233 def test_tuple_parameter_unpacking(self):
234 expected = "tuple parameter unpacking has been removed in 3.x"
Florent Xicluna945a8ba2010-03-17 19:15:56 +0000235 with check_py3k_warnings((expected, SyntaxWarning)):
Benjamin Petersonf4fcdb62008-06-08 23:00:00 +0000236 exec "def f((a, b)): pass"
Benjamin Petersonf4fcdb62008-06-08 23:00:00 +0000237
Georg Brandl80055f62008-03-25 07:56:27 +0000238 def test_buffer(self):
Nick Coghlan48361f52008-08-11 15:45:58 +0000239 expected = 'buffer() not supported in 3.x'
Florent Xicluna945a8ba2010-03-17 19:15:56 +0000240 with check_py3k_warnings() as w:
Georg Brandl80055f62008-03-25 07:56:27 +0000241 self.assertWarning(buffer('a'), w, expected)
242
Georg Brandla9916b52008-05-17 22:11:54 +0000243 def test_file_xreadlines(self):
244 expected = ("f.xreadlines() not supported in 3.x, "
245 "try 'for line in f' instead")
246 with file(__file__) as f:
Florent Xicluna945a8ba2010-03-17 19:15:56 +0000247 with check_py3k_warnings() as w:
Georg Brandla9916b52008-05-17 22:11:54 +0000248 self.assertWarning(f.xreadlines(), w, expected)
249
Nick Coghlan48361f52008-08-11 15:45:58 +0000250 def test_hash_inheritance(self):
Florent Xicluna945a8ba2010-03-17 19:15:56 +0000251 with check_py3k_warnings() as w:
Nick Coghlan48361f52008-08-11 15:45:58 +0000252 # With object as the base class
253 class WarnOnlyCmp(object):
254 def __cmp__(self, other): pass
Mark Dickinsonec27d912010-06-05 13:18:33 +0000255 self.assertEqual(len(w.warnings), 0)
Nick Coghland2e09382008-09-11 12:11:06 +0000256 w.reset()
Nick Coghlan48361f52008-08-11 15:45:58 +0000257 class WarnOnlyEq(object):
258 def __eq__(self, other): pass
Nick Coghland2e09382008-09-11 12:11:06 +0000259 self.assertEqual(len(w.warnings), 1)
Nick Coghlan48361f52008-08-11 15:45:58 +0000260 self.assertWarning(None, w,
261 "Overriding __eq__ blocks inheritance of __hash__ in 3.x")
Nick Coghland2e09382008-09-11 12:11:06 +0000262 w.reset()
Nick Coghlan48361f52008-08-11 15:45:58 +0000263 class WarnCmpAndEq(object):
264 def __cmp__(self, other): pass
265 def __eq__(self, other): pass
Mark Dickinsonec27d912010-06-05 13:18:33 +0000266 self.assertEqual(len(w.warnings), 1)
Nick Coghlan48361f52008-08-11 15:45:58 +0000267 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
Mark Dickinsonec27d912010-06-05 13:18:33 +0000280 self.assertEqual(len(w.warnings), 0)
Nick Coghland2e09382008-09-11 12:11:06 +0000281 w.reset()
Nick Coghlan48361f52008-08-11 15:45:58 +0000282 class WarnOnlyEq(DefinesAllThree):
283 def __eq__(self, other): pass
Nick Coghland2e09382008-09-11 12:11:06 +0000284 self.assertEqual(len(w.warnings), 1)
Nick Coghlan48361f52008-08-11 15:45:58 +0000285 self.assertWarning(None, w,
286 "Overriding __eq__ blocks inheritance of __hash__ in 3.x")
Nick Coghland2e09382008-09-11 12:11:06 +0000287 w.reset()
Nick Coghlan48361f52008-08-11 15:45:58 +0000288 class WarnCmpAndEq(DefinesAllThree):
289 def __cmp__(self, other): pass
290 def __eq__(self, other): pass
Mark Dickinsonec27d912010-06-05 13:18:33 +0000291 self.assertEqual(len(w.warnings), 1)
Nick Coghlan48361f52008-08-11 15:45:58 +0000292 self.assertWarning(None, w,
293 "Overriding __eq__ blocks inheritance of __hash__ in 3.x")
Nick Coghland2e09382008-09-11 12:11:06 +0000294 w.reset()
Nick Coghlan48361f52008-08-11 15:45:58 +0000295 class NoWarningOnlyHash(DefinesAllThree):
296 def __hash__(self): pass
Nick Coghland2e09382008-09-11 12:11:06 +0000297 self.assertEqual(len(w.warnings), 0)
Nick Coghlan48361f52008-08-11 15:45:58 +0000298
Alexandre Vassalotti0fe79912009-07-05 04:22:40 +0000299 def test_operator(self):
300 from operator import isCallable, sequenceIncludes
301
Alexandre Vassalotti16a02472009-07-05 04:25:46 +0000302 callable_warn = ("operator.isCallable() is not supported in 3.x. "
303 "Use hasattr(obj, '__call__').")
Alexandre Vassalotti0fe79912009-07-05 04:22:40 +0000304 seq_warn = ("operator.sequenceIncludes() is not supported "
305 "in 3.x. Use operator.contains().")
Florent Xicluna945a8ba2010-03-17 19:15:56 +0000306 with check_py3k_warnings() as w:
Alexandre Vassalotti0fe79912009-07-05 04:22:40 +0000307 self.assertWarning(isCallable(self), w, callable_warn)
308 w.reset()
309 self.assertWarning(sequenceIncludes(range(3), 2), w, seq_warn)
310
Serhiy Storchaka79fa98a2014-06-01 22:13:39 +0300311 def test_nonascii_bytes_literals(self):
312 expected = "non-ascii bytes literals not supported in 3.x"
313 with check_py3k_warnings((expected, SyntaxWarning)):
314 exec "b'\xbd'"
315
Georg Brandl07e56812008-03-21 20:21:46 +0000316
Brett Cannone5d2cba2008-05-06 23:23:34 +0000317class TestStdlibRemovals(unittest.TestCase):
318
Brett Cannon3c759142008-05-09 05:25:37 +0000319 # test.testall not tested as it executes all unit tests as an
320 # import side-effect.
Brett Cannon4c1f8812008-05-10 02:27:04 +0000321 all_platforms = ('audiodev', 'imputil', 'mutex', 'user', 'new', 'rexec',
Brett Cannon1e8fba72008-07-18 19:30:22 +0000322 'Bastion', 'compiler', 'dircache', 'mimetools',
323 'fpformat', 'ihooks', 'mhlib', 'statvfs', 'htmllib',
324 'sgmllib', 'rfc822', 'sunaudio')
Brett Cannon54c77aa2008-05-14 21:08:41 +0000325 inclusive_platforms = {'irix' : ('pure', 'AL', 'al', 'CD', 'cd', 'cddb',
Brett Cannon044616a2008-05-15 02:33:55 +0000326 'cdplayer', 'CL', 'cl', 'DEVICE', 'GL',
Brett Cannon75ba4652008-05-15 03:23:17 +0000327 'gl', 'ERRNO', 'FILE', 'FL', 'flp', 'fl',
Brett Cannond8c41ec2008-05-15 03:41:55 +0000328 'fm', 'GET', 'GLWS', 'imgfile', 'IN',
Brett Cannoncd2de082008-05-15 03:51:21 +0000329 'IOCTL', 'jpeg', 'panel', 'panelparser',
Brett Cannon74a596c2008-05-15 04:17:35 +0000330 'readcd', 'SV', 'torgb', 'WAIT'),
Benjamin Peterson23681932008-05-12 21:42:13 +0000331 'darwin' : ('autoGIL', 'Carbon', 'OSATerminology',
Ronald Oussoren934f4e12010-06-22 09:18:28 +0000332 'icglue', 'Nav',
333 # MacOS should (and does) give a Py3kWarning, but one of the
334 # earlier tests already imports the MacOS extension which causes
335 # this test to fail. Disabling the test for 'MacOS' avoids this
336 # spurious test failure.
337 #'MacOS',
338 'aepack',
Brett Cannonea785fb2008-05-14 01:09:40 +0000339 'aetools', 'aetypes', 'applesingle',
340 'appletrawmain', 'appletrunner',
341 'argvemulator', 'bgenlocations',
Benjamin Peterson23681932008-05-12 21:42:13 +0000342 'EasyDialogs', 'macerrors', 'macostools',
343 'findertools', 'FrameWork', 'ic',
344 'gensuitemodule', 'icopen', 'macresource',
345 'MiniAEFrame', 'pimp', 'PixMapWrapper',
Brett Cannonea785fb2008-05-14 01:09:40 +0000346 'terminalcommand', 'videoreader',
347 '_builtinSuites', 'CodeWarrior',
348 'Explorer', 'Finder', 'Netscape',
349 'StdSuites', 'SystemEvents', 'Terminal',
350 'cfmfile', 'bundlebuilder', 'buildtools',
Benjamin Petersona6864e02008-07-14 17:42:17 +0000351 'ColorPicker', 'Audio_mac'),
Brett Cannon22248172008-05-16 00:10:24 +0000352 'sunos5' : ('sunaudiodev', 'SUNAUDIODEV'),
353 }
Brett Cannonac861b52008-05-12 03:45:59 +0000354 optional_modules = ('bsddb185', 'Canvas', 'dl', 'linuxaudiodev', 'imageop',
Antoine Pitrou80157252010-01-08 19:20:17 +0000355 'sv', 'bsddb', 'dbhash')
Brett Cannone5d2cba2008-05-06 23:23:34 +0000356
Brett Cannon9ac39742008-05-09 22:51:58 +0000357 def check_removal(self, module_name, optional=False):
Brett Cannone5d2cba2008-05-06 23:23:34 +0000358 """Make sure the specified module, when imported, raises a
359 DeprecationWarning and specifies itself in the message."""
Victor Stinnerccd62702015-09-03 10:46:17 +0200360 if module_name in sys.modules:
361 mod = sys.modules[module_name]
362 filename = getattr(mod, '__file__', '')
363 mod = None
364 # the module is not implemented in C?
365 if not filename.endswith(('.py', '.pyc', '.pyo')):
366 # Issue #23375: If the module was already loaded, reimporting
367 # the module will not emit again the warning. The warning is
368 # emited when the module is loaded, but C modules cannot
369 # unloaded.
370 if test_support.verbose:
371 print("Cannot test the Python 3 DeprecationWarning of the "
372 "%s module, the C module is already loaded"
373 % module_name)
374 return
Florent Xicluna4d42f2b2010-03-09 19:57:01 +0000375 with CleanImport(module_name), warnings.catch_warnings():
376 warnings.filterwarnings("error", ".+ (module|package) .+ removed",
377 DeprecationWarning, __name__)
378 warnings.filterwarnings("error", ".+ removed .+ (module|package)",
Benjamin Petersona6864e02008-07-14 17:42:17 +0000379 DeprecationWarning, __name__)
380 try:
381 __import__(module_name, level=0)
382 except DeprecationWarning as exc:
Ezio Melottiaa980582010-01-23 23:04:36 +0000383 self.assertIn(module_name, exc.args[0],
384 "%s warning didn't contain module name"
385 % module_name)
Benjamin Petersona6864e02008-07-14 17:42:17 +0000386 except ImportError:
387 if not optional:
388 self.fail("Non-optional module {0} raised an "
389 "ImportError.".format(module_name))
390 else:
Florent Xicluna4d42f2b2010-03-09 19:57:01 +0000391 # For extension modules, check the __warningregistry__.
392 # They won't rerun their init code even with CleanImport.
393 if not check_deprecated_module(module_name):
394 self.fail("DeprecationWarning not raised for {0}"
395 .format(module_name))
Brett Cannone5d2cba2008-05-06 23:23:34 +0000396
397 def test_platform_independent_removals(self):
398 # Make sure that the modules that are available on all platforms raise
399 # the proper DeprecationWarning.
400 for module_name in self.all_platforms:
401 self.check_removal(module_name)
402
Brett Cannon9ac39742008-05-09 22:51:58 +0000403 def test_platform_specific_removals(self):
404 # Test the removal of platform-specific modules.
405 for module_name in self.inclusive_platforms.get(sys.platform, []):
406 self.check_removal(module_name, optional=True)
407
Brett Cannon768d44f2008-05-10 02:47:54 +0000408 def test_optional_module_removals(self):
409 # Test the removal of modules that may or may not be built.
410 for module_name in self.optional_modules:
411 self.check_removal(module_name, optional=True)
412
Benjamin Peterson0893a0a2008-05-09 00:27:01 +0000413 def test_os_path_walk(self):
414 msg = "In 3.x, os.path.walk is removed in favor of os.walk."
415 def dumbo(where, names, args): pass
416 for path_mod in ("ntpath", "macpath", "os2emxpath", "posixpath"):
417 mod = __import__(path_mod)
Nick Coghland2e09382008-09-11 12:11:06 +0000418 reset_module_registry(mod)
Florent Xicluna945a8ba2010-03-17 19:15:56 +0000419 with check_py3k_warnings() as w:
Benjamin Peterson1d310232008-05-27 01:42:29 +0000420 mod.walk("crashers", dumbo, None)
Ezio Melotti2623a372010-11-21 13:34:58 +0000421 self.assertEqual(str(w.message), msg)
Benjamin Peterson0893a0a2008-05-09 00:27:01 +0000422
Benjamin Peterson541f7da2008-08-18 02:12:23 +0000423 def test_reduce_move(self):
424 from operator import add
Nick Coghland2e09382008-09-11 12:11:06 +0000425 # reduce tests may have already triggered this warning
Florent Xicluna4d42f2b2010-03-09 19:57:01 +0000426 reset_module_registry(unittest.case)
Brett Cannon672237d2008-09-09 00:49:16 +0000427 with warnings.catch_warnings():
Benjamin Peterson541f7da2008-08-18 02:12:23 +0000428 warnings.filterwarnings("error", "reduce")
429 self.assertRaises(DeprecationWarning, reduce, add, range(10))
430
Brett Cannonabb34fe2008-05-29 05:08:50 +0000431 def test_mutablestring_removal(self):
432 # UserString.MutableString has been removed in 3.0.
433 import UserString
Nick Coghland2e09382008-09-11 12:11:06 +0000434 # UserString tests may have already triggered this warning
435 reset_module_registry(UserString)
Brett Cannon672237d2008-09-09 00:49:16 +0000436 with warnings.catch_warnings():
Brett Cannonabb34fe2008-05-29 05:08:50 +0000437 warnings.filterwarnings("error", ".*MutableString",
438 DeprecationWarning)
439 self.assertRaises(DeprecationWarning, UserString.MutableString)
440
Brett Cannone5d2cba2008-05-06 23:23:34 +0000441
Steven Bethardae42f332008-03-18 17:26:10 +0000442def test_main():
Florent Xicluna4d42f2b2010-03-09 19:57:01 +0000443 run_unittest(TestPy3KWarnings,
444 TestStdlibRemovals)
Steven Bethardae42f332008-03-18 17:26:10 +0000445
446if __name__ == '__main__':
447 test_main()