Ignore:
Timestamp:
Mar 19, 2014, 11:31:01 PM (11 years ago)
Author:
dmik
Message:

python: Merge vendor 2.7.6 to trunk.

Location:
python/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • python/trunk

  • python/trunk/Lib/test/test_pep352.py

    r2 r391  
    33import exceptions
    44import warnings
    5 from test.test_support import run_unittest
     5from test.test_support import run_unittest, check_warnings
    66import os
     7import sys
    78from platform import system as platform_system
    89
    9 def ignore_message_warning():
    10     """Ignore the DeprecationWarning for BaseException.message."""
    11     warnings.resetwarnings()
    12     warnings.filterwarnings("ignore", "BaseException.message",
    13                             DeprecationWarning)
    14 
     10DEPRECATION_WARNINGS = ["BaseException.message has been deprecated"]
     11
     12if sys.py3kwarning:
     13    DEPRECATION_WARNINGS.extend(
     14        ["exceptions must derive from BaseException",
     15         "catching classes that don't inherit from BaseException is not allowed",
     16         "__get(item|slice)__ not supported for exception classes"])
     17
     18_deprecations = [(msg, DeprecationWarning) for msg in DEPRECATION_WARNINGS]
     19
     20# Silence Py3k and other deprecation warnings
     21def ignore_deprecation_warnings(func):
     22    """Ignore the known DeprecationWarnings."""
     23    def wrapper(*args, **kw):
     24        with check_warnings(*_deprecations, quiet=True):
     25            return func(*args, **kw)
     26    return wrapper
    1527
    1628class ExceptionClassTests(unittest.TestCase):
     
    2032
    2133    def test_builtins_new_style(self):
    22         self.failUnless(issubclass(Exception, object))
    23 
     34        self.assertTrue(issubclass(Exception, object))
     35
     36    @ignore_deprecation_warnings
    2437    def verify_instance_interface(self, ins):
    25         with warnings.catch_warnings():
    26             ignore_message_warning()
    27             for attr in ("args", "message", "__str__", "__repr__",
    28                             "__getitem__"):
    29                 self.failUnless(hasattr(ins, attr),
    30                         "%s missing %s attribute" %
     38        for attr in ("args", "message", "__str__", "__repr__", "__getitem__"):
     39            self.assertTrue(hasattr(ins, attr),
     40                            "%s missing %s attribute" %
    3141                            (ins.__class__.__name__, attr))
    3242
     
    4252            except AttributeError:
    4353                self.fail("base class %s not a built-in" % superclass_name)
    44             self.failUnless(superclass_name in exc_set)
     54            self.assertIn(superclass_name, exc_set)
    4555            exc_set.discard(superclass_name)
    4656            superclasses = []  # Loop will insert base exception
     
    6979                    while superclasses[-1][0] >= depth:
    7080                        superclasses.pop()
    71                 self.failUnless(issubclass(exc, superclasses[-1][1]),
     81                self.assertTrue(issubclass(exc, superclasses[-1][1]),
    7282                "%s is not a subclass of %s" % (exc.__name__,
    7383                    superclasses[-1][1].__name__))
     
    7686                except TypeError:
    7787                    pass
    78                 self.failUnless(exc_name in exc_set)
     88                self.assertIn(exc_name, exc_set)
    7989                exc_set.discard(exc_name)
    8090                last_exc = exc
     
    8292        finally:
    8393            inheritance_tree.close()
    84         self.failUnlessEqual(len(exc_set), 0, "%s not accounted for" % exc_set)
     94        self.assertEqual(len(exc_set), 0, "%s not accounted for" % exc_set)
    8595
    8696    interface_tests = ("length", "args", "message", "str", "unicode", "repr",
     
    8999    def interface_test_driver(self, results):
    90100        for test_name, (given, expected) in zip(self.interface_tests, results):
    91             self.failUnlessEqual(given, expected, "%s: %s != %s" % (test_name,
     101            self.assertEqual(given, expected, "%s: %s != %s" % (test_name,
    92102                given, expected))
    93103
     104    @ignore_deprecation_warnings
    94105    def test_interface_single_arg(self):
    95106        # Make sure interface works properly when given a single argument
    96107        arg = "spam"
    97108        exc = Exception(arg)
    98         with warnings.catch_warnings():
    99             ignore_message_warning()
    100             results = ([len(exc.args), 1], [exc.args[0], arg],
    101                     [exc.message, arg],
    102                     [str(exc), str(arg)], [unicode(exc), unicode(arg)],
    103                 [repr(exc), exc.__class__.__name__ + repr(exc.args)], [exc[0],
    104                 arg])
    105             self.interface_test_driver(results)
    106 
     109        results = ([len(exc.args), 1], [exc.args[0], arg], [exc.message, arg],
     110                   [str(exc), str(arg)], [unicode(exc), unicode(arg)],
     111                   [repr(exc), exc.__class__.__name__ + repr(exc.args)],
     112                   [exc[0], arg])
     113        self.interface_test_driver(results)
     114
     115    @ignore_deprecation_warnings
    107116    def test_interface_multi_arg(self):
    108117        # Make sure interface correct when multiple arguments given
     
    110119        args = tuple(range(arg_count))
    111120        exc = Exception(*args)
    112         with warnings.catch_warnings():
    113             ignore_message_warning()
    114             results = ([len(exc.args), arg_count], [exc.args, args],
    115                     [exc.message, ''], [str(exc), str(args)],
    116                     [unicode(exc), unicode(args)],
    117                     [repr(exc), exc.__class__.__name__ + repr(exc.args)],
    118                     [exc[-1], args[-1]])
    119             self.interface_test_driver(results)
    120 
     121        results = ([len(exc.args), arg_count], [exc.args, args],
     122                   [exc.message, ''], [str(exc), str(args)],
     123                   [unicode(exc), unicode(args)],
     124                   [repr(exc), exc.__class__.__name__ + repr(exc.args)],
     125                   [exc[-1], args[-1]])
     126        self.interface_test_driver(results)
     127
     128    @ignore_deprecation_warnings
    121129    def test_interface_no_arg(self):
    122130        # Make sure that with no args that interface is correct
    123131        exc = Exception()
    124         with warnings.catch_warnings():
    125             ignore_message_warning()
    126             results = ([len(exc.args), 0], [exc.args, tuple()],
    127                     [exc.message, ''],
    128                     [str(exc), ''], [unicode(exc), u''],
    129                     [repr(exc), exc.__class__.__name__ + '()'], [True, True])
    130             self.interface_test_driver(results)
     132        results = ([len(exc.args), 0], [exc.args, tuple()],
     133                   [exc.message, ''],
     134                   [str(exc), ''], [unicode(exc), u''],
     135                   [repr(exc), exc.__class__.__name__ + '()'], [True, True])
     136        self.interface_test_driver(results)
    131137
    132138
    133139    def test_message_deprecation(self):
    134140        # As of Python 2.6, BaseException.message is deprecated.
    135         with warnings.catch_warnings():
    136             warnings.resetwarnings()
    137             warnings.filterwarnings('error')
    138 
    139             try:
    140                 BaseException().message
    141             except DeprecationWarning:
    142                 pass
    143             else:
    144                 self.fail("BaseException.message not deprecated")
     141        with check_warnings(("", DeprecationWarning)):
     142            BaseException().message
    145143
    146144
     
    180178                        "tuple" % type(object_))
    181179
     180    @ignore_deprecation_warnings
    182181    def test_raise_classic(self):
    183182        # Raising a classic class is okay (for now).
     
    195194            pass
    196195        except:
    197             self.fail("unable to raise class class instance")
     196            self.fail("unable to raise classic class instance")
    198197
    199198    def test_raise_new_style_non_exception(self):
     
    217216            warnings.filterwarnings("error")
    218217            str_exc = "spam"
    219             try:
     218            with self.assertRaises(DeprecationWarning):
    220219                try:
    221220                    raise StandardError
    222221                except str_exc:
    223222                    pass
    224             except DeprecationWarning:
    225                 pass
    226             except StandardError:
    227                 self.fail("catching a string exception did not raise "
    228                             "DeprecationWarning")
     223
    229224            # Make sure that even if the string exception is listed in a tuple
    230225            # that a warning is raised.
    231             try:
     226            with self.assertRaises(DeprecationWarning):
    232227                try:
    233228                    raise StandardError
    234229                except (AssertionError, str_exc):
    235230                    pass
    236             except DeprecationWarning:
    237                 pass
    238             except StandardError:
    239                 self.fail("catching a string exception specified in a tuple did "
    240                             "not raise DeprecationWarning")
    241231
    242232
Note: See TracChangeset for help on using the changeset viewer.