]>
Commit | Line | Data |
---|---|---|
4710c53d | 1 | import unittest\r |
2 | import sys\r | |
3 | from test.test_support import check_py3k_warnings, CleanImport, run_unittest\r | |
4 | import warnings\r | |
5 | \r | |
6 | if not sys.py3kwarning:\r | |
7 | raise unittest.SkipTest('%s must be run with the -3 flag' % __name__)\r | |
8 | \r | |
9 | try:\r | |
10 | from test.test_support import __warningregistry__ as _registry\r | |
11 | except ImportError:\r | |
12 | def check_deprecated_module(module_name):\r | |
13 | return False\r | |
14 | else:\r | |
15 | past_warnings = _registry.keys()\r | |
16 | del _registry\r | |
17 | def check_deprecated_module(module_name):\r | |
18 | """Lookup the past warnings for module already loaded using\r | |
19 | test_support.import_module(..., deprecated=True)\r | |
20 | """\r | |
21 | return any(module_name in msg and ' removed' in msg\r | |
22 | and issubclass(cls, DeprecationWarning)\r | |
23 | and (' module' in msg or ' package' in msg)\r | |
24 | for (msg, cls, line) in past_warnings)\r | |
25 | \r | |
26 | def reset_module_registry(module):\r | |
27 | try:\r | |
28 | registry = module.__warningregistry__\r | |
29 | except AttributeError:\r | |
30 | pass\r | |
31 | else:\r | |
32 | registry.clear()\r | |
33 | \r | |
34 | class TestPy3KWarnings(unittest.TestCase):\r | |
35 | \r | |
36 | def assertWarning(self, _, warning, expected_message):\r | |
37 | self.assertEqual(str(warning.message), expected_message)\r | |
38 | \r | |
39 | def assertNoWarning(self, _, recorder):\r | |
40 | self.assertEqual(len(recorder.warnings), 0)\r | |
41 | \r | |
42 | def test_backquote(self):\r | |
43 | expected = 'backquote not supported in 3.x; use repr()'\r | |
44 | with check_py3k_warnings((expected, SyntaxWarning)):\r | |
45 | exec "`2`" in {}\r | |
46 | \r | |
47 | def test_paren_arg_names(self):\r | |
48 | expected = 'parenthesized argument names are invalid in 3.x'\r | |
49 | def check(s):\r | |
50 | with check_py3k_warnings((expected, SyntaxWarning)):\r | |
51 | exec s in {}\r | |
52 | check("def f((x)): pass")\r | |
53 | check("def f((((x))), (y)): pass")\r | |
54 | check("def f((x), (((y))), m=32): pass")\r | |
55 | # Something like def f((a, (b))): pass will raise the tuple\r | |
56 | # unpacking warning.\r | |
57 | \r | |
58 | def test_forbidden_names(self):\r | |
59 | # So we don't screw up our globals\r | |
60 | def safe_exec(expr):\r | |
61 | def f(**kwargs): pass\r | |
62 | exec expr in {'f' : f}\r | |
63 | \r | |
64 | tests = [("True", "assignment to True or False is forbidden in 3.x"),\r | |
65 | ("False", "assignment to True or False is forbidden in 3.x"),\r | |
66 | ("nonlocal", "nonlocal is a keyword in 3.x")]\r | |
67 | with check_py3k_warnings(('', SyntaxWarning)) as w:\r | |
68 | for keyword, expected in tests:\r | |
69 | safe_exec("{0} = False".format(keyword))\r | |
70 | self.assertWarning(None, w, expected)\r | |
71 | w.reset()\r | |
72 | try:\r | |
73 | safe_exec("obj.{0} = True".format(keyword))\r | |
74 | except NameError:\r | |
75 | pass\r | |
76 | self.assertWarning(None, w, expected)\r | |
77 | w.reset()\r | |
78 | safe_exec("def {0}(): pass".format(keyword))\r | |
79 | self.assertWarning(None, w, expected)\r | |
80 | w.reset()\r | |
81 | safe_exec("class {0}: pass".format(keyword))\r | |
82 | self.assertWarning(None, w, expected)\r | |
83 | w.reset()\r | |
84 | safe_exec("def f({0}=43): pass".format(keyword))\r | |
85 | self.assertWarning(None, w, expected)\r | |
86 | w.reset()\r | |
87 | \r | |
88 | \r | |
89 | def test_type_inequality_comparisons(self):\r | |
90 | expected = 'type inequality comparisons not supported in 3.x'\r | |
91 | with check_py3k_warnings() as w:\r | |
92 | self.assertWarning(int < str, w, expected)\r | |
93 | w.reset()\r | |
94 | self.assertWarning(type < object, w, expected)\r | |
95 | \r | |
96 | def test_object_inequality_comparisons(self):\r | |
97 | expected = 'comparing unequal types not supported in 3.x'\r | |
98 | with check_py3k_warnings() as w:\r | |
99 | self.assertWarning(str < [], w, expected)\r | |
100 | w.reset()\r | |
101 | self.assertWarning(object() < (1, 2), w, expected)\r | |
102 | \r | |
103 | def test_dict_inequality_comparisons(self):\r | |
104 | expected = 'dict inequality comparisons not supported in 3.x'\r | |
105 | with check_py3k_warnings() as w:\r | |
106 | self.assertWarning({} < {2:3}, w, expected)\r | |
107 | w.reset()\r | |
108 | self.assertWarning({} <= {}, w, expected)\r | |
109 | w.reset()\r | |
110 | self.assertWarning({} > {2:3}, w, expected)\r | |
111 | w.reset()\r | |
112 | self.assertWarning({2:3} >= {}, w, expected)\r | |
113 | \r | |
114 | def test_cell_inequality_comparisons(self):\r | |
115 | expected = 'cell comparisons not supported in 3.x'\r | |
116 | def f(x):\r | |
117 | def g():\r | |
118 | return x\r | |
119 | return g\r | |
120 | cell0, = f(0).func_closure\r | |
121 | cell1, = f(1).func_closure\r | |
122 | with check_py3k_warnings() as w:\r | |
123 | self.assertWarning(cell0 == cell1, w, expected)\r | |
124 | w.reset()\r | |
125 | self.assertWarning(cell0 < cell1, w, expected)\r | |
126 | \r | |
127 | def test_code_inequality_comparisons(self):\r | |
128 | expected = 'code inequality comparisons not supported in 3.x'\r | |
129 | def f(x):\r | |
130 | pass\r | |
131 | def g(x):\r | |
132 | pass\r | |
133 | with check_py3k_warnings() as w:\r | |
134 | self.assertWarning(f.func_code < g.func_code, w, expected)\r | |
135 | w.reset()\r | |
136 | self.assertWarning(f.func_code <= g.func_code, w, expected)\r | |
137 | w.reset()\r | |
138 | self.assertWarning(f.func_code >= g.func_code, w, expected)\r | |
139 | w.reset()\r | |
140 | self.assertWarning(f.func_code > g.func_code, w, expected)\r | |
141 | \r | |
142 | def test_builtin_function_or_method_comparisons(self):\r | |
143 | expected = ('builtin_function_or_method '\r | |
144 | 'order comparisons not supported in 3.x')\r | |
145 | func = eval\r | |
146 | meth = {}.get\r | |
147 | with check_py3k_warnings() as w:\r | |
148 | self.assertWarning(func < meth, w, expected)\r | |
149 | w.reset()\r | |
150 | self.assertWarning(func > meth, w, expected)\r | |
151 | w.reset()\r | |
152 | self.assertWarning(meth <= func, w, expected)\r | |
153 | w.reset()\r | |
154 | self.assertWarning(meth >= func, w, expected)\r | |
155 | w.reset()\r | |
156 | self.assertNoWarning(meth == func, w)\r | |
157 | self.assertNoWarning(meth != func, w)\r | |
158 | lam = lambda x: x\r | |
159 | self.assertNoWarning(lam == func, w)\r | |
160 | self.assertNoWarning(lam != func, w)\r | |
161 | \r | |
162 | def test_frame_attributes(self):\r | |
163 | template = "%s has been removed in 3.x"\r | |
164 | f = sys._getframe(0)\r | |
165 | for attr in ("f_exc_traceback", "f_exc_value", "f_exc_type"):\r | |
166 | expected = template % attr\r | |
167 | with check_py3k_warnings() as w:\r | |
168 | self.assertWarning(getattr(f, attr), w, expected)\r | |
169 | w.reset()\r | |
170 | self.assertWarning(setattr(f, attr, None), w, expected)\r | |
171 | \r | |
172 | def test_sort_cmp_arg(self):\r | |
173 | expected = "the cmp argument is not supported in 3.x"\r | |
174 | lst = range(5)\r | |
175 | cmp = lambda x,y: -1\r | |
176 | \r | |
177 | with check_py3k_warnings() as w:\r | |
178 | self.assertWarning(lst.sort(cmp=cmp), w, expected)\r | |
179 | w.reset()\r | |
180 | self.assertWarning(sorted(lst, cmp=cmp), w, expected)\r | |
181 | w.reset()\r | |
182 | self.assertWarning(lst.sort(cmp), w, expected)\r | |
183 | w.reset()\r | |
184 | self.assertWarning(sorted(lst, cmp), w, expected)\r | |
185 | \r | |
186 | def test_sys_exc_clear(self):\r | |
187 | expected = 'sys.exc_clear() not supported in 3.x; use except clauses'\r | |
188 | with check_py3k_warnings() as w:\r | |
189 | self.assertWarning(sys.exc_clear(), w, expected)\r | |
190 | \r | |
191 | def test_methods_members(self):\r | |
192 | expected = '__members__ and __methods__ not supported in 3.x'\r | |
193 | class C:\r | |
194 | __methods__ = ['a']\r | |
195 | __members__ = ['b']\r | |
196 | c = C()\r | |
197 | with check_py3k_warnings() as w:\r | |
198 | self.assertWarning(dir(c), w, expected)\r | |
199 | \r | |
200 | def test_softspace(self):\r | |
201 | expected = 'file.softspace not supported in 3.x'\r | |
202 | with file(__file__) as f:\r | |
203 | with check_py3k_warnings() as w:\r | |
204 | self.assertWarning(f.softspace, w, expected)\r | |
205 | def set():\r | |
206 | f.softspace = 0\r | |
207 | with check_py3k_warnings() as w:\r | |
208 | self.assertWarning(set(), w, expected)\r | |
209 | \r | |
210 | def test_slice_methods(self):\r | |
211 | class Spam(object):\r | |
212 | def __getslice__(self, i, j): pass\r | |
213 | def __setslice__(self, i, j, what): pass\r | |
214 | def __delslice__(self, i, j): pass\r | |
215 | class Egg:\r | |
216 | def __getslice__(self, i, h): pass\r | |
217 | def __setslice__(self, i, j, what): pass\r | |
218 | def __delslice__(self, i, j): pass\r | |
219 | \r | |
220 | expected = "in 3.x, __{0}slice__ has been removed; use __{0}item__"\r | |
221 | \r | |
222 | for obj in (Spam(), Egg()):\r | |
223 | with check_py3k_warnings() as w:\r | |
224 | self.assertWarning(obj[1:2], w, expected.format('get'))\r | |
225 | w.reset()\r | |
226 | del obj[3:4]\r | |
227 | self.assertWarning(None, w, expected.format('del'))\r | |
228 | w.reset()\r | |
229 | obj[4:5] = "eggs"\r | |
230 | self.assertWarning(None, w, expected.format('set'))\r | |
231 | \r | |
232 | def test_tuple_parameter_unpacking(self):\r | |
233 | expected = "tuple parameter unpacking has been removed in 3.x"\r | |
234 | with check_py3k_warnings((expected, SyntaxWarning)):\r | |
235 | exec "def f((a, b)): pass"\r | |
236 | \r | |
237 | def test_buffer(self):\r | |
238 | expected = 'buffer() not supported in 3.x'\r | |
239 | with check_py3k_warnings() as w:\r | |
240 | self.assertWarning(buffer('a'), w, expected)\r | |
241 | \r | |
242 | def test_file_xreadlines(self):\r | |
243 | expected = ("f.xreadlines() not supported in 3.x, "\r | |
244 | "try 'for line in f' instead")\r | |
245 | with file(__file__) as f:\r | |
246 | with check_py3k_warnings() as w:\r | |
247 | self.assertWarning(f.xreadlines(), w, expected)\r | |
248 | \r | |
249 | def test_hash_inheritance(self):\r | |
250 | with check_py3k_warnings() as w:\r | |
251 | # With object as the base class\r | |
252 | class WarnOnlyCmp(object):\r | |
253 | def __cmp__(self, other): pass\r | |
254 | self.assertEqual(len(w.warnings), 0)\r | |
255 | w.reset()\r | |
256 | class WarnOnlyEq(object):\r | |
257 | def __eq__(self, other): pass\r | |
258 | self.assertEqual(len(w.warnings), 1)\r | |
259 | self.assertWarning(None, w,\r | |
260 | "Overriding __eq__ blocks inheritance of __hash__ in 3.x")\r | |
261 | w.reset()\r | |
262 | class WarnCmpAndEq(object):\r | |
263 | def __cmp__(self, other): pass\r | |
264 | def __eq__(self, other): pass\r | |
265 | self.assertEqual(len(w.warnings), 1)\r | |
266 | self.assertWarning(None, w,\r | |
267 | "Overriding __eq__ blocks inheritance of __hash__ in 3.x")\r | |
268 | w.reset()\r | |
269 | class NoWarningOnlyHash(object):\r | |
270 | def __hash__(self): pass\r | |
271 | self.assertEqual(len(w.warnings), 0)\r | |
272 | # With an intermediate class in the heirarchy\r | |
273 | class DefinesAllThree(object):\r | |
274 | def __cmp__(self, other): pass\r | |
275 | def __eq__(self, other): pass\r | |
276 | def __hash__(self): pass\r | |
277 | class WarnOnlyCmp(DefinesAllThree):\r | |
278 | def __cmp__(self, other): pass\r | |
279 | self.assertEqual(len(w.warnings), 0)\r | |
280 | w.reset()\r | |
281 | class WarnOnlyEq(DefinesAllThree):\r | |
282 | def __eq__(self, other): pass\r | |
283 | self.assertEqual(len(w.warnings), 1)\r | |
284 | self.assertWarning(None, w,\r | |
285 | "Overriding __eq__ blocks inheritance of __hash__ in 3.x")\r | |
286 | w.reset()\r | |
287 | class WarnCmpAndEq(DefinesAllThree):\r | |
288 | def __cmp__(self, other): pass\r | |
289 | def __eq__(self, other): pass\r | |
290 | self.assertEqual(len(w.warnings), 1)\r | |
291 | self.assertWarning(None, w,\r | |
292 | "Overriding __eq__ blocks inheritance of __hash__ in 3.x")\r | |
293 | w.reset()\r | |
294 | class NoWarningOnlyHash(DefinesAllThree):\r | |
295 | def __hash__(self): pass\r | |
296 | self.assertEqual(len(w.warnings), 0)\r | |
297 | \r | |
298 | def test_operator(self):\r | |
299 | from operator import isCallable, sequenceIncludes\r | |
300 | \r | |
301 | callable_warn = ("operator.isCallable() is not supported in 3.x. "\r | |
302 | "Use hasattr(obj, '__call__').")\r | |
303 | seq_warn = ("operator.sequenceIncludes() is not supported "\r | |
304 | "in 3.x. Use operator.contains().")\r | |
305 | with check_py3k_warnings() as w:\r | |
306 | self.assertWarning(isCallable(self), w, callable_warn)\r | |
307 | w.reset()\r | |
308 | self.assertWarning(sequenceIncludes(range(3), 2), w, seq_warn)\r | |
309 | \r | |
310 | \r | |
311 | class TestStdlibRemovals(unittest.TestCase):\r | |
312 | \r | |
313 | # test.testall not tested as it executes all unit tests as an\r | |
314 | # import side-effect.\r | |
315 | all_platforms = ('audiodev', 'imputil', 'mutex', 'user', 'new', 'rexec',\r | |
316 | 'Bastion', 'compiler', 'dircache', 'mimetools',\r | |
317 | 'fpformat', 'ihooks', 'mhlib', 'statvfs', 'htmllib',\r | |
318 | 'sgmllib', 'rfc822', 'sunaudio')\r | |
319 | inclusive_platforms = {'irix' : ('pure', 'AL', 'al', 'CD', 'cd', 'cddb',\r | |
320 | 'cdplayer', 'CL', 'cl', 'DEVICE', 'GL',\r | |
321 | 'gl', 'ERRNO', 'FILE', 'FL', 'flp', 'fl',\r | |
322 | 'fm', 'GET', 'GLWS', 'imgfile', 'IN',\r | |
323 | 'IOCTL', 'jpeg', 'panel', 'panelparser',\r | |
324 | 'readcd', 'SV', 'torgb', 'WAIT'),\r | |
325 | 'darwin' : ('autoGIL', 'Carbon', 'OSATerminology',\r | |
326 | 'icglue', 'Nav',\r | |
327 | # MacOS should (and does) give a Py3kWarning, but one of the\r | |
328 | # earlier tests already imports the MacOS extension which causes\r | |
329 | # this test to fail. Disabling the test for 'MacOS' avoids this\r | |
330 | # spurious test failure.\r | |
331 | #'MacOS',\r | |
332 | 'aepack',\r | |
333 | 'aetools', 'aetypes', 'applesingle',\r | |
334 | 'appletrawmain', 'appletrunner',\r | |
335 | 'argvemulator', 'bgenlocations',\r | |
336 | 'EasyDialogs', 'macerrors', 'macostools',\r | |
337 | 'findertools', 'FrameWork', 'ic',\r | |
338 | 'gensuitemodule', 'icopen', 'macresource',\r | |
339 | 'MiniAEFrame', 'pimp', 'PixMapWrapper',\r | |
340 | 'terminalcommand', 'videoreader',\r | |
341 | '_builtinSuites', 'CodeWarrior',\r | |
342 | 'Explorer', 'Finder', 'Netscape',\r | |
343 | 'StdSuites', 'SystemEvents', 'Terminal',\r | |
344 | 'cfmfile', 'bundlebuilder', 'buildtools',\r | |
345 | 'ColorPicker', 'Audio_mac'),\r | |
346 | 'sunos5' : ('sunaudiodev', 'SUNAUDIODEV'),\r | |
347 | }\r | |
348 | optional_modules = ('bsddb185', 'Canvas', 'dl', 'linuxaudiodev', 'imageop',\r | |
349 | 'sv', 'bsddb', 'dbhash')\r | |
350 | \r | |
351 | def check_removal(self, module_name, optional=False):\r | |
352 | """Make sure the specified module, when imported, raises a\r | |
353 | DeprecationWarning and specifies itself in the message."""\r | |
354 | with CleanImport(module_name), warnings.catch_warnings():\r | |
355 | warnings.filterwarnings("error", ".+ (module|package) .+ removed",\r | |
356 | DeprecationWarning, __name__)\r | |
357 | warnings.filterwarnings("error", ".+ removed .+ (module|package)",\r | |
358 | DeprecationWarning, __name__)\r | |
359 | try:\r | |
360 | __import__(module_name, level=0)\r | |
361 | except DeprecationWarning as exc:\r | |
362 | self.assertIn(module_name, exc.args[0],\r | |
363 | "%s warning didn't contain module name"\r | |
364 | % module_name)\r | |
365 | except ImportError:\r | |
366 | if not optional:\r | |
367 | self.fail("Non-optional module {0} raised an "\r | |
368 | "ImportError.".format(module_name))\r | |
369 | else:\r | |
370 | # For extension modules, check the __warningregistry__.\r | |
371 | # They won't rerun their init code even with CleanImport.\r | |
372 | if not check_deprecated_module(module_name):\r | |
373 | self.fail("DeprecationWarning not raised for {0}"\r | |
374 | .format(module_name))\r | |
375 | \r | |
376 | def test_platform_independent_removals(self):\r | |
377 | # Make sure that the modules that are available on all platforms raise\r | |
378 | # the proper DeprecationWarning.\r | |
379 | for module_name in self.all_platforms:\r | |
380 | self.check_removal(module_name)\r | |
381 | \r | |
382 | def test_platform_specific_removals(self):\r | |
383 | # Test the removal of platform-specific modules.\r | |
384 | for module_name in self.inclusive_platforms.get(sys.platform, []):\r | |
385 | self.check_removal(module_name, optional=True)\r | |
386 | \r | |
387 | def test_optional_module_removals(self):\r | |
388 | # Test the removal of modules that may or may not be built.\r | |
389 | for module_name in self.optional_modules:\r | |
390 | self.check_removal(module_name, optional=True)\r | |
391 | \r | |
392 | def test_os_path_walk(self):\r | |
393 | msg = "In 3.x, os.path.walk is removed in favor of os.walk."\r | |
394 | def dumbo(where, names, args): pass\r | |
395 | for path_mod in ("ntpath", "macpath", "os2emxpath", "posixpath"):\r | |
396 | mod = __import__(path_mod)\r | |
397 | reset_module_registry(mod)\r | |
398 | with check_py3k_warnings() as w:\r | |
399 | mod.walk("crashers", dumbo, None)\r | |
400 | self.assertEqual(str(w.message), msg)\r | |
401 | \r | |
402 | def test_reduce_move(self):\r | |
403 | from operator import add\r | |
404 | # reduce tests may have already triggered this warning\r | |
405 | reset_module_registry(unittest.case)\r | |
406 | with warnings.catch_warnings():\r | |
407 | warnings.filterwarnings("error", "reduce")\r | |
408 | self.assertRaises(DeprecationWarning, reduce, add, range(10))\r | |
409 | \r | |
410 | def test_mutablestring_removal(self):\r | |
411 | # UserString.MutableString has been removed in 3.0.\r | |
412 | import UserString\r | |
413 | # UserString tests may have already triggered this warning\r | |
414 | reset_module_registry(UserString)\r | |
415 | with warnings.catch_warnings():\r | |
416 | warnings.filterwarnings("error", ".*MutableString",\r | |
417 | DeprecationWarning)\r | |
418 | self.assertRaises(DeprecationWarning, UserString.MutableString)\r | |
419 | \r | |
420 | \r | |
421 | def test_main():\r | |
422 | run_unittest(TestPy3KWarnings,\r | |
423 | TestStdlibRemovals)\r | |
424 | \r | |
425 | if __name__ == '__main__':\r | |
426 | test_main()\r |