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