-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathtest_exception.py
More file actions
84 lines (64 loc) · 2.35 KB
/
Copy pathtest_exception.py
File metadata and controls
84 lines (64 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import sys
import tempfile
import traceback
from pathlib import Path
from torch.utils.cpp_extension import load_inline
ROOT = Path(__file__).resolve().parents[1]
CPP_SOURCE = r'''
#line 1 "embedded_exception_test.cpp"
#include <pybind11/pybind11.h>
#include <deep_jit/utils/exception.hpp>
namespace deep_jit::exception_test {
__attribute__((noinline, visibility("default"))) void panic_leaf() {
DJ_PANIC("test failure from pybind11");
}
__attribute__((noinline, visibility("default"))) void panic_middle() {
panic_leaf();
}
__attribute__((noinline, visibility("default"))) void panic_entry() {
panic_middle();
}
} // namespace deep_jit::exception_test
PYBIND11_MODULE(TORCH_EXTENSION_NAME, module) {
module.def("raise_exception", &deep_jit::exception_test::panic_entry);
}
'''
def python_entry(module):
module.raise_exception()
def main() -> None:
with tempfile.TemporaryDirectory(prefix='deep-jit-exception-') as directory:
module = load_inline(
name='deep_jit_exception_test',
cpp_sources=CPP_SOURCE,
extra_cflags=[
'-std=c++20',
'-O3',
'-g1',
'-fno-omit-frame-pointer',
'-fno-optimize-sibling-calls',
],
extra_include_paths=[str(ROOT / 'include')],
build_directory=directory,
with_cuda=False,
verbose=True,
)
try:
python_entry(module)
except RuntimeError:
python_traceback = traceback.format_exc()
else:
raise AssertionError('the pybind11 call did not raise RuntimeError')
print(python_traceback, file=sys.stderr, end='')
assert 'Traceback (most recent call last):' in python_traceback
assert 'in python_entry' in python_traceback
assert (
'RuntimeError: Panic error (embedded_exception_test.cpp:8): '
'test failure from pybind11\n'
'C++ trace (most recent call first):\n'
) in python_traceback
assert 'deep_jit::exception_test::panic_leaf()' in python_traceback
assert 'deep_jit::exception_test::panic_middle()' in python_traceback
assert 'deep_jit::exception_test::panic_entry()' in python_traceback
assert 'embedded_exception_test.cpp:' in python_traceback
if __name__ == '__main__':
main()