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