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