blob: 4dfc84257816483580a42f34d4590959bf905357 [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
13 def test_type_inequality_comparisons(self):
Georg Brandld5b635f2008-03-25 08:29:14 +000014 expected = 'type inequality comparisons not supported in 3.x'
Steven Bethardae42f332008-03-18 17:26:10 +000015 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 Brandld5b635f2008-03-25 08:29:14 +000021 expected = 'comparing unequal types not supported in 3.x'
Steven Bethardae42f332008-03-18 17:26:10 +000022 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 Brandld5b635f2008-03-25 08:29:14 +000028 expected = 'dict inequality comparisons not supported in 3.x'
Steven Bethardae42f332008-03-18 17:26:10 +000029 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 Brandld5b635f2008-03-25 08:29:14 +000039 expected = 'cell comparisons not supported in 3.x'
Steven Bethardae42f332008-03-18 17:26:10 +000040 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 Bethard6a644f92008-03-18 22:08:20 +000051 def test_code_inequality_comparisons(self):
Georg Brandld5b635f2008-03-25 08:29:14 +000052 expected = 'code inequality comparisons not supported in 3.x'
Steven Bethard6a644f92008-03-18 22:08:20 +000053 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 Brandld5b635f2008-03-25 08:29:14 +000068 'inequality comparisons not supported in 3.x')
Steven Bethard6a644f92008-03-18 22:08:20 +000069 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 Bethardae42f332008-03-18 17:26:10 +000080 def assertWarning(self, _, warning, expected_message):
81 self.assertEqual(str(warning.message), expected_message)
82
Raymond Hettinger05387862008-03-19 17:45:19 +000083 def test_sort_cmp_arg(self):
Georg Brandld5b635f2008-03-25 08:29:14 +000084 expected = "the cmp argument is not supported in 3.x"
Raymond Hettinger05387862008-03-19 17:45:19 +000085 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 Brandl5a444242008-03-21 20:11:46 +000097 def test_sys_exc_clear(self):
Georg Brandld5b635f2008-03-25 08:29:14 +000098 expected = 'sys.exc_clear() not supported in 3.x; use except clauses'
Georg Brandl5a444242008-03-21 20:11:46 +000099 with catch_warning() as w:
100 self.assertWarning(sys.exc_clear(), w, expected)
101
Georg Brandl07e56812008-03-21 20:21:46 +0000102 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 Brandl65bb42d2008-03-21 20:38:24 +0000111 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 Brandl80055f62008-03-25 07:56:27 +0000121 def test_buffer(self):
Georg Brandld5b635f2008-03-25 08:29:14 +0000122 expected = 'buffer() not supported in 3.x; use memoryview()'
Georg Brandl80055f62008-03-25 07:56:27 +0000123 with catch_warning() as w:
124 self.assertWarning(buffer('a'), w, expected)
125
Georg Brandl07e56812008-03-21 20:21:46 +0000126
Brett Cannone5d2cba2008-05-06 23:23:34 +0000127class TestStdlibRemovals(unittest.TestCase):
128
Brett Cannon3c759142008-05-09 05:25:37 +0000129 # test.testall not tested as it executes all unit tests as an
130 # import side-effect.
Brett Cannon4c1f8812008-05-10 02:27:04 +0000131 all_platforms = ('audiodev', 'imputil', 'mutex', 'user', 'new', 'rexec',
Brett Cannon27508d42008-05-10 22:45:07 +0000132 'Bastion', 'compiler', 'dircache', 'fpformat',
Brett Cannon2a869132008-05-11 03:01:47 +0000133 'ihooks', 'mhlib')
Brett Cannon34721d52008-05-14 01:08:21 +0000134 inclusive_platforms = {'irix' : ('pure', 'AL', 'al'),
Benjamin Peterson23681932008-05-12 21:42:13 +0000135 'darwin' : ('autoGIL', 'Carbon', 'OSATerminology',
Brett Cannonea785fb2008-05-14 01:09:40 +0000136 'icglue', 'Nav', 'MacOS', 'aepack',
137 'aetools', 'aetypes', 'applesingle',
138 'appletrawmain', 'appletrunner',
139 'argvemulator', 'bgenlocations',
Benjamin Peterson23681932008-05-12 21:42:13 +0000140 'EasyDialogs', 'macerrors', 'macostools',
141 'findertools', 'FrameWork', 'ic',
142 'gensuitemodule', 'icopen', 'macresource',
143 'MiniAEFrame', 'pimp', 'PixMapWrapper',
Brett Cannonea785fb2008-05-14 01:09:40 +0000144 'terminalcommand', 'videoreader',
145 '_builtinSuites', 'CodeWarrior',
146 'Explorer', 'Finder', 'Netscape',
147 'StdSuites', 'SystemEvents', 'Terminal',
148 'cfmfile', 'bundlebuilder', 'buildtools',
149 'ColorPicker')}
Brett Cannonac861b52008-05-12 03:45:59 +0000150 optional_modules = ('bsddb185', 'Canvas', 'dl', 'linuxaudiodev', 'imageop',
151 'sv')
Brett Cannone5d2cba2008-05-06 23:23:34 +0000152
Brett Cannon9ac39742008-05-09 22:51:58 +0000153 def check_removal(self, module_name, optional=False):
Brett Cannone5d2cba2008-05-06 23:23:34 +0000154 """Make sure the specified module, when imported, raises a
155 DeprecationWarning and specifies itself in the message."""
Alexandre Vassalottieb83f702008-05-11 07:06:04 +0000156 with CleanImport(module_name):
Brett Cannonddf7a422008-05-10 03:16:38 +0000157 with catch_warning(record=False) as w:
Brett Cannone5d2cba2008-05-06 23:23:34 +0000158 warnings.filterwarnings("error", ".+ removed",
159 DeprecationWarning)
160 try:
161 __import__(module_name, level=0)
162 except DeprecationWarning as exc:
Benjamin Petersonab1fb9f2008-05-12 22:26:05 +0000163 self.assert_(module_name in exc.args[0],
164 "%s warning didn't contain module name"
165 % module_name)
Brett Cannon9ac39742008-05-09 22:51:58 +0000166 except ImportError:
167 if not optional:
Benjamin Petersonbbb09372008-05-13 00:09:46 +0000168 self.fail("Non-optional module {0} raised an "
169 "ImportError.".format(module_name))
Brett Cannone5d2cba2008-05-06 23:23:34 +0000170 else:
Benjamin Petersonbbb09372008-05-13 00:09:46 +0000171 self.fail("DeprecationWarning not raised for {0}"
172 .format(module_name))
Brett Cannone5d2cba2008-05-06 23:23:34 +0000173
174 def test_platform_independent_removals(self):
175 # Make sure that the modules that are available on all platforms raise
176 # the proper DeprecationWarning.
177 for module_name in self.all_platforms:
178 self.check_removal(module_name)
179
Brett Cannon9ac39742008-05-09 22:51:58 +0000180 def test_platform_specific_removals(self):
181 # Test the removal of platform-specific modules.
182 for module_name in self.inclusive_platforms.get(sys.platform, []):
183 self.check_removal(module_name, optional=True)
184
Brett Cannon768d44f2008-05-10 02:47:54 +0000185 def test_optional_module_removals(self):
186 # Test the removal of modules that may or may not be built.
187 for module_name in self.optional_modules:
188 self.check_removal(module_name, optional=True)
189
Benjamin Peterson0893a0a2008-05-09 00:27:01 +0000190 def test_os_path_walk(self):
191 msg = "In 3.x, os.path.walk is removed in favor of os.walk."
192 def dumbo(where, names, args): pass
193 for path_mod in ("ntpath", "macpath", "os2emxpath", "posixpath"):
194 mod = __import__(path_mod)
195 with catch_warning() as w:
196 # Since os3exmpath just imports it from ntpath
197 warnings.simplefilter("always")
198 mod.walk(".", dumbo, None)
199 self.assertEquals(str(w.message), msg)
200
Brett Cannone5d2cba2008-05-06 23:23:34 +0000201
Alexandre Vassalottieb83f702008-05-11 07:06:04 +0000202class TestStdlibRenames(unittest.TestCase):
203
Alexandre Vassalottifb9ce652008-05-12 01:37:10 +0000204 renames = {'copy_reg': 'copyreg', 'Queue': 'queue',
205 'SocketServer': 'socketserver'}
Alexandre Vassalottieb83f702008-05-11 07:06:04 +0000206
Alexandre Vassalotti1fcaa772008-05-11 23:12:38 +0000207 def check_rename(self, module_name, new_module_name):
Alexandre Vassalottieb83f702008-05-11 07:06:04 +0000208 """Make sure that:
209 - A DeprecationWarning is raised when importing using the
210 old 2.x module name.
211 - The module can be imported using the new 3.x name.
212 - The warning message specify both names.
213 """
Alexandre Vassalottieb83f702008-05-11 07:06:04 +0000214 with CleanImport(module_name):
215 with catch_warning(record=False) as w:
216 warnings.filterwarnings("error", ".+ renamed to",
217 DeprecationWarning)
218 try:
219 __import__(module_name, level=0)
220 except DeprecationWarning as exc:
221 self.assert_(module_name in exc.args[0])
222 self.assert_(new_module_name in exc.args[0])
223 else:
224 self.fail("DeprecationWarning not raised for %s" %
225 module_name)
226 with CleanImport(new_module_name):
227 try:
228 __import__(new_module_name, level=0)
Alexandre Vassalotti1fcaa772008-05-11 23:12:38 +0000229 except ImportError:
230 self.fail("cannot import %s with its 3.x name, %s" %
231 module_name, new_module_name)
Alexandre Vassalottieb83f702008-05-11 07:06:04 +0000232 except DeprecationWarning:
233 self.fail("unexpected DeprecationWarning raised for %s" %
234 module_name)
235
Alexandre Vassalotti1fcaa772008-05-11 23:12:38 +0000236 def test_module_renames(self):
237 for module_name, new_module_name in self.renames.items():
Alexandre Vassalottieb83f702008-05-11 07:06:04 +0000238 self.check_rename(module_name, new_module_name)
239
240
Steven Bethardae42f332008-03-18 17:26:10 +0000241def test_main():
Alexandre Vassalottieb83f702008-05-11 07:06:04 +0000242 run_unittest(TestPy3KWarnings,
243 TestStdlibRemovals,
244 TestStdlibRenames)
Steven Bethardae42f332008-03-18 17:26:10 +0000245
246if __name__ == '__main__':
247 test_main()