Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(12143)

Side by Side Diff: Lib/unittest/case.py

Issue 15836: unittest assertRaises should verify excClass is actually a BaseException class
Patch Set: Created 7 years, 1 month ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
OLDNEW
1 """Test case implementation""" 1 """Test case implementation"""
2 2
3 import sys 3 import sys
4 import functools 4 import functools
5 import difflib 5 import difflib
6 import pprint 6 import pprint
7 import re 7 import re
8 import warnings 8 import warnings
9 import collections 9 import collections
10 10
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
91 91
92 def expectedFailure(func): 92 def expectedFailure(func):
93 @functools.wraps(func) 93 @functools.wraps(func)
94 def wrapper(*args, **kwargs): 94 def wrapper(*args, **kwargs):
95 try: 95 try:
96 func(*args, **kwargs) 96 func(*args, **kwargs)
97 except Exception: 97 except Exception:
98 raise _ExpectedFailure(sys.exc_info()) 98 raise _ExpectedFailure(sys.exc_info())
99 raise _UnexpectedSuccess 99 raise _UnexpectedSuccess
100 return wrapper 100 return wrapper
101
102 def _ensure_is_exception_type(e):
103 if not isinstance(e, type) or not issubclass(e, BaseException):
104 raise TypeError('assertRaises arg 1 must be an exception type '
105 'or tuple of exception types')
101 106
102 107
103 class _AssertRaisesBaseContext(object): 108 class _AssertRaisesBaseContext(object):
104 109
105 def __init__(self, expected, test_case, callable_obj=None, 110 def __init__(self, expected, test_case, callable_obj=None,
106 expected_regex=None): 111 expected_regex=None):
107 self.expected = expected 112 self.expected = expected
113
114 if type(expected) is tuple:
115 for e in expected:
116 _ensure_is_exception_type(e)
117 else:
118 _ensure_is_exception_type(expected)
119
108 self.test_case = test_case 120 self.test_case = test_case
109 if callable_obj is not None: 121 if callable_obj is not None:
110 try: 122 try:
111 self.obj_name = callable_obj.__name__ 123 self.obj_name = callable_obj.__name__
112 except AttributeError: 124 except AttributeError:
113 self.obj_name = str(callable_obj) 125 self.obj_name = str(callable_obj)
114 else: 126 else:
115 self.obj_name = None 127 self.obj_name = None
116 if isinstance(expected_regex, (bytes, str)): 128 if isinstance(expected_regex, (bytes, str)):
117 expected_regex = re.compile(expected_regex) 129 expected_regex = re.compile(expected_regex)
(...skipping 1088 matching lines...) Expand 10 before | Expand all | Expand 10 after
1206 1218
1207 def __repr__(self): 1219 def __repr__(self):
1208 return "<%s tec=%s>" % (strclass(self.__class__), 1220 return "<%s tec=%s>" % (strclass(self.__class__),
1209 self._testFunc) 1221 self._testFunc)
1210 1222
1211 def shortDescription(self): 1223 def shortDescription(self):
1212 if self._description is not None: 1224 if self._description is not None:
1213 return self._description 1225 return self._description
1214 doc = self._testFunc.__doc__ 1226 doc = self._testFunc.__doc__
1215 return doc and doc.split("\n")[0].strip() or None 1227 return doc and doc.split("\n")[0].strip() or None
OLDNEW

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+