MCPcopy Create free account
hub / github.com/sqlalchemy/sqlalchemy / _expect_raises

Function _expect_raises

lib/sqlalchemy/testing/assertions.py:453–492  ·  view source on GitHub ↗
(except_cls, msg=None, check_context=False)

Source from the content-addressed store, hash-verified

451
452@contextlib.contextmanager
453def _expect_raises(except_cls, msg=None, check_context=False):
454 if (
455 isinstance(except_cls, type)
456 and issubclass(except_cls, Warning)
457 or isinstance(except_cls, Warning)
458 ):
459 raise TypeError(
460 "Use expect_warnings for warnings, not "
461 "expect_raises / assert_raises"
462 )
463 ec = _ErrorContainer()
464 if check_context:
465 are_we_already_in_a_traceback = sys.exc_info()[0]
466 try:
467 yield ec
468 success = False
469 except except_cls as err:
470 ec.error = err
471 success = True
472 if msg is not None:
473 # I'm often pdbing here, and "err" above isn't
474 # in scope, so assign the string explicitly
475 error_as_string = str(err)
476 assert re.search(msg, error_as_string, re.UNICODE), "%r !~ %s" % (
477 msg,
478 error_as_string,
479 )
480 if check_context and not are_we_already_in_a_traceback:
481 _assert_proper_exception_context(err)
482 print(str(err).encode("utf-8"))
483
484 # it's generally a good idea to not carry traceback objects outside
485 # of the except: block, but in this case especially we seem to have
486 # hit some bug in either python 3.10.0b2 or greenlet or both which
487 # this seems to fix:
488 # https://github.com/python-greenlet/greenlet/issues/242
489 del ec
490
491 # assert outside the block so it works for AssertionError too !
492 assert success, "Callable did not raise an exception"
493
494
495def expect_raises(except_cls, check_context=True):

Callers 3

_assert_raisesFunction · 0.85
expect_raisesFunction · 0.85
expect_raises_messageFunction · 0.85

Calls 2

_ErrorContainerClass · 0.85

Tested by

no test coverage detected