def raises(expected_exception, *args, **kwargs):
__tracebackhide__ = True<EOL>for exc in filterfalse(isclass, always_iterable(expected_exception, BASE_TYPE)):<EOL><INDENT>msg = (<EOL>"<STR_LIT>"<EOL>"<STR_LIT>"<EOL>)<EOL>raise TypeError(msg % type(exc))<EOL><DEDENT>message = "<STR_LIT>".format(expected_exception)<EOL>match_expr = None<EOL>if not args:<EOL><INDENT>if "<STR_LIT:message>" in kwargs:<EOL><INDENT>message = kwargs.pop("<STR_LIT:message>")<EOL>warnings.warn(deprecated.RAISES_MESSAGE_PARAMETER, stacklevel=<NUM_LIT:2>)<EOL><DEDENT>if "<STR_LIT>" in kwargs:<EOL><INDENT>match_expr = kwargs.pop("<STR_LIT>")<EOL><DEDENT>if kwargs:<EOL><INDENT>msg = "<STR_LIT>"<EOL>msg += "<STR_LIT:U+002CU+0020>".join(sorted(kwargs))<EOL>raise TypeError(msg)<EOL><DEDENT>return RaisesContext(expected_exception, message, match_expr)<EOL><DEDENT>elif isinstance(args[<NUM_LIT:0>], str):<EOL><INDENT>warnings.warn(deprecated.RAISES_EXEC, stacklevel=<NUM_LIT:2>)<EOL>code, = args<EOL>assert isinstance(code, str)<EOL>frame = sys._getframe(<NUM_LIT:1>)<EOL>loc = frame.f_locals.copy()<EOL>loc.update(kwargs)<EOL>try:<EOL><INDENT>code = _pytest._code.Source(code).compile(_genframe=frame)<EOL>six.exec_(code, frame.f_globals, loc)<EOL><DEDENT>except expected_exception:<EOL><INDENT>return _pytest._code.ExceptionInfo.from_current()<EOL><DEDENT><DEDENT>else:<EOL><INDENT>func = args[<NUM_LIT:0>]<EOL>try:<EOL><INDENT>func(*args[<NUM_LIT:1>:], **kwargs)<EOL><DEDENT>except expected_exception:<EOL><INDENT>return _pytest._code.ExceptionInfo.from_current()<EOL><DEDENT><DEDENT>fail(message)<EOL>
r"""
    Assert that a code block/function call raises ``expected_exception``
    or raise a failure exception otherwise.

    :kwparam match: if specified, asserts that the exception matches a text or regex

    :kwparam message: **(deprecated since 4.1)** if specified, provides a custom failure message
        if the exception is not raised

    .. currentmodule:: _pytest._code

    Use ``pytest.raises`` as a context manager, which will capture the exception of the given
    type::

        >>> with raises(ZeroDivisionError):
        ...    1/0

    If the code block does not raise the expected exception (``ZeroDivisionError`` in the example
    above), or no exception at all, the check will fail instead.

    You can also use the keyword argument ``match`` to assert that the
    exception matches a text or regex::

        >>> with raises(ValueError, match='must be 0 or None'):
        ...     raise ValueError("value must be 0 or None")

        >>> with raises(ValueError, match=r'must be \d+$'):
        ...     raise ValueError("value must be 42")

    The context manager produces an :class:`ExceptionInfo` object which can be used to inspect the
    details of the captured exception::

        >>> with raises(ValueError) as exc_info:
        ...     raise ValueError("value must be 42")
        >>> assert exc_info.type is ValueError
        >>> assert exc_info.value.args[0] == "value must be 42"

    .. deprecated:: 4.1

        In the context manager form you may use the keyword argument
        ``message`` to specify a custom failure message that will be displayed
        in case the ``pytest.raises`` check fails. This has been deprecated as it
        is considered error prone as users often mean to use ``match`` instead.

    .. note::

       When using ``pytest.raises`` as a context manager, it's worthwhile to
       note that normal context manager rules apply and that the exception
       raised *must* be the final line in the scope of the context manager.
       Lines of code after that, within the scope of the context manager will
       not be executed. For example::

           >>> value = 15
           >>> with raises(ValueError) as exc_info:
           ...     if value > 10:
           ...         raise ValueError("value must be <= 10")
           ...     assert exc_info.type is ValueError  # this will not execute

       Instead, the following approach must be taken (note the difference in
       scope)::

           >>> with raises(ValueError) as exc_info:
           ...     if value > 10:
           ...         raise ValueError("value must be <= 10")
           ...
           >>> assert exc_info.type is ValueError

    **Using with** ``pytest.mark.parametrize``

    When using :ref:`pytest.mark.parametrize ref`
    it is possible to parametrize tests such that
    some runs raise an exception and others do not.

    See :ref:`parametrizing_conditional_raising` for an example.

    **Legacy form**

    It is possible to specify a callable by passing a to-be-called lambda::

        >>> raises(ZeroDivisionError, lambda: 1/0)
        <ExceptionInfo ...>

    or you can specify an arbitrary callable with arguments::

        >>> def f(x): return 1/x
        ...
        >>> raises(ZeroDivisionError, f, 0)
        <ExceptionInfo ...>
        >>> raises(ZeroDivisionError, f, x=0)
        <ExceptionInfo ...>

    The form above is fully supported but discouraged for new code because the
    context manager form is regarded as more readable and less error-prone.

    .. note::
        Similar to caught exception objects in Python, explicitly clearing
        local references to returned ``ExceptionInfo`` objects can
        help the Python interpreter speed up its garbage collection.

        Clearing those references breaks a reference cycle
        (``ExceptionInfo`` --> caught exception --> frame stack raising
        the exception --> current frame stack --> local variables -->
        ``ExceptionInfo``) which makes Python keep all objects referenced
        from that cycle (including all local variables in the current
        frame) alive until the next cyclic garbage collection run. See the
        official Python ``try`` statement documentation for more detailed
        information.