class_docstr: A suite of tests for with statements.
docstr: Check that exceptions thrown in the bodies of with-statements are
  handled correctly.
docstr_tok: ["Check","that","exceptions","thrown","in","the","bodies","of","with-statements","are","handled","correctly","."]
code: def test_with_exceptions(self):
      """
      Check that exceptions thrown in the bodies of with-statements are
      handled correctly.
      """
  
      @torch.jit.script
      class Context:
          """
          This class implements a basic context manager interface for use in
          the unit tests. Unlike Context, the stateful part of this class
          is a Tensor that is mutated in-place so that modifications made in the
          JIT interpreter are visible outside of it.
          """
  
          def __init__(self, start: int):
              self.count = torch.tensor([start], dtype=torch.double)
  
          def __enter__(self):
              self.count.add_(0.3)
              return self.count
  
          def __exit__(self, type: Any, value: Any, tb: Any):
              self.count.sub_(0.3)
  
      make_global(Context)
  
      @torch.jit.script
      def method_that_raises() -> torch.Tensor:
          raise Exception("raised exception")
  
      @torch.jit.script
      def test_exception(x: torch.Tensor, c: Context) -> torch.Tensor:
          """
          Test the case in which an exception is thrown while executing the body of a with-statement.
          """
          with c as _:
              x += method_that_raises()
  
          return x
  
      @torch.jit.script
      def test_exception_nested(
          x: torch.Tensor, c: Context
      ) -> torch.Tensor:
          """
          Test the case in which an exception is thrown while executing the body of a nested with-statement.
          """
          with c as _:
              with c as _:
                  x += method_that_raises()
  
          return x
  
      @torch.jit.script
      def with_that_raises(c: Context) -> torch.Tensor:
          a = torch.tensor([1])
  
          with c as _:
              a += method_that_raises()
  
          return a
  
      @torch.jit.script
      def test_exception_fn_call(
          x: torch.Tensor, c: Context
      ) -> torch.Tensor:
          """
          Test the case in which an exception is thrown while there are active with-statements in two different
          frames.
          """
          with c as _:
              x += with_that_raises(c)
  
          return x
  
      c = Context(1)
  
      # checkScript and checkScriptRaisesRegex cannot be used because the string frontend will
      # not compile class types (of which Context, the context manager being used for this test
      # is one).
      with self.assertRaisesRegexWithHighlight(
          Exception,
          r"raised exception",
          'raise Exception("raised exception',
      ):
          test_exception(torch.randn(2), c)
      self.assertEqual(c.count, 1)
  
      with self.assertRaisesRegexWithHighlight(
          Exception,
          r"raised exception",
          'raise Exception("raised exception',
      ):
          test_exception_nested(torch.randn(2), c)
      self.assertEqual(c.count, 1)
  
      with self.assertRaisesRegexWithHighlight(
          Exception,
          r"raised exception",
          'raise Exception("raised exception',
      ):
          test_exception_fn_call(torch.randn(2), c)
      self.assertEqual(c.count, 1)
  
file_path: pytorch/test/jit/test_with.py
imports: import os
  import sys
  from typing import Any, List
  import torch
  from torch.testing._internal.common_utils import skipIfTorchDynamo
  from torch.testing._internal.jit_utils import JitTestCase, make_global
label_desc: Use image node_id 3 for calling the TestWith obj's underlying member method code with example usage: obj.test_with_exceptions() and returns: x, x, a, x, self