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