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