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