Repository: pytorch
Issue: [BUG][JIT] `torch.jit.script` is not compatible with `DeprecationWarning` and `FutureWarning`
Labels: oncall: jit

### 🐛 Describe the bug

`torch.jit.script` is not compatible with `warnings.warn` when the warning category is specified. It raises a `RuntimeError` in C bindings:

```
RuntimeError: Python exception cannot be used as a value
```

`Warning` is a subclass of `Exception`:

```python
In [1]: UserWarning.__mro__
Out[1]: (<class 'UserWarning'>, <class 'Warning'>, <class 'Exception'>, <class 'BaseException'>, <class 'object'>)
```

Reference: 

- #126898
- https://github.com/pytorch/pytorch/pull/126898#discussion_r1616292207
- Commit: Remove `FutureWarning` for JIT compatibility c2a843d21850552d07012e07b276490a4d1db52c

```python
In [1]: import warnings

In [2]: import torch

In [3]: def func0(x):
   ...:     warnings.warn("func0 is deprecated")  # UserWarning is the default category
   ...:     return x
   ...:

In [4]: torch.jit.script(func0)  # OK
Out[4]: <torch.jit.ScriptFunction object at 0x7f0f9a1dccb0>

In [5]: def func1(x):
   ...:     warnings.warn("func1 is deprecated", UserWarning)
   ...:     return x
   ...:

In [6]: torch.jit.script(func1)
╭───────────────────────────────────────────── Traceback (most recent call last) ──────────────────────────────────────────────╮
│ <ipython-input-6-501924eff1a7>:1 in <module>                                                                                 │
│                                                                                                                              │
│ /home/PanXuehai/Projects/pytorch/torch/jit/_script.py:1429 in script                                                         │
│                                                                                                                              │
│   1426 │   _TOPLEVEL = False                                                                                                 │
│   1427 │                                                                                                                     │
│   1428 │   try:                                                                                                              │
│ ❱ 1429 │   │   return _script_impl(                                                                                          │
│   1430 │   │   │   obj=obj,                                                                                                  │
│   1431 │   │   │   optimize=optimize,                                                                                        │
│   1432 │   │   │   _frames_up=_frames_up + 1,                                                                                │
│                                                                                                                              │
│ /home/PanXuehai/Projects/pytorch/torch/jit/_script.py:1201 in _script_impl                                                   │
│                                                                                                                              │
│   1198 │   │   ast = get_jit_def(obj, obj.__name__)                                                                          │
│   1199 │   │   if _rcb is None:                                                                                              │
│   1200 │   │   │   _rcb = _jit_internal.createResolutionCallbackFromClosure(obj)                                             │
│ ❱ 1201 │   │   fn = torch._C._jit_script_compile(                                                                            │
│   1202 │   │   │   qualified_name, ast, _rcb, get_default_args(obj)                                                          │
│   1203 │   │   )                                                                                                             │
│   1204 │   │   # Forward docstrings                                                                                          │
╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
RuntimeError:
Python exception cannot be used as a value:
  File "<ipython-input-5-c4f0f2070b6c>", line 2
def func1(x):
    warnings.warn("func1 is deprecated", UserWarning)
                                         ~~~~~~~~~~~ <--- HERE
    return x


In [7]: def func2(x):
   ...:     warnings.warn("func2 is deprecated", DeprecationWarning)
   ...:     return x
   ...:

In [8]: torch.jit.script(func2)
╭───────────────────────────────────────────── Traceback (most recent call last) ──────────────────────────────────────────────╮
│ <ipython-input-8-9c358e7e7293>:1 in <module>                                                                                 │
│                                                                                                                              │
│ /home/PanXuehai/Projects/pytorch/torch/jit/_script.py:1429 in script                                                         │
│                                                                                                                              │
│   1426 │   _TOPLEVEL = False                                                                                                 │
│   1427 │                                                                                                                     │
│   1428 │   try:                                                                                                              │
│ ❱ 1429 │   │   return _script_impl(                                                                                          │
│   1430 │   │   │   obj=obj,                                                                                                  │
│   1431 │   │   │   optimize=optimize,                                                                                        │
│   1432 │   │   │   _frames_up=_frames_up + 1,                                                                                │
│                                                                                                                              │
│ /home/PanXuehai/Projects/pytorch/torch/jit/_script.py:1201 in _script_impl                                                   │
│                                                                                                                              │
│   1198 │   │   ast = get_jit_def(obj, obj.__name__)                                                                          │
│   1199 │   │   if _rcb is None:                                                                                              │
│   1200 │   │   │   _rcb = _jit_internal.createResolutionCallbackFromClosure(obj)                                             │
│ ❱ 1201 │   │   fn = torch._C._jit_script_compile(                                                                            │
│   1202 │   │   │   qualified_name, ast, _rcb, get_default_args(obj)                                                          │
│   1203 │   │   )                                                                                                             │
│   1204 │   │   # Forward docstrings                                                                                          │
╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
RuntimeError:
Python exception cannot be used as a value:
  File "<ipython-input-7-b51cafdeee6e>", line 2
def func2(x):
    warnings.warn("func2 is deprecated", DeprecationWarning)
                                         ~~~~~~~~~~~~~~~~~~ <--- HERE
    return x


In [9]: def func3(x):
   ...:     warnings.warn("func3 is deprecated", FutureWarning)
   ...:     return x
   ...:

In [10]: torch.jit.script(func3)
╭───────────────────────────────────────────── Traceback (most recent call last) ──────────────────────────────────────────────╮
│ <ipython-input-10-dd37498f163c>:1 in <module>                                                                                │
│                                                                                                                              │
│ /home/PanXuehai/Projects/pytorch/torch/jit/_script.py:1429 in script                                                         │
│                                                                                                                              │
│   1426 │   _TOPLEVEL = False                                                                                                 │
│   1427 │                                                                                                                     │
│   1428 │   try:                                                                                                              │
│ ❱ 1429 │   │   return _script_impl(                                                                                          │
│   1430 │   │   │   obj=obj,                                                                                                  │
│   1431 │   │   │   optimize=optimize,                                                                                        │
│   1432 │   │   │   _frames_up=_frames_up + 1,                                                                                │
│                                                                                                                              │
│ /home/PanXuehai/Projects/pytorch/torch/jit/_script.py:1201 in _script_impl                                                   │
│                                                                                                                              │
│   1198 │   │   ast = get_jit_def(obj, obj.__name__)                                                                          │
│   1199 │   │   if _rcb is None:                                                                                              │
│   1200 │   │   │   _rcb = _jit_internal.createResolutionCallbackFromClosure(obj)                                             │
│ ❱ 1201 │   │   fn = torch._C._jit_script_compile(                                                                            │
│   1202 │   │   │   qualified_name, ast, _rcb, get_default_args(obj)                                                          │
│   1203 │   │   )                                                                                                             │
│   1204 │   │   # Forward docstrings                                                                                          │
╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
RuntimeError:
Python exception cannot be used as a value:
  File "<ipython-input-9-4b6a0e0081f1>", line 2
def func3(x):
    warnings.warn("func3 is deprecated", FutureWarning)
                                         ~~~~~~~~~~~~~ <--- HERE
    return x
```

### Versions

nightly

cc @EikanWang @jgong5 @wenzhe-nrv @sanchitintel