blob: 8bb21bae89cc31fba2283623943fddcfa2b48deb [file] [log] [blame]
Steven Bethardae42f332008-03-18 17:26:10 +00001import unittest
Brett Cannon977eb022008-03-19 17:37:43 +00002import sys
Benjamin Petersonbec087f2009-03-26 21:10:30 +00003from test.test_support import check_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()'
Nick Coghland2e09382008-09-11 12:11:06 +000044 with check_warnings() as w:
Benjamin Peterson2fe3ef82008-06-08 02:05:33 +000045 exec "`2`" in {}
46 self.assertWarning(None, w, expected)
47
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):
51 exec s in {}
52 self.assertWarning(None, w, expected)
53 with check_warnings() as w:
54 check("def f((x)): pass")
55 check("def f((((x))), (y)): pass")
56 check("def f((x), (((y))), m=32): pass")
57 # Something like def f((a, (b))): pass will raise the tuple
58 # unpacking warning.
59
Benjamin Peterson399b1fe2008-10-25 02:53:28 +000060 def test_forbidden_names(self):
Benjamin Petersond5efd202008-06-08 22:52:37 +000061 # So we don't screw up our globals
62 def safe_exec(expr):
63 def f(**kwargs): pass
64 exec expr in {'f' : f}
65
Benjamin Peterson399b1fe2008-10-25 02:53:28 +000066 tests = [("True", "assignment to True or False is forbidden in 3.x"),
67 ("False", "assignment to True or False is forbidden in 3.x"),
68 ("nonlocal", "nonlocal is a keyword in 3.x")]
Nick Coghland2e09382008-09-11 12:11:06 +000069 with check_warnings() as w:
Benjamin Peterson399b1fe2008-10-25 02:53:28 +000070 for keyword, expected in tests:
71 safe_exec("{0} = False".format(keyword))
72 self.assertWarning(None, w, expected)
73 w.reset()
74 try:
75 safe_exec("obj.{0} = True".format(keyword))
76 except NameError:
77 pass
78 self.assertWarning(None, w, expected)
79 w.reset()
80 safe_exec("def {0}(): pass".format(keyword))
81 self.assertWarning(None, w, expected)
82 w.reset()
83 safe_exec("class {0}: pass".format(keyword))
84 self.assertWarning(None, w, expected)
85 w.reset()
86 safe_exec("def f({0}=43): pass".format(keyword))
87 self.assertWarning(None, w, expected)
88 w.reset()
Benjamin Petersond5efd202008-06-08 22:52:37 +000089
90
Steven Bethardae42f332008-03-18 17:26:10 +000091 def test_type_inequality_comparisons(self):
Georg Brandld5b635f2008-03-25 08:29:14 +000092 expected = 'type inequality comparisons not supported in 3.x'
Nick Coghland2e09382008-09-11 12:11:06 +000093 with check_warnings() as w:
Steven Bethardae42f332008-03-18 17:26:10 +000094 self.assertWarning(int < str, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +000095 w.reset()
Steven Bethardae42f332008-03-18 17:26:10 +000096 self.assertWarning(type < object, w, expected)
97
98 def test_object_inequality_comparisons(self):
Georg Brandld5b635f2008-03-25 08:29:14 +000099 expected = 'comparing unequal types not supported in 3.x'
Nick Coghland2e09382008-09-11 12:11:06 +0000100 with check_warnings() as w:
Steven Bethardae42f332008-03-18 17:26:10 +0000101 self.assertWarning(str < [], w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000102 w.reset()
Steven Bethardae42f332008-03-18 17:26:10 +0000103 self.assertWarning(object() < (1, 2), w, expected)
104
105 def test_dict_inequality_comparisons(self):
Georg Brandld5b635f2008-03-25 08:29:14 +0000106 expected = 'dict inequality comparisons not supported in 3.x'
Nick Coghland2e09382008-09-11 12:11:06 +0000107 with check_warnings() as w:
Steven Bethardae42f332008-03-18 17:26:10 +0000108 self.assertWarning({} < {2:3}, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000109 w.reset()
Steven Bethardae42f332008-03-18 17:26:10 +0000110 self.assertWarning({} <= {}, 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)
Nick Coghland2e09382008-09-11 12:11:06 +0000113 w.reset()
Steven Bethardae42f332008-03-18 17:26:10 +0000114 self.assertWarning({2:3} >= {}, w, expected)
115
116 def test_cell_inequality_comparisons(self):
Georg Brandld5b635f2008-03-25 08:29:14 +0000117 expected = 'cell comparisons not supported in 3.x'
Steven Bethardae42f332008-03-18 17:26:10 +0000118 def f(x):
119 def g():
120 return x
121 return g
122 cell0, = f(0).func_closure
123 cell1, = f(1).func_closure
Nick Coghland2e09382008-09-11 12:11:06 +0000124 with check_warnings() as w:
Steven Bethardae42f332008-03-18 17:26:10 +0000125 self.assertWarning(cell0 == cell1, w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000126 w.reset()
Steven Bethardae42f332008-03-18 17:26:10 +0000127 self.assertWarning(cell0 < cell1, w, expected)
128
Steven Bethard6a644f92008-03-18 22:08:20 +0000129 def test_code_inequality_comparisons(self):
Georg Brandld5b635f2008-03-25 08:29:14 +0000130 expected = 'code inequality comparisons not supported in 3.x'
Steven Bethard6a644f92008-03-18 22:08:20 +0000131 def f(x):
132 pass
133 def g(x):
134 pass
Nick Coghland2e09382008-09-11 12:11:06 +0000135 with check_warnings() as w:
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)
Nick Coghland2e09382008-09-11 12:11:06 +0000141 w.reset()
Steven Bethard6a644f92008-03-18 22:08:20 +0000142 self.assertWarning(f.func_code > g.func_code, w, expected)
143
144 def test_builtin_function_or_method_comparisons(self):
145 expected = ('builtin_function_or_method '
Benjamin Peterson1bf47652009-07-02 17:06:17 +0000146 'order comparisons not supported in 3.x')
Steven Bethard6a644f92008-03-18 22:08:20 +0000147 func = eval
148 meth = {}.get
Nick Coghland2e09382008-09-11 12:11:06 +0000149 with check_warnings() as w:
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(func > meth, 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)
Nick Coghland2e09382008-09-11 12:11:06 +0000155 w.reset()
Steven Bethard6a644f92008-03-18 22:08:20 +0000156 self.assertWarning(meth >= func, w, expected)
Benjamin Peterson1bf47652009-07-02 17:06:17 +0000157 w.reset()
158 self.assertNoWarning(meth == func, w)
159 self.assertNoWarning(meth != func, w)
160 lam = lambda x: x
161 self.assertNoWarning(lam == func, w)
162 self.assertNoWarning(lam != func, w)
Steven Bethard6a644f92008-03-18 22:08:20 +0000163
Benjamin Petersonf09925d2008-12-22 20:16:25 +0000164 def test_frame_attributes(self):
165 template = "%s has been removed in 3.x"
166 f = sys._getframe(0)
167 for attr in ("f_exc_traceback", "f_exc_value", "f_exc_type"):
168 expected = template % attr
169 with check_warnings() as w:
170 self.assertWarning(getattr(f, attr), w, expected)
171 w.reset()
172 self.assertWarning(setattr(f, attr, None), w, expected)
173
Raymond Hettinger05387862008-03-19 17:45:19 +0000174 def test_sort_cmp_arg(self):
Georg Brandld5b635f2008-03-25 08:29:14 +0000175 expected = "the cmp argument is not supported in 3.x"
Raymond Hettinger05387862008-03-19 17:45:19 +0000176 lst = range(5)
177 cmp = lambda x,y: -1
178
Nick Coghland2e09382008-09-11 12:11:06 +0000179 with check_warnings() as w:
Raymond Hettinger05387862008-03-19 17:45:19 +0000180 self.assertWarning(lst.sort(cmp=cmp), w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000181 w.reset()
Raymond Hettinger05387862008-03-19 17:45:19 +0000182 self.assertWarning(sorted(lst, cmp=cmp), w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000183 w.reset()
Raymond Hettinger05387862008-03-19 17:45:19 +0000184 self.assertWarning(lst.sort(cmp), w, expected)
Nick Coghland2e09382008-09-11 12:11:06 +0000185 w.reset()
Raymond Hettinger05387862008-03-19 17:45:19 +0000186 self.assertWarning(sorted(lst, cmp), w, expected)
187
Georg Brandl5a444242008-03-21 20:11:46 +0000188 def test_sys_exc_clear(self):
Georg Brandld5b635f2008-03-25 08:29:14 +0000189 expected = 'sys.exc_clear() not supported in 3.x; use except clauses'
Nick Coghland2e09382008-09-11 12:11:06 +0000190 with check_warnings() as w:
Georg Brandl5a444242008-03-21 20:11:46 +0000191 self.assertWarning(sys.exc_clear(), w, expected)
192
Georg Brandl07e56812008-03-21 20:21:46 +0000193 def test_methods_members(self):
194 expected = '__members__ and __methods__ not supported in 3.x'
195 class C:
196 __methods__ = ['a']
197 __members__ = ['b']
198 c = C()
Nick Coghland2e09382008-09-11 12:11:06 +0000199 with check_warnings() as w:
Georg Brandl07e56812008-03-21 20:21:46 +0000200 self.assertWarning(dir(c), w, expected)
201
Georg Brandl65bb42d2008-03-21 20:38:24 +0000202 def test_softspace(self):
203 expected = 'file.softspace not supported in 3.x'
204 with file(__file__) as f:
Nick Coghland2e09382008-09-11 12:11:06 +0000205 with check_warnings() as w:
Georg Brandl65bb42d2008-03-21 20:38:24 +0000206 self.assertWarning(f.softspace, w, expected)
207 def set():
208 f.softspace = 0
Nick Coghland2e09382008-09-11 12:11:06 +0000209 with check_warnings() as w:
Georg Brandl65bb42d2008-03-21 20:38:24 +0000210 self.assertWarning(set(), w, expected)
211
Benjamin Peterson712ee922008-08-24 18:10:20 +0000212 def test_slice_methods(self):
213 class Spam(object):
214 def __getslice__(self, i, j): pass
215 def __setslice__(self, i, j, what): pass
216 def __delslice__(self, i, j): pass
217 class Egg:
218 def __getslice__(self, i, h): pass
219 def __setslice__(self, i, j, what): pass
220 def __delslice__(self, i, j): pass
221
222 expected = "in 3.x, __{0}slice__ has been removed; use __{0}item__"
223
224 for obj in (Spam(), Egg()):
Nick Coghland2e09382008-09-11 12:11:06 +0000225 with check_warnings() as w:
Benjamin Peterson712ee922008-08-24 18:10:20 +0000226 self.assertWarning(obj[1:2], w, expected.format('get'))
Nick Coghland2e09382008-09-11 12:11:06 +0000227 w.reset()
Benjamin Peterson712ee922008-08-24 18:10:20 +0000228 del obj[3:4]
229 self.assertWarning(None, w, expected.format('del'))
Nick Coghland2e09382008-09-11 12:11:06 +0000230 w.reset()
Benjamin Peterson712ee922008-08-24 18:10:20 +0000231 obj[4:5] = "eggs"
232 self.assertWarning(None, w, expected.format('set'))
233
Benjamin Petersonf4fcdb62008-06-08 23:00:00 +0000234 def test_tuple_parameter_unpacking(self):
235 expected = "tuple parameter unpacking has been removed in 3.x"
Nick Coghland2e09382008-09-11 12:11:06 +0000236 with check_warnings() as w:
Benjamin Petersonf4fcdb62008-06-08 23:00:00 +0000237 exec "def f((a, b)): pass"
238 self.assertWarning(None, w, expected)
239
Georg Brandl80055f62008-03-25 07:56:27 +0000240 def test_buffer(self):
Nick Coghlan48361f52008-08-11 15:45:58 +0000241 expected = 'buffer() not supported in 3.x'
Nick Coghland2e09382008-09-11 12:11:06 +0000242 with check_warnings() as w:
Georg Brandl80055f62008-03-25 07:56:27 +0000243 self.assertWarning(buffer('a'), w, expected)
244
Georg Brandla9916b52008-05-17 22:11:54 +0000245 def test_file_xreadlines(self):
246 expected = ("f.xreadlines() not supported in 3.x, "
247 "try 'for line in f' instead")
248 with file(__file__) as f:
Nick Coghland2e09382008-09-11 12:11:06 +0000249 with check_warnings() as w:
Georg Brandla9916b52008-05-17 22:11:54 +0000250 self.assertWarning(f.xreadlines(), w, expected)
251
Nick Coghlan48361f52008-08-11 15:45:58 +0000252 def test_hash_inheritance(self):
Nick Coghland2e09382008-09-11 12:11:06 +0000253 with check_warnings() as w:
Nick Coghlan48361f52008-08-11 15:45:58 +0000254 # With object as the base class
255 class WarnOnlyCmp(object):
256 def __cmp__(self, other): pass
Nick Coghland2e09382008-09-11 12:11:06 +0000257 self.assertEqual(len(w.warnings), 1)
Nick Coghlan48361f52008-08-11 15:45:58 +0000258 self.assertWarning(None, w,
259 "Overriding __cmp__ blocks inheritance of __hash__ in 3.x")
Nick Coghland2e09382008-09-11 12:11:06 +0000260 w.reset()
Nick Coghlan48361f52008-08-11 15:45:58 +0000261 class WarnOnlyEq(object):
262 def __eq__(self, other): pass
Nick Coghland2e09382008-09-11 12:11:06 +0000263 self.assertEqual(len(w.warnings), 1)
Nick Coghlan48361f52008-08-11 15:45:58 +0000264 self.assertWarning(None, w,
265 "Overriding __eq__ blocks inheritance of __hash__ in 3.x")
Nick Coghland2e09382008-09-11 12:11:06 +0000266 w.reset()
Nick Coghlan48361f52008-08-11 15:45:58 +0000267 class WarnCmpAndEq(object):
268 def __cmp__(self, other): pass
269 def __eq__(self, other): pass
Nick Coghland2e09382008-09-11 12:11:06 +0000270 self.assertEqual(len(w.warnings), 2)
271 self.assertWarning(None, w.warnings[0],
Nick Coghlan48361f52008-08-11 15:45:58 +0000272 "Overriding __cmp__ blocks inheritance of __hash__ in 3.x")
273 self.assertWarning(None, w,
274 "Overriding __eq__ blocks inheritance of __hash__ in 3.x")
Nick Coghland2e09382008-09-11 12:11:06 +0000275 w.reset()
Nick Coghlan48361f52008-08-11 15:45:58 +0000276 class NoWarningOnlyHash(object):
277 def __hash__(self): pass
Nick Coghland2e09382008-09-11 12:11:06 +0000278 self.assertEqual(len(w.warnings), 0)
Nick Coghlan48361f52008-08-11 15:45:58 +0000279 # With an intermediate class in the heirarchy
280 class DefinesAllThree(object):
281 def __cmp__(self, other): pass
282 def __eq__(self, other): pass
283 def __hash__(self): pass
284 class WarnOnlyCmp(DefinesAllThree):
285 def __cmp__(self, other): pass
Nick Coghland2e09382008-09-11 12:11:06 +0000286 self.assertEqual(len(w.warnings), 1)
Nick Coghlan48361f52008-08-11 15:45:58 +0000287 self.assertWarning(None, w,
288 "Overriding __cmp__ blocks inheritance of __hash__ in 3.x")
Nick Coghland2e09382008-09-11 12:11:06 +0000289 w.reset()
Nick Coghlan48361f52008-08-11 15:45:58 +0000290 class WarnOnlyEq(DefinesAllThree):
291 def __eq__(self, other): pass
Nick Coghland2e09382008-09-11 12:11:06 +0000292 self.assertEqual(len(w.warnings), 1)
Nick Coghlan48361f52008-08-11 15:45:58 +0000293 self.assertWarning(None, w,
294 "Overriding __eq__ blocks inheritance of __hash__ in 3.x")
Nick Coghland2e09382008-09-11 12:11:06 +0000295 w.reset()
Nick Coghlan48361f52008-08-11 15:45:58 +0000296 class WarnCmpAndEq(DefinesAllThree):
297 def __cmp__(self, other): pass
298 def __eq__(self, other): pass
Nick Coghland2e09382008-09-11 12:11:06 +0000299 self.assertEqual(len(w.warnings), 2)
300 self.assertWarning(None, w.warnings[0],
Nick Coghlan48361f52008-08-11 15:45:58 +0000301 "Overriding __cmp__ blocks inheritance of __hash__ in 3.x")
302 self.assertWarning(None, w,
303 "Overriding __eq__ blocks inheritance of __hash__ in 3.x")
Nick Coghland2e09382008-09-11 12:11:06 +0000304 w.reset()
Nick Coghlan48361f52008-08-11 15:45:58 +0000305 class NoWarningOnlyHash(DefinesAllThree):
306 def __hash__(self): pass
Nick Coghland2e09382008-09-11 12:11:06 +0000307 self.assertEqual(len(w.warnings), 0)
Nick Coghlan48361f52008-08-11 15:45:58 +0000308
Alexandre Vassalotti0fe79912009-07-05 04:22:40 +0000309 def test_operator(self):
310 from operator import isCallable, sequenceIncludes
311
Alexandre Vassalotti16a02472009-07-05 04:25:46 +0000312 callable_warn = ("operator.isCallable() is not supported in 3.x. "
313 "Use hasattr(obj, '__call__').")
Alexandre Vassalotti0fe79912009-07-05 04:22:40 +0000314 seq_warn = ("operator.sequenceIncludes() is not supported "
315 "in 3.x. Use operator.contains().")
316 with check_warnings() as w:
317 self.assertWarning(isCallable(self), w, callable_warn)
318 w.reset()
319 self.assertWarning(sequenceIncludes(range(3), 2), w, seq_warn)
320
Georg Brandl07e56812008-03-21 20:21:46 +0000321
Brett Cannone5d2cba2008-05-06 23:23:34 +0000322class TestStdlibRemovals(unittest.TestCase):
323
Brett Cannon3c759142008-05-09 05:25:37 +0000324 # test.testall not tested as it executes all unit tests as an
325 # import side-effect.
Brett Cannon4c1f8812008-05-10 02:27:04 +0000326 all_platforms = ('audiodev', 'imputil', 'mutex', 'user', 'new', 'rexec',
Brett Cannon1e8fba72008-07-18 19:30:22 +0000327 'Bastion', 'compiler', 'dircache', 'mimetools',
328 'fpformat', 'ihooks', 'mhlib', 'statvfs', 'htmllib',
329 'sgmllib', 'rfc822', 'sunaudio')
Brett Cannon54c77aa2008-05-14 21:08:41 +0000330 inclusive_platforms = {'irix' : ('pure', 'AL', 'al', 'CD', 'cd', 'cddb',
Brett Cannon044616a2008-05-15 02:33:55 +0000331 'cdplayer', 'CL', 'cl', 'DEVICE', 'GL',
Brett Cannon75ba4652008-05-15 03:23:17 +0000332 'gl', 'ERRNO', 'FILE', 'FL', 'flp', 'fl',
Brett Cannond8c41ec2008-05-15 03:41:55 +0000333 'fm', 'GET', 'GLWS', 'imgfile', 'IN',
Brett Cannoncd2de082008-05-15 03:51:21 +0000334 'IOCTL', 'jpeg', 'panel', 'panelparser',
Brett Cannon74a596c2008-05-15 04:17:35 +0000335 'readcd', 'SV', 'torgb', 'WAIT'),
Benjamin Peterson23681932008-05-12 21:42:13 +0000336 'darwin' : ('autoGIL', 'Carbon', 'OSATerminology',
Brett Cannonea785fb2008-05-14 01:09:40 +0000337 'icglue', 'Nav', 'MacOS', 'aepack',
338 'aetools', 'aetypes', 'applesingle',
339 'appletrawmain', 'appletrunner',
340 'argvemulator', 'bgenlocations',
Benjamin Peterson23681932008-05-12 21:42:13 +0000341 'EasyDialogs', 'macerrors', 'macostools',
342 'findertools', 'FrameWork', 'ic',
343 'gensuitemodule', 'icopen', 'macresource',
344 'MiniAEFrame', 'pimp', 'PixMapWrapper',
Brett Cannonea785fb2008-05-14 01:09:40 +0000345 'terminalcommand', 'videoreader',
346 '_builtinSuites', 'CodeWarrior',
347 'Explorer', 'Finder', 'Netscape',
348 'StdSuites', 'SystemEvents', 'Terminal',
349 'cfmfile', 'bundlebuilder', 'buildtools',
Benjamin Petersona6864e02008-07-14 17:42:17 +0000350 'ColorPicker', 'Audio_mac'),
Brett Cannon22248172008-05-16 00:10:24 +0000351 'sunos5' : ('sunaudiodev', 'SUNAUDIODEV'),
352 }
Brett Cannonac861b52008-05-12 03:45:59 +0000353 optional_modules = ('bsddb185', 'Canvas', 'dl', 'linuxaudiodev', 'imageop',
Antoine Pitrou80157252010-01-08 19:20:17 +0000354 'sv', 'bsddb', 'dbhash')
Brett Cannone5d2cba2008-05-06 23:23:34 +0000355
Brett Cannon9ac39742008-05-09 22:51:58 +0000356 def check_removal(self, module_name, optional=False):
Brett Cannone5d2cba2008-05-06 23:23:34 +0000357 """Make sure the specified module, when imported, raises a
358 DeprecationWarning and specifies itself in the message."""
Florent Xicluna4d42f2b2010-03-09 19:57:01 +0000359 with CleanImport(module_name), warnings.catch_warnings():
360 warnings.filterwarnings("error", ".+ (module|package) .+ removed",
361 DeprecationWarning, __name__)
362 warnings.filterwarnings("error", ".+ removed .+ (module|package)",
Benjamin Petersona6864e02008-07-14 17:42:17 +0000363 DeprecationWarning, __name__)
364 try:
365 __import__(module_name, level=0)
366 except DeprecationWarning as exc:
Ezio Melottiaa980582010-01-23 23:04:36 +0000367 self.assertIn(module_name, exc.args[0],
368 "%s warning didn't contain module name"
369 % module_name)
Benjamin Petersona6864e02008-07-14 17:42:17 +0000370 except ImportError:
371 if not optional:
372 self.fail("Non-optional module {0} raised an "
373 "ImportError.".format(module_name))
374 else:
Florent Xicluna4d42f2b2010-03-09 19:57:01 +0000375 # For extension modules, check the __warningregistry__.
376 # They won't rerun their init code even with CleanImport.
377 if not check_deprecated_module(module_name):
378 self.fail("DeprecationWarning not raised for {0}"
379 .format(module_name))
Brett Cannone5d2cba2008-05-06 23:23:34 +0000380
381 def test_platform_independent_removals(self):
382 # Make sure that the modules that are available on all platforms raise
383 # the proper DeprecationWarning.
384 for module_name in self.all_platforms:
385 self.check_removal(module_name)
386
Brett Cannon9ac39742008-05-09 22:51:58 +0000387 def test_platform_specific_removals(self):
388 # Test the removal of platform-specific modules.
389 for module_name in self.inclusive_platforms.get(sys.platform, []):
390 self.check_removal(module_name, optional=True)
391
Brett Cannon768d44f2008-05-10 02:47:54 +0000392 def test_optional_module_removals(self):
393 # Test the removal of modules that may or may not be built.
394 for module_name in self.optional_modules:
395 self.check_removal(module_name, optional=True)
396
Benjamin Peterson0893a0a2008-05-09 00:27:01 +0000397 def test_os_path_walk(self):
398 msg = "In 3.x, os.path.walk is removed in favor of os.walk."
399 def dumbo(where, names, args): pass
400 for path_mod in ("ntpath", "macpath", "os2emxpath", "posixpath"):
401 mod = __import__(path_mod)
Nick Coghland2e09382008-09-11 12:11:06 +0000402 reset_module_registry(mod)
403 with check_warnings() as w:
Benjamin Peterson1d310232008-05-27 01:42:29 +0000404 mod.walk("crashers", dumbo, None)
Nick Coghland2e09382008-09-11 12:11:06 +0000405 self.assertEquals(str(w.message), msg)
Benjamin Peterson0893a0a2008-05-09 00:27:01 +0000406
Benjamin Peterson541f7da2008-08-18 02:12:23 +0000407 def test_reduce_move(self):
408 from operator import add
Nick Coghland2e09382008-09-11 12:11:06 +0000409 # reduce tests may have already triggered this warning
Florent Xicluna4d42f2b2010-03-09 19:57:01 +0000410 reset_module_registry(unittest.case)
Brett Cannon672237d2008-09-09 00:49:16 +0000411 with warnings.catch_warnings():
Benjamin Peterson541f7da2008-08-18 02:12:23 +0000412 warnings.filterwarnings("error", "reduce")
413 self.assertRaises(DeprecationWarning, reduce, add, range(10))
414
Brett Cannonabb34fe2008-05-29 05:08:50 +0000415 def test_mutablestring_removal(self):
416 # UserString.MutableString has been removed in 3.0.
417 import UserString
Nick Coghland2e09382008-09-11 12:11:06 +0000418 # UserString tests may have already triggered this warning
419 reset_module_registry(UserString)
Brett Cannon672237d2008-09-09 00:49:16 +0000420 with warnings.catch_warnings():
Brett Cannonabb34fe2008-05-29 05:08:50 +0000421 warnings.filterwarnings("error", ".*MutableString",
422 DeprecationWarning)
423 self.assertRaises(DeprecationWarning, UserString.MutableString)
424
Brett Cannone5d2cba2008-05-06 23:23:34 +0000425
Steven Bethardae42f332008-03-18 17:26:10 +0000426def test_main():
Florent Xicluna4d42f2b2010-03-09 19:57:01 +0000427 run_unittest(TestPy3KWarnings,
428 TestStdlibRemovals)
Steven Bethardae42f332008-03-18 17:26:10 +0000429
430if __name__ == '__main__':
431 test_main()