code: def raises(expected_exception, *args, **kwargs):
      r"""
      Assert that a code block/function call raises ``expected_exception``
      or raise a failure exception otherwise.
  
      :kwparam match: if specified, a string containing a regular expression,
          or a regular expression object, that is tested against the string
          representation of the exception using ``re.search``. To match a literal
          string that may contain `special characters`__, the pattern can
          first be escaped with ``re.escape``.
  
      __ https://docs.python.org/3/library/re.html#regular-expression-syntax
  
      :kwparam message: **(deprecated since 4.1)** if specified, provides a custom failure message
          if the exception is not raised. See :ref:`the deprecation docs <raises message deprecated>` for a workaround.
  
      .. 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.
          See :ref:`the deprecation docs <raises message deprecated>` for a workaround.
  
      .. 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.
  
      """
      __tracebackhide__ = True
      for exc in filterfalse(
          isclass, always_iterable(expected_exception, BASE_TYPE)
      ):
          msg = (
              "exceptions must be old-style classes or"
              " derived from BaseException, not %s"
          )
          raise TypeError(msg % type(exc))
  
      message = "DID NOT RAISE {}".format(expected_exception)
      match_expr = None
  
      if not args:
          if "message" in kwargs:
              message = kwargs.pop("message")
              warnings.warn(
                  deprecated.RAISES_MESSAGE_PARAMETER, stacklevel=2
              )
          if "match" in kwargs:
              match_expr = kwargs.pop("match")
          if kwargs:
              msg = "Unexpected keyword arguments passed to pytest.raises: "
              msg += ", ".join(sorted(kwargs))
              raise TypeError(msg)
          return RaisesContext(expected_exception, message, match_expr)
      elif isinstance(args[0], str):
          warnings.warn(deprecated.RAISES_EXEC, stacklevel=2)
          (code,) = args
          assert isinstance(code, str)
          frame = sys._getframe(1)
          loc = frame.f_locals.copy()
          loc.update(kwargs)
          # print "raises frame scope: %r" % frame.f_locals
          try:
              code = _pytest._code.Source(code).compile(_genframe=frame)
              exec(code, frame.f_globals, loc)
              # XXX didn't mean f_globals == f_locals something special?
              #     this is destroyed here ...
          except expected_exception:
              return _pytest._code.ExceptionInfo.from_current()
      else:
          func = args[0]
          try:
              func(*args[1:], **kwargs)
          except expected_exception:
              return _pytest._code.ExceptionInfo.from_current()
      fail(message)
  
imports: from __future__ import absolute_import
  import math
  import pprint
  import sys
  import warnings
  from decimal import Decimal
  from numbers import Number
  from more_itertools.more import always_iterable
  from six.moves import filterfalse
  from six.moves import zip
  import _pytest._code
  from _pytest import deprecated
  from _pytest.compat import isclass
  from _pytest.compat import Iterable
  from _pytest.compat import Mapping
  from _pytest.compat import Sized
  from _pytest.compat import STRING_TYPES
  from _pytest.outcomes import fail
label_desc: Use image node_id 6 for calling a global function with example usage: raises(expected_exception) and returns: RaisesContext, _pytest, _pytest