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