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