blob: 617d996f218b3f050979ec4cc3fffb39824c64b2 [file] [log] [blame]
Steven Bethardae42f332008-03-18 17:26:10 +00001import unittest
Brett Cannon977eb022008-03-19 17:37:43 +00002import sys
Alexandre Vassalottieb83f702008-05-11 07:06:04 +00003from test.test_support import (catch_warning, CleanImport,
4 TestSkipped, run_unittest)
Steven Bethardae42f332008-03-18 17:26:10 +00005import warnings
6
Brett Cannon977eb022008-03-19 17:37:43 +00007if not sys.py3kwarning:
8 raise TestSkipped('%s must be run with the -3 flag' % __name__)
9
Steven Bethardae42f332008-03-18 17:26:10 +000010
11class TestPy3KWarnings(unittest.TestCase):
12
Benjamin Peterson2fe3ef82008-06-08 02:05:33 +000013 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
Steven Bethardae42f332008-03-18 17:26:10 +000019 def test_type_inequality_comparisons(self):
Georg Brandld5b635f2008-03-25 08:29:14 +000020 expected = 'type inequality comparisons not supported in 3.x'
Steven Bethardae42f332008-03-18 17:26:10 +000021 with catch_warning() as w:
22 self.assertWarning(int < str, w, expected)
23 with catch_warning() as w:
24 self.assertWarning(type < object, w, expected)
25
26 def test_object_inequality_comparisons(self):
Georg Brandld5b635f2008-03-25 08:29:14 +000027 expected = 'comparing unequal types not supported in 3.x'
Steven Bethardae42f332008-03-18 17:26:10 +000028 with catch_warning() as w:
29 self.assertWarning(str < [], w, expected)
30 with catch_warning() as w:
31 self.assertWarning(object() < (1, 2), w, expected)
32
33 def test_dict_inequality_comparisons(self):
Georg Brandld5b635f2008-03-25 08:29:14 +000034 expected = 'dict inequality comparisons not supported in 3.x'
Steven Bethardae42f332008-03-18 17:26:10 +000035 with catch_warning() as w:
36 self.assertWarning({} < {2:3}, w, expected)
37 with catch_warning() as w:
38 self.assertWarning({} <= {}, w, expected)
39 with catch_warning() as w:
40 self.assertWarning({} > {2:3}, w, expected)
41 with catch_warning() as w:
42 self.assertWarning({2:3} >= {}, w, expected)
43
44 def test_cell_inequality_comparisons(self):
Georg Brandld5b635f2008-03-25 08:29:14 +000045 expected = 'cell comparisons not supported in 3.x'
Steven Bethardae42f332008-03-18 17:26:10 +000046 def f(x):
47 def g():
48 return x
49 return g
50 cell0, = f(0).func_closure
51 cell1, = f(1).func_closure
52 with catch_warning() as w:
53 self.assertWarning(cell0 == cell1, w, expected)
54 with catch_warning() as w:
55 self.assertWarning(cell0 < cell1, w, expected)
56
Steven Bethard6a644f92008-03-18 22:08:20 +000057 def test_code_inequality_comparisons(self):
Georg Brandld5b635f2008-03-25 08:29:14 +000058 expected = 'code inequality comparisons not supported in 3.x'
Steven Bethard6a644f92008-03-18 22:08:20 +000059 def f(x):
60 pass
61 def g(x):
62 pass
63 with catch_warning() as w:
64 self.assertWarning(f.func_code < g.func_code, w, expected)
65 with catch_warning() as w:
66 self.assertWarning(f.func_code <= g.func_code, w, expected)
67 with catch_warning() as w:
68 self.assertWarning(f.func_code >= g.func_code, w, expected)
69 with catch_warning() as w:
70 self.assertWarning(f.func_code > g.func_code, w, expected)
71
72 def test_builtin_function_or_method_comparisons(self):
73 expected = ('builtin_function_or_method '
Georg Brandld5b635f2008-03-25 08:29:14 +000074 'inequality comparisons not supported in 3.x')
Steven Bethard6a644f92008-03-18 22:08:20 +000075 func = eval
76 meth = {}.get
77 with catch_warning() as w:
78 self.assertWarning(func < meth, w, expected)
79 with catch_warning() as w:
80 self.assertWarning(func > meth, w, expected)
81 with catch_warning() as w:
82 self.assertWarning(meth <= func, w, expected)
83 with catch_warning() as w:
84 self.assertWarning(meth >= func, w, expected)
85
Steven Bethardae42f332008-03-18 17:26:10 +000086 def assertWarning(self, _, warning, expected_message):
87 self.assertEqual(str(warning.message), expected_message)
88
Raymond Hettinger05387862008-03-19 17:45:19 +000089 def test_sort_cmp_arg(self):
Georg Brandld5b635f2008-03-25 08:29:14 +000090 expected = "the cmp argument is not supported in 3.x"
Raymond Hettinger05387862008-03-19 17:45:19 +000091 lst = range(5)
92 cmp = lambda x,y: -1
93
94 with catch_warning() as w:
95 self.assertWarning(lst.sort(cmp=cmp), w, expected)
96 with catch_warning() as w:
97 self.assertWarning(sorted(lst, cmp=cmp), w, expected)
98 with catch_warning() as w:
99 self.assertWarning(lst.sort(cmp), w, expected)
100 with catch_warning() as w:
101 self.assertWarning(sorted(lst, cmp), w, expected)
102
Georg Brandl5a444242008-03-21 20:11:46 +0000103 def test_sys_exc_clear(self):
Georg Brandld5b635f2008-03-25 08:29:14 +0000104 expected = 'sys.exc_clear() not supported in 3.x; use except clauses'
Georg Brandl5a444242008-03-21 20:11:46 +0000105 with catch_warning() as w:
106 self.assertWarning(sys.exc_clear(), w, expected)
107
Georg Brandl07e56812008-03-21 20:21:46 +0000108 def test_methods_members(self):
109 expected = '__members__ and __methods__ not supported in 3.x'
110 class C:
111 __methods__ = ['a']
112 __members__ = ['b']
113 c = C()
114 with catch_warning() as w:
115 self.assertWarning(dir(c), w, expected)
116
Georg Brandl65bb42d2008-03-21 20:38:24 +0000117 def test_softspace(self):
118 expected = 'file.softspace not supported in 3.x'
119 with file(__file__) as f:
120 with catch_warning() as w:
121 self.assertWarning(f.softspace, w, expected)
122 def set():
123 f.softspace = 0
124 with catch_warning() as w:
125 self.assertWarning(set(), w, expected)
126
Georg Brandl80055f62008-03-25 07:56:27 +0000127 def test_buffer(self):
Georg Brandld5b635f2008-03-25 08:29:14 +0000128 expected = 'buffer() not supported in 3.x; use memoryview()'
Georg Brandl80055f62008-03-25 07:56:27 +0000129 with catch_warning() as w:
130 self.assertWarning(buffer('a'), w, expected)
131
Georg Brandla9916b52008-05-17 22:11:54 +0000132 def test_file_xreadlines(self):
133 expected = ("f.xreadlines() not supported in 3.x, "
134 "try 'for line in f' instead")
135 with file(__file__) as f:
136 with catch_warning() as w:
137 self.assertWarning(f.xreadlines(), w, expected)
138
Georg Brandl07e56812008-03-21 20:21:46 +0000139
Brett Cannone5d2cba2008-05-06 23:23:34 +0000140class TestStdlibRemovals(unittest.TestCase):
141
Brett Cannon3c759142008-05-09 05:25:37 +0000142 # test.testall not tested as it executes all unit tests as an
143 # import side-effect.
Brett Cannon4c1f8812008-05-10 02:27:04 +0000144 all_platforms = ('audiodev', 'imputil', 'mutex', 'user', 'new', 'rexec',
Brett Cannon27508d42008-05-10 22:45:07 +0000145 'Bastion', 'compiler', 'dircache', 'fpformat',
Georg Brandlac19d852008-06-01 21:19:14 +0000146 'ihooks', 'mhlib', 'statvfs', 'htmllib', 'sgmllib')
Brett Cannon54c77aa2008-05-14 21:08:41 +0000147 inclusive_platforms = {'irix' : ('pure', 'AL', 'al', 'CD', 'cd', 'cddb',
Brett Cannon044616a2008-05-15 02:33:55 +0000148 'cdplayer', 'CL', 'cl', 'DEVICE', 'GL',
Brett Cannon75ba4652008-05-15 03:23:17 +0000149 'gl', 'ERRNO', 'FILE', 'FL', 'flp', 'fl',
Brett Cannond8c41ec2008-05-15 03:41:55 +0000150 'fm', 'GET', 'GLWS', 'imgfile', 'IN',
Brett Cannoncd2de082008-05-15 03:51:21 +0000151 'IOCTL', 'jpeg', 'panel', 'panelparser',
Brett Cannon74a596c2008-05-15 04:17:35 +0000152 'readcd', 'SV', 'torgb', 'WAIT'),
Benjamin Peterson23681932008-05-12 21:42:13 +0000153 'darwin' : ('autoGIL', 'Carbon', 'OSATerminology',
Brett Cannonea785fb2008-05-14 01:09:40 +0000154 'icglue', 'Nav', 'MacOS', 'aepack',
155 'aetools', 'aetypes', 'applesingle',
156 'appletrawmain', 'appletrunner',
157 'argvemulator', 'bgenlocations',
Benjamin Peterson23681932008-05-12 21:42:13 +0000158 'EasyDialogs', 'macerrors', 'macostools',
159 'findertools', 'FrameWork', 'ic',
160 'gensuitemodule', 'icopen', 'macresource',
161 'MiniAEFrame', 'pimp', 'PixMapWrapper',
Brett Cannonea785fb2008-05-14 01:09:40 +0000162 'terminalcommand', 'videoreader',
163 '_builtinSuites', 'CodeWarrior',
164 'Explorer', 'Finder', 'Netscape',
165 'StdSuites', 'SystemEvents', 'Terminal',
166 'cfmfile', 'bundlebuilder', 'buildtools',
Brett Cannon22248172008-05-16 00:10:24 +0000167 'ColorPicker'),
168 'sunos5' : ('sunaudiodev', 'SUNAUDIODEV'),
169 }
Brett Cannonac861b52008-05-12 03:45:59 +0000170 optional_modules = ('bsddb185', 'Canvas', 'dl', 'linuxaudiodev', 'imageop',
Alexandre Vassalotti3c4971c2008-05-16 19:14:31 +0000171 'sv', 'cPickle')
Brett Cannone5d2cba2008-05-06 23:23:34 +0000172
Brett Cannon9ac39742008-05-09 22:51:58 +0000173 def check_removal(self, module_name, optional=False):
Brett Cannone5d2cba2008-05-06 23:23:34 +0000174 """Make sure the specified module, when imported, raises a
175 DeprecationWarning and specifies itself in the message."""
Alexandre Vassalottieb83f702008-05-11 07:06:04 +0000176 with CleanImport(module_name):
Brett Cannonddf7a422008-05-10 03:16:38 +0000177 with catch_warning(record=False) as w:
Brett Cannone5d2cba2008-05-06 23:23:34 +0000178 warnings.filterwarnings("error", ".+ removed",
179 DeprecationWarning)
180 try:
181 __import__(module_name, level=0)
182 except DeprecationWarning as exc:
Benjamin Petersonab1fb9f2008-05-12 22:26:05 +0000183 self.assert_(module_name in exc.args[0],
184 "%s warning didn't contain module name"
185 % module_name)
Brett Cannon9ac39742008-05-09 22:51:58 +0000186 except ImportError:
187 if not optional:
Benjamin Petersonbbb09372008-05-13 00:09:46 +0000188 self.fail("Non-optional module {0} raised an "
189 "ImportError.".format(module_name))
Brett Cannone5d2cba2008-05-06 23:23:34 +0000190 else:
Benjamin Petersonbbb09372008-05-13 00:09:46 +0000191 self.fail("DeprecationWarning not raised for {0}"
192 .format(module_name))
Brett Cannone5d2cba2008-05-06 23:23:34 +0000193
194 def test_platform_independent_removals(self):
195 # Make sure that the modules that are available on all platforms raise
196 # the proper DeprecationWarning.
197 for module_name in self.all_platforms:
198 self.check_removal(module_name)
199
Brett Cannon9ac39742008-05-09 22:51:58 +0000200 def test_platform_specific_removals(self):
201 # Test the removal of platform-specific modules.
202 for module_name in self.inclusive_platforms.get(sys.platform, []):
203 self.check_removal(module_name, optional=True)
204
Brett Cannon768d44f2008-05-10 02:47:54 +0000205 def test_optional_module_removals(self):
206 # Test the removal of modules that may or may not be built.
207 for module_name in self.optional_modules:
208 self.check_removal(module_name, optional=True)
209
Benjamin Peterson0893a0a2008-05-09 00:27:01 +0000210 def test_os_path_walk(self):
211 msg = "In 3.x, os.path.walk is removed in favor of os.walk."
212 def dumbo(where, names, args): pass
213 for path_mod in ("ntpath", "macpath", "os2emxpath", "posixpath"):
214 mod = __import__(path_mod)
215 with catch_warning() as w:
Benjamin Peterson1d310232008-05-27 01:42:29 +0000216 mod.walk("crashers", dumbo, None)
Benjamin Peterson0893a0a2008-05-09 00:27:01 +0000217 self.assertEquals(str(w.message), msg)
218
Benjamin Peterson3aa84a72008-05-26 19:41:53 +0000219 def test_commands_members(self):
220 import commands
221 members = {"mk2arg" : 2, "mkarg" : 1, "getstatus" : 1}
222 for name, arg_count in members.items():
223 with catch_warning(record=False):
224 warnings.filterwarnings("error")
225 func = getattr(commands, name)
226 self.assertRaises(DeprecationWarning, func, *([None]*arg_count))
227
Brett Cannonabb34fe2008-05-29 05:08:50 +0000228 def test_mutablestring_removal(self):
229 # UserString.MutableString has been removed in 3.0.
230 import UserString
231 with catch_warning(record=False):
232 warnings.filterwarnings("error", ".*MutableString",
233 DeprecationWarning)
234 self.assertRaises(DeprecationWarning, UserString.MutableString)
235
Brett Cannone5d2cba2008-05-06 23:23:34 +0000236
Steven Bethardae42f332008-03-18 17:26:10 +0000237def test_main():
Benjamin Peterson1d310232008-05-27 01:42:29 +0000238 with catch_warning(record=True):
239 warnings.simplefilter("always")
240 run_unittest(TestPy3KWarnings,
241 TestStdlibRemovals)
Steven Bethardae42f332008-03-18 17:26:10 +0000242
243if __name__ == '__main__':
244 test_main()