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