Changeset 391 for python/trunk/Lib/test/test_pep352.py
- Timestamp:
- Mar 19, 2014, 11:31:01 PM (11 years ago)
- Location:
- python/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
python/trunk
-
Property svn:mergeinfo
set to
/python/vendor/Python-2.7.6 merged eligible /python/vendor/current merged eligible
-
Property svn:mergeinfo
set to
-
python/trunk/Lib/test/test_pep352.py
r2 r391 3 3 import exceptions 4 4 import warnings 5 from test.test_support import run_unittest 5 from test.test_support import run_unittest, check_warnings 6 6 import os 7 import sys 7 8 from platform import system as platform_system 8 9 9 def ignore_message_warning(): 10 """Ignore the DeprecationWarning for BaseException.message.""" 11 warnings.resetwarnings() 12 warnings.filterwarnings("ignore", "BaseException.message", 13 DeprecationWarning) 14 10 DEPRECATION_WARNINGS = ["BaseException.message has been deprecated"] 11 12 if 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 21 def 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 15 27 16 28 class ExceptionClassTests(unittest.TestCase): … … 20 32 21 33 def test_builtins_new_style(self): 22 self.failUnless(issubclass(Exception, object)) 23 34 self.assertTrue(issubclass(Exception, object)) 35 36 @ignore_deprecation_warnings 24 37 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" % 31 41 (ins.__class__.__name__, attr)) 32 42 … … 42 52 except AttributeError: 43 53 self.fail("base class %s not a built-in" % superclass_name) 44 self. failUnless(superclass_name inexc_set)54 self.assertIn(superclass_name, exc_set) 45 55 exc_set.discard(superclass_name) 46 56 superclasses = [] # Loop will insert base exception … … 69 79 while superclasses[-1][0] >= depth: 70 80 superclasses.pop() 71 self. failUnless(issubclass(exc, superclasses[-1][1]),81 self.assertTrue(issubclass(exc, superclasses[-1][1]), 72 82 "%s is not a subclass of %s" % (exc.__name__, 73 83 superclasses[-1][1].__name__)) … … 76 86 except TypeError: 77 87 pass 78 self. failUnless(exc_name inexc_set)88 self.assertIn(exc_name, exc_set) 79 89 exc_set.discard(exc_name) 80 90 last_exc = exc … … 82 92 finally: 83 93 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) 85 95 86 96 interface_tests = ("length", "args", "message", "str", "unicode", "repr", … … 89 99 def interface_test_driver(self, results): 90 100 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, 92 102 given, expected)) 93 103 104 @ignore_deprecation_warnings 94 105 def test_interface_single_arg(self): 95 106 # Make sure interface works properly when given a single argument 96 107 arg = "spam" 97 108 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 107 116 def test_interface_multi_arg(self): 108 117 # Make sure interface correct when multiple arguments given … … 110 119 args = tuple(range(arg_count)) 111 120 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 121 129 def test_interface_no_arg(self): 122 130 # Make sure that with no args that interface is correct 123 131 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) 131 137 132 138 133 139 def test_message_deprecation(self): 134 140 # 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 145 143 146 144 … … 180 178 "tuple" % type(object_)) 181 179 180 @ignore_deprecation_warnings 182 181 def test_raise_classic(self): 183 182 # Raising a classic class is okay (for now). … … 195 194 pass 196 195 except: 197 self.fail("unable to raise class class instance")196 self.fail("unable to raise classic class instance") 198 197 199 198 def test_raise_new_style_non_exception(self): … … 217 216 warnings.filterwarnings("error") 218 217 str_exc = "spam" 219 try:218 with self.assertRaises(DeprecationWarning): 220 219 try: 221 220 raise StandardError 222 221 except str_exc: 223 222 pass 224 except DeprecationWarning: 225 pass 226 except StandardError: 227 self.fail("catching a string exception did not raise " 228 "DeprecationWarning") 223 229 224 # Make sure that even if the string exception is listed in a tuple 230 225 # that a warning is raised. 231 try:226 with self.assertRaises(DeprecationWarning): 232 227 try: 233 228 raise StandardError 234 229 except (AssertionError, str_exc): 235 230 pass 236 except DeprecationWarning:237 pass238 except StandardError:239 self.fail("catching a string exception specified in a tuple did "240 "not raise DeprecationWarning")241 231 242 232
Note:
See TracChangeset
for help on using the changeset viewer.