class_docstr: Tests just-in-time cpp extensions.
  Don't confuse this with the PyTorch JIT (aka TorchScript).
code: def test_warning(self):
      # Note: the module created from this source will include the py::key_error
      # symbol. But because of visibility and the fact that it lives in a
      # different compilation unit than pybind, this trips up ubsan even though
      # it is fine. "ubsan.supp" thus needs to contain "vptr:warn_mod.so".
      source = """
          // error_type:
          // 0: no error
          // 1: torch::TypeError
          // 2: python_error()
          // 3: py::error_already_set
          at::Tensor foo(at::Tensor x, int error_type) {
              std::ostringstream err_stream;
              err_stream << "Error with "  << x.type();
  
              TORCH_WARN(err_stream.str());
              if(error_type == 1) {
                  throw torch::TypeError(err_stream.str().c_str());
              }
              if(error_type == 2) {
                  PyObject* obj = PyTuple_New(-1);
                  TORCH_CHECK(!obj);
                  // Pretend it was caught in a different thread and restored here
                  auto e = python_error();
                  e.persist();
                  e.restore();
                  throw e;
              }
              if(error_type == 3) {
                  throw py::key_error(err_stream.str());
              }
              return x.cos();
          }
          """
  
      # Ensure double type for hard-coded c name below
      t = torch.rand(2).double()
      cpp_tensor_name = r"CPUDoubleType"
  
      # Without error handling, the warnings cannot be catched
      warn_mod = torch.utils.cpp_extension.load_inline(
          name="warn_mod",
          cpp_sources=[source],
          functions=["foo"],
          with_pytorch_error_handling=False,
      )
  
      with warnings.catch_warnings(record=True) as w:
          warn_mod.foo(t, 0)
          self.assertEqual(len(w), 0)
  
          with self.assertRaisesRegex(TypeError, t.type()):
              warn_mod.foo(t, 1)
          self.assertEqual(len(w), 0)
  
          with self.assertRaisesRegex(
              SystemError, "bad argument to internal function"
          ):
              warn_mod.foo(t, 2)
          self.assertEqual(len(w), 0)
  
          with self.assertRaisesRegex(KeyError, cpp_tensor_name):
              warn_mod.foo(t, 3)
          self.assertEqual(len(w), 0)
  
      warn_mod = torch.utils.cpp_extension.load_inline(
          name="warn_mod",
          cpp_sources=[source],
          functions=["foo"],
          with_pytorch_error_handling=True,
      )
  
      with warnings.catch_warnings(record=True) as w:
          # Catched with no error should be detected
          warn_mod.foo(t, 0)
          self.assertEqual(len(w), 1)
  
          # Catched with cpp error should also be detected
          with self.assertRaisesRegex(TypeError, t.type()):
              warn_mod.foo(t, 1)
          self.assertEqual(len(w), 2)
  
          # Catched with python error should also be detected
          with self.assertRaisesRegex(
              SystemError, "bad argument to internal function"
          ):
              warn_mod.foo(t, 2)
          self.assertEqual(len(w), 3)
  
          # Catched with pybind error should also be detected
          # Note that there is no type name translation for pybind errors
          with self.assertRaisesRegex(KeyError, cpp_tensor_name):
              warn_mod.foo(t, 3)
          self.assertEqual(len(w), 4)
  
      # Make sure raising warnings are handled properly
      with warnings.catch_warnings(record=True) as w:
          warnings.simplefilter("error")
  
          # No error, the warning should raise
          with self.assertRaisesRegex(UserWarning, t.type()):
              warn_mod.foo(t, 0)
          self.assertEqual(len(w), 0)
  
          # Another error happened, the warning is ignored
          with self.assertRaisesRegex(TypeError, t.type()):
              warn_mod.foo(t, 1)
          self.assertEqual(len(w), 0)
  
imports: import os
  import shutil
  import sys
  import unittest
  import warnings
  import re
  import tempfile
  import subprocess
  import glob
  import torch.testing._internal.common_utils
  from torch.testing._internal.common_cuda import TEST_CUDNN, TEST_CUDA
  import torch
  import torch.backends.cudnn
  import torch.utils.cpp_extension
  from torch.utils.cpp_extension import CUDA_HOME, ROCM_HOME
  from torch.testing._internal.common_utils import gradcheck
  import torch.multiprocessing
  from torch.utils.cpp_extension import _TORCH_PATH, remove_extension_h_precompiler_headers, get_cxx_compiler, check_compiler_is_gcc
label_desc: Use image node_id 27 for calling the TestCppExtensionJIT obj's underlying member method code with example usage: obj.test_warning() without return types