blob: 0f78ac800a942c746c68501ea911f518cddd9d4b [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
5
Brett Cannon977eb022008-03-19 17:37:43 +00006if not sys.py3kwarning:
Benjamin Petersonbec087f2009-03-26 21:10:30 +00007 raise unittest.SkipTest('%s must be run with the -3 flag' % __name__)
Brett Cannon977eb022008-03-19 17:37:43 +00008
Florent Xicluna4d42f2b2010-03-09 19:57:01 +00009try:
10 from test.test_support import __warningregistry__ as _registry
11except ImportError:
12 def check_deprecated_module(module_name):
13 return False
14else:
15 past_warnings = _registry.keys()
16 del _registry
17 def check_deprecated_module(module_name):
18 """Lookup the past warnings for module already loaded using
19 test_support.import_module(..., deprecated=True)
20 """
21 return any(module_name in msg and ' removed' in msg
22 and issubclass(cls, DeprecationWarning)
23 and (' module' in msg or ' package' in msg)
24 for (msg, cls, line) in past_warnings)
25
Nick Coghland2e09382008-09-11 12:11:06 +000026def reset_module_registry(module):
27 try:
28 registry = module.__warningregistry__
29 except AttributeError:
30 pass
31 else:
32 registry.clear()
Steven Bethardae42f332008-03-18 17:26:10 +000033
34class TestPy3KWarnings(unittest.TestCase):
35
Nick Coghlan48361f52008-08-11 15:45:58 +000036 def assertWarning(self, _, warning, expected_message):
Nick Coghland2e09382008-09-11 12:11:06 +000037 self.assertEqual(str(warning.message), expected_message)
Nick Coghlan48361f52008-08-11 15:45:58 +000038
Benjamin Peterson1bf47652009-07-02 17:06:17 +000039 def assertNoWarning(self, _, recorder):
40 self.assertEqual(len(recorder.warnings), 0)
41
Benjamin Peterson2fe3ef82008-06-08 02:05:33 +000042 def test_backquote(self):
43 expected = 'backquote not supported in 3.x; use repr()'
Florent Xicluna945a8ba2010-03-17 19:15:56 +000044 with check_py3k_warnings((expected, SyntaxWarning)):
Benjamin Peterson2fe3ef82008-06-08 02:05:33 +000045 exec "`2`" in {}
Benjamin Peterson2fe3ef82008-06-08 02:05:33 +000046
Benjamin Peterson99a50232009-11-19 22:54:57 +000047 def test_paren_arg_names(self):
48 expected = 'parenthesized argument names are invalid in 3.x'
49 def check(s):
Florent Xicluna945a8ba2010-03-17 19:15:56 +000050 with check_py3k_warnings((expected, SyntaxWarning)):
51 exec s in {}
52 check("def f((x)): pass")
53 check("def f((((x))), (y)): pass")
54 check("def f((x), (((y))), m=32): pass")
55 # Something like def f((a, (b))): pass will raise the tuple
56 # unpacking warning.
Benjamin Peterson99a50232009-11-19 22:54:57 +000057
Benjamin Peterson399b1fe2008-10-25 02:53:28 +000058 def test_forbidden_names(self):
Benjamin Petersond5efd202008-06-08 22:52:37 +000059 # So we don't screw up our globals
60 def safe_exec(expr):
61 def f(**kwargs): pass
62 exec expr in {'f' : f}
63
Benjamin Peterson399b1fe2008-10-25 02:53:28 +000064 tests = [("True", "assignment to True or False is forbidden in 3.x"),
65 ("False", "assignment to True or False is forbidden in 3.x"),
66 ("nonlocal", "nonlocal is a keyword in 3.x")]
Florent Xicluna945a8ba2010-03-17 19:15:56 +000067 with check_py3k_warnings(('', SyntaxWarning)) as w:
Benjamin Peterson399b1fe2008-10-25 02:53:28 +000068 for keyword, expected in tests:
69 safe_exec("{0} = False".format(keyword))
70 self.assertWarning(None, w, expected)
71 w.reset()
72 try:
73 safe_exec("obj.{0} = True".format(keyword))
74 except NameError:
75 pass
76 self.assertWarning(None, w, expected)
77 w.reset()
78 safe_exec("def {0}(): pass".format(keyword))
79 self.assertWarning(None, w, expected)
80 w.reset()
81 safe_exec("class {0}: pass".format(keyword))
82 self.assertWarning(None, w, expected)
83 w.reset()
84 safe_exec("def f({0}=43): pass".format(keyword))
85 self.assertWarning(None, w, expected)
86 w.reset()
Benjamin Petersond5efd202008-06-08 22:52:37 +000087
88
Steven Bethardae42f332008-03-18 17:26:10 +000089 def test_type_inequality_comparisons(self):
Georg Brandld5b635f2008-03-25 08:29:14 +000090 expected = 'type inequality comparisons not supported in 3.x'
Florent Xicluna945a8ba2010-03-17 19:15:56 +000091 with check_py3k_warnings() as w:
Steven Bethardae42f332008-03-18 17:26:10 +000092 self.assertWarning(int < str, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +000093 w.reset()
Steven Bethardae42f332008-03-18 17:26:10 +000094 self.assertWarning(type < object, w, expected)
95
96 def test_object_inequality_comparisons(self):
Georg Brandld5b635f2008-03-25 08:29:14 +000097 expected = 'comparing unequal types not supported in 3.x'
Florent Xicluna945a8ba2010-03-17 19:15:56 +000098 with check_py3k_warnings() as w:
Steven Bethardae42f332008-03-18 17:26:10 +000099 self.assertWarning(str < [], w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000100 w.reset()
Steven Bethardae42f332008-03-18 17:26:10 +0000101 self.assertWarning(object() < (1, 2), w, expected)
102
103 def test_dict_inequality_comparisons(self):
Georg Brandld5b635f2008-03-25 08:29:14 +0000104 expected = 'dict inequality comparisons not supported in 3.x'
Florent Xicluna945a8ba2010-03-17 19:15:56 +0000105 with check_py3k_warnings() as w:
Steven Bethardae42f332008-03-18 17:26:10 +0000106 self.assertWarning({} < {2:3}, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000107 w.reset()
Steven Bethardae42f332008-03-18 17:26:10 +0000108 self.assertWarning({} <= {}, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000109 w.reset()
Steven Bethardae42f332008-03-18 17:26:10 +0000110 self.assertWarning({} > {2:3}, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000111 w.reset()
Steven Bethardae42f332008-03-18 17:26:10 +0000112 self.assertWarning({2:3} >= {}, w, expected)
113
114 def test_cell_inequality_comparisons(self):
Georg Brandld5b635f2008-03-25 08:29:14 +0000115 expected = 'cell comparisons not supported in 3.x'
Steven Bethardae42f332008-03-18 17:26:10 +0000116 def f(x):
117 def g():
118 return x
119 return g
120 cell0, = f(0).func_closure
121 cell1, = f(1).func_closure
Florent Xicluna945a8ba2010-03-17 19:15:56 +0000122 with check_py3k_warnings() as w:
Steven Bethardae42f332008-03-18 17:26:10 +0000123 self.assertWarning(cell0 == cell1, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000124 w.reset()
Steven Bethardae42f332008-03-18 17:26:10 +0000125 self.assertWarning(cell0 < cell1, w, expected)
126
Steven Bethard6a644f92008-03-18 22:08:20 +0000127 def test_code_inequality_comparisons(self):
Georg Brandld5b635f2008-03-25 08:29:14 +0000128 expected = 'code inequality comparisons not supported in 3.x'
Steven Bethard6a644f92008-03-18 22:08:20 +0000129 def f(x):
130 pass
131 def g(x):
132 pass
Florent Xicluna945a8ba2010-03-17 19:15:56 +0000133 with check_py3k_warnings() as w:
Steven Bethard6a644f92008-03-18 22:08:20 +0000134 self.assertWarning(f.func_code < g.func_code, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000135 w.reset()
Steven Bethard6a644f92008-03-18 22:08:20 +0000136 self.assertWarning(f.func_code <= g.func_code, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000137 w.reset()
Steven Bethard6a644f92008-03-18 22:08:20 +0000138 self.assertWarning(f.func_code >= g.func_code, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000139 w.reset()
Steven Bethard6a644f92008-03-18 22:08:20 +0000140 self.assertWarning(f.func_code > g.func_code, w, expected)
141
142 def test_builtin_function_or_method_comparisons(self):
143 expected = ('builtin_function_or_method '
Benjamin Peterson1bf47652009-07-02 17:06:17 +0000144 'order comparisons not supported in 3.x')
Steven Bethard6a644f92008-03-18 22:08:20 +0000145 func = eval
146 meth = {}.get
Florent Xicluna945a8ba2010-03-17 19:15:56 +0000147 with check_py3k_warnings() as w:
Steven Bethard6a644f92008-03-18 22:08:20 +0000148 self.assertWarning(func < meth, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000149 w.reset()
Steven Bethard6a644f92008-03-18 22:08:20 +0000150 self.assertWarning(func > meth, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000151 w.reset()
Steven Bethard6a644f92008-03-18 22:08:20 +0000152 self.assertWarning(meth <= func, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000153 w.reset()
Steven Bethard6a644f92008-03-18 22:08:20 +0000154 self.assertWarning(meth >= func, w, expected)
Benjamin Peterson1bf47652009-07-02 17:06:17 +0000155 w.reset()
156 self.assertNoWarning(meth == func, w)
157 self.assertNoWarning(meth != func, w)
158 lam = lambda x: x
159 self.assertNoWarning(lam == func, w)
160 self.assertNoWarning(lam != func, w)
Steven Bethard6a644f92008-03-18 22:08:20 +0000161
Benjamin Petersonf09925d2008-12-22 20:16:25 +0000162 def test_frame_attributes(self):
163 template = "%s has been removed in 3.x"
164 f = sys._getframe(0)
165 for attr in ("f_exc_traceback", "f_exc_value", "f_exc_type"):
166 expected = template % attr
Florent Xicluna945a8ba2010-03-17 19:15:56 +0000167 with check_py3k_warnings() as w:
Benjamin Petersonf09925d2008-12-22 20:16:25 +0000168 self.assertWarning(getattr(f, attr), w, expected)
169 w.reset()
170 self.assertWarning(setattr(f, attr, None), w, expected)
171
Raymond Hettinger05387862008-03-19 17:45:19 +0000172 def test_sort_cmp_arg(self):
Georg Brandld5b635f2008-03-25 08:29:14 +0000173 expected = "the cmp argument is not supported in 3.x"
Raymond Hettinger05387862008-03-19 17:45:19 +0000174 lst = range(5)
175 cmp = lambda x,y: -1
176
Florent Xicluna945a8ba2010-03-17 19:15:56 +0000177 with check_py3k_warnings() as w:
Raymond Hettinger05387862008-03-19 17:45:19 +0000178 self.assertWarning(lst.sort(cmp=cmp), w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000179 w.reset()
Raymond Hettinger05387862008-03-19 17:45:19 +0000180 self.assertWarning(sorted(lst, cmp=cmp), w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000181 w.reset()
Raymond Hettinger05387862008-03-19 17:45:19 +0000182 self.assertWarning(lst.sort(cmp), w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000183 w.reset()
Raymond Hettinger05387862008-03-19 17:45:19 +0000184 self.assertWarning(sorted(lst, cmp), w, expected)
185
Georg Brandl5a444242008-03-21 20:11:46 +0000186 def test_sys_exc_clear(self):
Georg Brandld5b635f2008-03-25 08:29:14 +0000187 expected = 'sys.exc_clear() not supported in 3.x; use except clauses'
Florent Xicluna945a8ba2010-03-17 19:15:56 +0000188 with check_py3k_warnings() as w:
Georg Brandl5a444242008-03-21 20:11:46 +0000189 self.assertWarning(sys.exc_clear(), w, expected)
190
Georg Brandl07e56812008-03-21 20:21:46 +0000191 def test_methods_members(self):
192 expected = '__members__ and __methods__ not supported in 3.x'
193 class C:
194 __methods__ = ['a']
195 __members__ = ['b']
196 c = C()
Florent Xicluna945a8ba2010-03-17 19:15:56 +0000197 with check_py3k_warnings() as w:
Georg Brandl07e56812008-03-21 20:21:46 +0000198 self.assertWarning(dir(c), w, expected)
199
Georg Brandl65bb42d2008-03-21 20:38:24 +0000200 def test_softspace(self):
201 expected = 'file.softspace not supported in 3.x'
202 with file(__file__) as f:
Florent Xicluna945a8ba2010-03-17 19:15:56 +0000203 with check_py3k_warnings() as w:
Georg Brandl65bb42d2008-03-21 20:38:24 +0000204 self.assertWarning(f.softspace, w, expected)
205 def set():
206 f.softspace = 0
Florent Xicluna945a8ba2010-03-17 19:15:56 +0000207 with check_py3k_warnings() as w:
Georg Brandl65bb42d2008-03-21 20:38:24 +0000208 self.assertWarning(set(), w, expected)
209
Benjamin Peterson712ee922008-08-24 18:10:20 +0000210 def test_slice_methods(self):
211 class Spam(object):
212 def __getslice__(self, i, j): pass
213 def __setslice__(self, i, j, what): pass
214 def __delslice__(self, i, j): pass
215 class Egg:
216 def __getslice__(self, i, h): pass
217 def __setslice__(self, i, j, what): pass
218 def __delslice__(self, i, j): pass
219
220 expected = "in 3.x, __{0}slice__ has been removed; use __{0}item__"
221
222 for obj in (Spam(), Egg()):
Florent Xicluna945a8ba2010-03-17 19:15:56 +0000223 with check_py3k_warnings() as w:
Benjamin Peterson712ee922008-08-24 18:10:20 +0000224 self.assertWarning(obj[1:2], w, expected.format('get'))
Nick Coghland2e09382008-09-11 12:11:06 +0000225 w.reset()
Benjamin Peterson712ee922008-08-24 18:10:20 +0000226 del obj[3:4]
227 self.assertWarning(None, w, expected.format('del'))
Nick Coghland2e09382008-09-11 12:11:06 +0000228 w.reset()
Benjamin Peterson712ee922008-08-24 18:10:20 +0000229 obj[4:5] = "eggs"
230 self.assertWarning(None, w, expected.format('set'))
231
Benjamin Petersonf4fcdb62008-06-08 23:00:00 +0000232 def test_tuple_parameter_unpacking(self):
233 expected = "tuple parameter unpacking has been removed in 3.x"
Florent Xicluna945a8ba2010-03-17 19:15:56 +0000234 with check_py3k_warnings((expected, SyntaxWarning)):
Benjamin Petersonf4fcdb62008-06-08 23:00:00 +0000235 exec "def f((a, b)): pass"
Benjamin Petersonf4fcdb62008-06-08 23:00:00 +0000236
Georg Brandl80055f62008-03-25 07:56:27 +0000237 def test_buffer(self):
Nick Coghlan48361f52008-08-11 15:45:58 +0000238 expected = 'buffer() not supported in 3.x'
Florent Xicluna945a8ba2010-03-17 19:15:56 +0000239 with check_py3k_warnings() as w:
Georg Brandl80055f62008-03-25 07:56:27 +0000240 self.assertWarning(buffer('a'), w, expected)
241
Georg Brandla9916b52008-05-17 22:11:54 +0000242 def test_file_xreadlines(self):
243 expected = ("f.xreadlines() not supported in 3.x, "
244 "try 'for line in f' instead")
245 with file(__file__) as f:
Florent Xicluna945a8ba2010-03-17 19:15:56 +0000246 with check_py3k_warnings() as w:
Georg Brandla9916b52008-05-17 22:11:54 +0000247 self.assertWarning(f.xreadlines(), w, expected)
248
Nick Coghlan48361f52008-08-11 15:45:58 +0000249 def test_hash_inheritance(self):
Florent Xicluna945a8ba2010-03-17 19:15:56 +0000250 with check_py3k_warnings() as w:
Nick Coghlan48361f52008-08-11 15:45:58 +0000251 # With object as the base class
252 class WarnOnlyCmp(object):
253 def __cmp__(self, other): pass
Nick Coghland2e09382008-09-11 12:11:06 +0000254 self.assertEqual(len(w.warnings), 1)
Nick Coghlan48361f52008-08-11 15:45:58 +0000255 self.assertWarning(None, w,
256 "Overriding __cmp__ blocks inheritance of __hash__ in 3.x")
Nick Coghland2e09382008-09-11 12:11:06 +0000257 w.reset()
Nick Coghlan48361f52008-08-11 15:45:58 +0000258 class WarnOnlyEq(object):
259 def __eq__(self, other): pass
Nick Coghland2e09382008-09-11 12:11:06 +0000260 self.assertEqual(len(w.warnings), 1)
Nick Coghlan48361f52008-08-11 15:45:58 +0000261 self.assertWarning(None, w,
262 "Overriding __eq__ blocks inheritance of __hash__ in 3.x")
Nick Coghland2e09382008-09-11 12:11:06 +0000263 w.reset()
Nick Coghlan48361f52008-08-11 15:45:58 +0000264 class WarnCmpAndEq(object):
265 def __cmp__(self, other): pass
266 def __eq__(self, other): pass
Nick Coghland2e09382008-09-11 12:11:06 +0000267 self.assertEqual(len(w.warnings), 2)
268 self.assertWarning(None, w.warnings[0],
Nick Coghlan48361f52008-08-11 15:45:58 +0000269 "Overriding __cmp__ blocks inheritance of __hash__ in 3.x")
270 self.assertWarning(None, w,
271 "Overriding __eq__ blocks inheritance of __hash__ in 3.x")
Nick Coghland2e09382008-09-11 12:11:06 +0000272 w.reset()
Nick Coghlan48361f52008-08-11 15:45:58 +0000273 class NoWarningOnlyHash(object):
274 def __hash__(self): pass
Nick Coghland2e09382008-09-11 12:11:06 +0000275 self.assertEqual(len(w.warnings), 0)
Nick Coghlan48361f52008-08-11 15:45:58 +0000276 # With an intermediate class in the heirarchy
277 class DefinesAllThree(object):
278 def __cmp__(self, other): pass
279 def __eq__(self, other): pass
280 def __hash__(self): pass
281 class WarnOnlyCmp(DefinesAllThree):
282 def __cmp__(self, other): pass
Nick Coghland2e09382008-09-11 12:11:06 +0000283 self.assertEqual(len(w.warnings), 1)
Nick Coghlan48361f52008-08-11 15:45:58 +0000284 self.assertWarning(None, w,
285 "Overriding __cmp__ blocks inheritance of __hash__ in 3.x")
Nick Coghland2e09382008-09-11 12:11:06 +0000286 w.reset()
Nick Coghlan48361f52008-08-11 15:45:58 +0000287 class WarnOnlyEq(DefinesAllThree):
288 def __eq__(self, other): pass
Nick Coghland2e09382008-09-11 12:11:06 +0000289 self.assertEqual(len(w.warnings), 1)
Nick Coghlan48361f52008-08-11 15:45:58 +0000290 self.assertWarning(None, w,
291 "Overriding __eq__ blocks inheritance of __hash__ in 3.x")
Nick Coghland2e09382008-09-11 12:11:06 +0000292 w.reset()
Nick Coghlan48361f52008-08-11 15:45:58 +0000293 class WarnCmpAndEq(DefinesAllThree):
294 def __cmp__(self, other): pass
295 def __eq__(self, other): pass
Nick Coghland2e09382008-09-11 12:11:06 +0000296 self.assertEqual(len(w.warnings), 2)
297 self.assertWarning(None, w.warnings[0],
Nick Coghlan48361f52008-08-11 15:45:58 +0000298 "Overriding __cmp__ blocks inheritance of __hash__ in 3.x")
299 self.assertWarning(None, w,
300 "Overriding __eq__ blocks inheritance of __hash__ in 3.x")
Nick Coghland2e09382008-09-11 12:11:06 +0000301 w.reset()
Nick Coghlan48361f52008-08-11 15:45:58 +0000302 class NoWarningOnlyHash(DefinesAllThree):
303 def __hash__(self): pass
Nick Coghland2e09382008-09-11 12:11:06 +0000304 self.assertEqual(len(w.warnings), 0)
Nick Coghlan48361f52008-08-11 15:45:58 +0000305
Alexandre Vassalotti0fe79912009-07-05 04:22:40 +0000306 def test_operator(self):
307 from operator import isCallable, sequenceIncludes
308
Alexandre Vassalotti16a02472009-07-05 04:25:46 +0000309 callable_warn = ("operator.isCallable() is not supported in 3.x. "
310 "Use hasattr(obj, '__call__').")
Alexandre Vassalotti0fe79912009-07-05 04:22:40 +0000311 seq_warn = ("operator.sequenceIncludes() is not supported "
312 "in 3.x. Use operator.contains().")
Florent Xicluna945a8ba2010-03-17 19:15:56 +0000313 with check_py3k_warnings() as w:
Alexandre Vassalotti0fe79912009-07-05 04:22:40 +0000314 self.assertWarning(isCallable(self), w, callable_warn)
315 w.reset()
316 self.assertWarning(sequenceIncludes(range(3), 2), w, seq_warn)
317
Georg Brandl07e56812008-03-21 20:21:46 +0000318
Brett Cannone5d2cba2008-05-06 23:23:34 +0000319class TestStdlibRemovals(unittest.TestCase):
320
Brett Cannon3c759142008-05-09 05:25:37 +0000321 # test.testall not tested as it executes all unit tests as an
322 # import side-effect.
Brett Cannon4c1f8812008-05-10 02:27:04 +0000323 all_platforms = ('audiodev', 'imputil', 'mutex', 'user', 'new', 'rexec',
Brett Cannon1e8fba72008-07-18 19:30:22 +0000324 'Bastion', 'compiler', 'dircache', 'mimetools',
325 'fpformat', 'ihooks', 'mhlib', 'statvfs', 'htmllib',
326 'sgmllib', 'rfc822', 'sunaudio')
Brett Cannon54c77aa2008-05-14 21:08:41 +0000327 inclusive_platforms = {'irix' : ('pure', 'AL', 'al', 'CD', 'cd', 'cddb',
Brett Cannon044616a2008-05-15 02:33:55 +0000328 'cdplayer', 'CL', 'cl', 'DEVICE', 'GL',
Brett Cannon75ba4652008-05-15 03:23:17 +0000329 'gl', 'ERRNO', 'FILE', 'FL', 'flp', 'fl',
Brett Cannond8c41ec2008-05-15 03:41:55 +0000330 'fm', 'GET', 'GLWS', 'imgfile', 'IN',
Brett Cannoncd2de082008-05-15 03:51:21 +0000331 'IOCTL', 'jpeg', 'panel', 'panelparser',
Brett Cannon74a596c2008-05-15 04:17:35 +0000332 'readcd', 'SV', 'torgb', 'WAIT'),
Benjamin Peterson23681932008-05-12 21:42:13 +0000333 'darwin' : ('autoGIL', 'Carbon', 'OSATerminology',
Brett Cannonea785fb2008-05-14 01:09:40 +0000334 'icglue', 'Nav', 'MacOS', 'aepack',
335 'aetools', 'aetypes', 'applesingle',
336 'appletrawmain', 'appletrunner',
337 'argvemulator', 'bgenlocations',
Benjamin Peterson23681932008-05-12 21:42:13 +0000338 'EasyDialogs', 'macerrors', 'macostools',
339 'findertools', 'FrameWork', 'ic',
340 'gensuitemodule', 'icopen', 'macresource',
341 'MiniAEFrame', 'pimp', 'PixMapWrapper',
Brett Cannonea785fb2008-05-14 01:09:40 +0000342 'terminalcommand', 'videoreader',
343 '_builtinSuites', 'CodeWarrior',
344 'Explorer', 'Finder', 'Netscape',
345 'StdSuites', 'SystemEvents', 'Terminal',
346 'cfmfile', 'bundlebuilder', 'buildtools',
Benjamin Petersona6864e02008-07-14 17:42:17 +0000347 'ColorPicker', 'Audio_mac'),
Brett Cannon22248172008-05-16 00:10:24 +0000348 'sunos5' : ('sunaudiodev', 'SUNAUDIODEV'),
349 }
Brett Cannonac861b52008-05-12 03:45:59 +0000350 optional_modules = ('bsddb185', 'Canvas', 'dl', 'linuxaudiodev', 'imageop',
Antoine Pitrou80157252010-01-08 19:20:17 +0000351 'sv', 'bsddb', 'dbhash')
Brett Cannone5d2cba2008-05-06 23:23:34 +0000352
Brett Cannon9ac39742008-05-09 22:51:58 +0000353 def check_removal(self, module_name, optional=False):
Brett Cannone5d2cba2008-05-06 23:23:34 +0000354 """Make sure the specified module, when imported, raises a
355 DeprecationWarning and specifies itself in the message."""
Florent Xicluna4d42f2b2010-03-09 19:57:01 +0000356 with CleanImport(module_name), warnings.catch_warnings():
357 warnings.filterwarnings("error", ".+ (module|package) .+ removed",
358 DeprecationWarning, __name__)
359 warnings.filterwarnings("error", ".+ removed .+ (module|package)",
Benjamin Petersona6864e02008-07-14 17:42:17 +0000360 DeprecationWarning, __name__)
361 try:
362 __import__(module_name, level=0)
363 except DeprecationWarning as exc:
Ezio Melottiaa980582010-01-23 23:04:36 +0000364 self.assertIn(module_name, exc.args[0],
365 "%s warning didn't contain module name"
366 % module_name)
Benjamin Petersona6864e02008-07-14 17:42:17 +0000367 except ImportError:
368 if not optional:
369 self.fail("Non-optional module {0} raised an "
370 "ImportError.".format(module_name))
371 else:
Florent Xicluna4d42f2b2010-03-09 19:57:01 +0000372 # For extension modules, check the __warningregistry__.
373 # They won't rerun their init code even with CleanImport.
374 if not check_deprecated_module(module_name):
375 self.fail("DeprecationWarning not raised for {0}"
376 .format(module_name))
Brett Cannone5d2cba2008-05-06 23:23:34 +0000377
378 def test_platform_independent_removals(self):
379 # Make sure that the modules that are available on all platforms raise
380 # the proper DeprecationWarning.
381 for module_name in self.all_platforms:
382 self.check_removal(module_name)
383
Brett Cannon9ac39742008-05-09 22:51:58 +0000384 def test_platform_specific_removals(self):
385 # Test the removal of platform-specific modules.
386 for module_name in self.inclusive_platforms.get(sys.platform, []):
387 self.check_removal(module_name, optional=True)
388
Brett Cannon768d44f2008-05-10 02:47:54 +0000389 def test_optional_module_removals(self):
390 # Test the removal of modules that may or may not be built.
391 for module_name in self.optional_modules:
392 self.check_removal(module_name, optional=True)
393
Benjamin Peterson0893a0a2008-05-09 00:27:01 +0000394 def test_os_path_walk(self):
395 msg = "In 3.x, os.path.walk is removed in favor of os.walk."
396 def dumbo(where, names, args): pass
397 for path_mod in ("ntpath", "macpath", "os2emxpath", "posixpath"):
398 mod = __import__(path_mod)
Nick Coghland2e09382008-09-11 12:11:06 +0000399 reset_module_registry(mod)
Florent Xicluna945a8ba2010-03-17 19:15:56 +0000400 with check_py3k_warnings() as w:
Benjamin Peterson1d310232008-05-27 01:42:29 +0000401 mod.walk("crashers", dumbo, None)
Nick Coghland2e09382008-09-11 12:11:06 +0000402 self.assertEquals(str(w.message), msg)
Benjamin Peterson0893a0a2008-05-09 00:27:01 +0000403
Benjamin Peterson541f7da2008-08-18 02:12:23 +0000404 def test_reduce_move(self):
405 from operator import add
Nick Coghland2e09382008-09-11 12:11:06 +0000406 # reduce tests may have already triggered this warning
Florent Xicluna4d42f2b2010-03-09 19:57:01 +0000407 reset_module_registry(unittest.case)
Brett Cannon672237d2008-09-09 00:49:16 +0000408 with warnings.catch_warnings():
Benjamin Peterson541f7da2008-08-18 02:12:23 +0000409 warnings.filterwarnings("error", "reduce")
410 self.assertRaises(DeprecationWarning, reduce, add, range(10))
411
Brett Cannonabb34fe2008-05-29 05:08:50 +0000412 def test_mutablestring_removal(self):
413 # UserString.MutableString has been removed in 3.0.
414 import UserString
Nick Coghland2e09382008-09-11 12:11:06 +0000415 # UserString tests may have already triggered this warning
416 reset_module_registry(UserString)
Brett Cannon672237d2008-09-09 00:49:16 +0000417 with warnings.catch_warnings():
Brett Cannonabb34fe2008-05-29 05:08:50 +0000418 warnings.filterwarnings("error", ".*MutableString",
419 DeprecationWarning)
420 self.assertRaises(DeprecationWarning, UserString.MutableString)
421
Brett Cannone5d2cba2008-05-06 23:23:34 +0000422
Steven Bethardae42f332008-03-18 17:26:10 +0000423def test_main():
Florent Xicluna4d42f2b2010-03-09 19:57:01 +0000424 run_unittest(TestPy3KWarnings,
425 TestStdlibRemovals)
Steven Bethardae42f332008-03-18 17:26:10 +0000426
427if __name__ == '__main__':
428 test_main()