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