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