MCPcopy Index your code
hub / github.com/python/cpython / _AssertRaisesContext

Class _AssertRaisesContext

Lib/unittest/case.py:251–287  ·  view source on GitHub ↗

A context manager used to implement TestCase.assertRaises* methods.

Source from the content-addressed store, hash-verified

249
250
251class _AssertRaisesContext(_AssertRaisesBaseContext):
252 """A context manager used to implement TestCase.assertRaises* methods."""
253
254 _base_type = BaseException
255 _base_type_str = 'an exception type or tuple of exception types'
256
257 def __enter__(self):
258 return self
259
260 def __exit__(self, exc_type, exc_value, tb):
261 if exc_type is None:
262 try:
263 exc_name = self.expected.__name__
264 except AttributeError:
265 exc_name = str(self.expected)
266 if self.obj_name:
267 self._raiseFailure("{} not raised by {}".format(exc_name,
268 self.obj_name))
269 else:
270 self._raiseFailure("{} not raised".format(exc_name))
271 else:
272 traceback.clear_frames(tb)
273 if not issubclass(exc_type, self.expected):
274 # let unexpected exceptions pass through
275 return False
276 # store exception, without traceback, for later retrieval
277 self.exception = exc_value.with_traceback(None)
278 if self.expected_regex is None:
279 return True
280
281 expected_regex = self.expected_regex
282 if not expected_regex.search(str(exc_value)):
283 self._raiseFailure('"{}" does not match "{}"'.format(
284 expected_regex.pattern, str(exc_value)))
285 return True
286
287 __class_getitem__ = classmethod(types.GenericAlias)
288
289
290class _AssertWarnsContext(_AssertRaisesBaseContext):

Callers 2

assertRaisesMethod · 0.85
assertRaisesRegexMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…