~pythonregexp2.7/python/issue2636

« back to all changes in this revision

Viewing changes to Lib/test/test_py3kwarn.py

  • Committer: Jeffrey C. "The TimeHorse" Jacobs
  • Date: 2008-05-24 16:05:21 UTC
  • mfrom: (39021.1.401 Regexp-2.6)
  • Revision ID: darklord@timehorse.com-20080524160521-1xenj7p6u3wb89et
Merged in changes from the latest python source snapshot.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
import unittest
2
2
import sys
3
 
from test.test_support import (catch_warning, TestSkipped, run_unittest,
4
 
                                TestSkipped)
 
3
from test.test_support import (catch_warning, CleanImport,
 
4
                               TestSkipped, run_unittest)
5
5
import warnings
6
6
 
7
7
if not sys.py3kwarning:
123
123
        with catch_warning() as w:
124
124
            self.assertWarning(buffer('a'), w, expected)
125
125
 
 
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
 
 
133
 
 
134
class TestStdlibRemovals(unittest.TestCase):
 
135
 
 
136
    # test.testall not tested as it executes all unit tests as an
 
137
    # import side-effect.
 
138
    all_platforms = ('audiodev', 'imputil', 'mutex', 'user', 'new', 'rexec',
 
139
                        'Bastion', 'compiler', 'dircache', 'fpformat',
 
140
                        'ihooks', 'mhlib', 'statvfs')
 
141
    inclusive_platforms = {'irix' : ('pure', 'AL', 'al', 'CD', 'cd', 'cddb',
 
142
                                     'cdplayer', 'CL', 'cl', 'DEVICE', 'GL',
 
143
                                     'gl', 'ERRNO', 'FILE', 'FL', 'flp', 'fl',
 
144
                                     'fm', 'GET', 'GLWS', 'imgfile', 'IN',
 
145
                                     'IOCTL', 'jpeg', 'panel', 'panelparser',
 
146
                                     'readcd', 'SV', 'torgb', 'WAIT'),
 
147
                          'darwin' : ('autoGIL', 'Carbon', 'OSATerminology',
 
148
                                      'icglue', 'Nav', 'MacOS', 'aepack',
 
149
                                      'aetools', 'aetypes', 'applesingle',
 
150
                                      'appletrawmain', 'appletrunner',
 
151
                                      'argvemulator', 'bgenlocations',
 
152
                                      'EasyDialogs', 'macerrors', 'macostools',
 
153
                                      'findertools', 'FrameWork', 'ic',
 
154
                                      'gensuitemodule', 'icopen', 'macresource',
 
155
                                      'MiniAEFrame', 'pimp', 'PixMapWrapper',
 
156
                                      'terminalcommand', 'videoreader',
 
157
                                      '_builtinSuites', 'CodeWarrior',
 
158
                                      'Explorer', 'Finder', 'Netscape',
 
159
                                      'StdSuites', 'SystemEvents', 'Terminal',
 
160
                                      'cfmfile', 'bundlebuilder', 'buildtools',
 
161
                                      'ColorPicker'),
 
162
                           'sunos5' : ('sunaudiodev', 'SUNAUDIODEV'),
 
163
                          }
 
164
    optional_modules = ('bsddb185', 'Canvas', 'dl', 'linuxaudiodev', 'imageop',
 
165
                        'sv', 'cPickle')
 
166
 
 
167
    def check_removal(self, module_name, optional=False):
 
168
        """Make sure the specified module, when imported, raises a
 
169
        DeprecationWarning and specifies itself in the message."""
 
170
        with CleanImport(module_name):
 
171
            with catch_warning(record=False) as w:
 
172
                warnings.filterwarnings("error", ".+ removed",
 
173
                                        DeprecationWarning)
 
174
                try:
 
175
                    __import__(module_name, level=0)
 
176
                except DeprecationWarning as exc:
 
177
                    self.assert_(module_name in exc.args[0],
 
178
                                 "%s warning didn't contain module name"
 
179
                                 % module_name)
 
180
                except ImportError:
 
181
                    if not optional:
 
182
                        self.fail("Non-optional module {0} raised an "
 
183
                                  "ImportError.".format(module_name))
 
184
                else:
 
185
                    self.fail("DeprecationWarning not raised for {0}"
 
186
                                .format(module_name))
 
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
 
 
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
 
 
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
 
 
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:
 
210
                # Since os3exmpath just imports it from ntpath
 
211
                warnings.simplefilter("always")
 
212
                mod.walk(".", dumbo, None)
 
213
            self.assertEquals(str(w.message), msg)
 
214
 
 
215
 
 
216
class TestStdlibRenames(unittest.TestCase):
 
217
 
 
218
    renames = {'Queue': 'queue',
 
219
               'SocketServer': 'socketserver',
 
220
               'ConfigParser': 'configparser',
 
221
              }
 
222
 
 
223
    def check_rename(self, module_name, new_module_name):
 
224
        """Make sure that:
 
225
        - A DeprecationWarning is raised when importing using the
 
226
          old 2.x module name.
 
227
        - The module can be imported using the new 3.x name.
 
228
        - The warning message specify both names.
 
229
        """
 
230
        with CleanImport(module_name):
 
231
            with catch_warning(record=False) as w:
 
232
                warnings.filterwarnings("error", ".+ renamed to",
 
233
                                        DeprecationWarning)
 
234
                try:
 
235
                    __import__(module_name, level=0)
 
236
                except DeprecationWarning as exc:
 
237
                    self.assert_(module_name in exc.args[0])
 
238
                    self.assert_(new_module_name in exc.args[0])
 
239
                else:
 
240
                    self.fail("DeprecationWarning not raised for %s" %
 
241
                              module_name)
 
242
        with CleanImport(new_module_name):
 
243
            try:
 
244
                __import__(new_module_name, level=0)
 
245
            except ImportError:
 
246
                self.fail("cannot import %s with its 3.x name, %s" %
 
247
                          module_name, new_module_name)
 
248
            except DeprecationWarning:
 
249
                self.fail("unexpected DeprecationWarning raised for %s" %
 
250
                          module_name)
 
251
 
 
252
    def test_module_renames(self):
 
253
        for module_name, new_module_name in self.renames.items():
 
254
            self.check_rename(module_name, new_module_name)
 
255
 
126
256
 
127
257
def test_main():
128
 
    run_unittest(TestPy3KWarnings)
 
258
    run_unittest(TestPy3KWarnings,
 
259
                 TestStdlibRemovals,
 
260
                 TestStdlibRenames)
129
261
 
130
262
if __name__ == '__main__':
131
263
    test_main()