1 | use pyo3::exceptions::*; |
2 | |
3 | #[macro_export ] |
4 | macro_rules! create_exception { |
5 | ($module: expr, $name: ident, $base: ty) => { |
6 | $crate::create_exception!($module, $name, $base, "" ); |
7 | }; |
8 | ($module: expr, $name: ident, $base: ty, $doc: expr) => { |
9 | ::pyo3::create_exception!($module, $name, $base, $doc); |
10 | |
11 | $crate::inventory::submit! { |
12 | $crate::type_info::PyErrorInfo { |
13 | name: stringify!($name), |
14 | module: stringify!($module), |
15 | base: <$base as $crate::exception::NativeException>::type_name, |
16 | } |
17 | } |
18 | }; |
19 | } |
20 | |
21 | /// Native exceptions in Python |
22 | pub trait NativeException { |
23 | /// Type name in Python side |
24 | fn type_name() -> &'static str; |
25 | } |
26 | |
27 | macro_rules! impl_native_exception { |
28 | ($name:ident, $type_name:literal) => { |
29 | impl NativeException for $name { |
30 | fn type_name() -> &'static str { |
31 | $type_name |
32 | } |
33 | } |
34 | }; |
35 | } |
36 | |
37 | impl_native_exception!(PyArithmeticError, "ArithmeticError" ); |
38 | impl_native_exception!(PyAssertionError, "AssertionError" ); |
39 | impl_native_exception!(PyAttributeError, "AttributeError" ); |
40 | impl_native_exception!(PyBaseException, "BaseException" ); |
41 | impl_native_exception!(PyBlockingIOError, "BlockingIOError" ); |
42 | impl_native_exception!(PyBrokenPipeError, "BrokenPipeError" ); |
43 | impl_native_exception!(PyBufferError, "BufferError" ); |
44 | impl_native_exception!(PyBytesWarning, "BytesWarning" ); |
45 | impl_native_exception!(PyChildProcessError, "ChildProcessError" ); |
46 | impl_native_exception!(PyConnectionAbortedError, "ConnectionAbortedError" ); |
47 | impl_native_exception!(PyConnectionError, "ConnectionError" ); |
48 | impl_native_exception!(PyConnectionRefusedError, "ConnectionRefusedError" ); |
49 | impl_native_exception!(PyConnectionResetError, "ConnectionResetError" ); |
50 | impl_native_exception!(PyDeprecationWarning, "DeprecationWarning" ); |
51 | impl_native_exception!(PyEOFError, "EOFError" ); |
52 | // FIXME: This only exists in Python 3.10+. |
53 | // We need to find a way to conditionally compile this. |
54 | // impl_native_exception!(PyEncodingWarning, "EncodingWarning"); |
55 | impl_native_exception!(PyEnvironmentError, "EnvironmentError" ); |
56 | impl_native_exception!(PyException, "Exception" ); |
57 | impl_native_exception!(PyFileExistsError, "FileExistsError" ); |
58 | impl_native_exception!(PyFileNotFoundError, "FileNotFoundError" ); |
59 | impl_native_exception!(PyFloatingPointError, "FloatingPointError" ); |
60 | impl_native_exception!(PyFutureWarning, "FutureWarning" ); |
61 | impl_native_exception!(PyGeneratorExit, "GeneratorExit" ); |
62 | impl_native_exception!(PyIOError, "IOError" ); |
63 | impl_native_exception!(PyImportError, "ImportError" ); |
64 | impl_native_exception!(PyImportWarning, "ImportWarning" ); |
65 | impl_native_exception!(PyIndexError, "IndexError" ); |
66 | impl_native_exception!(PyInterruptedError, "InterruptedError" ); |
67 | impl_native_exception!(PyIsADirectoryError, "IsADirectoryError" ); |
68 | impl_native_exception!(PyKeyError, "KeyError" ); |
69 | impl_native_exception!(PyKeyboardInterrupt, "KeyboardInterrupt" ); |
70 | impl_native_exception!(PyLookupError, "LookupError" ); |
71 | impl_native_exception!(PyMemoryError, "MemoryError" ); |
72 | impl_native_exception!(PyModuleNotFoundError, "ModuleNotFoundError" ); |
73 | impl_native_exception!(PyNameError, "NameError" ); |
74 | impl_native_exception!(PyNotADirectoryError, "NotADirectoryError" ); |
75 | impl_native_exception!(PyNotImplementedError, "NotImplementedError" ); |
76 | impl_native_exception!(PyOSError, "OSError" ); |
77 | impl_native_exception!(PyOverflowError, "OverflowError" ); |
78 | impl_native_exception!(PyPendingDeprecationWarning, "PendingDeprecationWarning" ); |
79 | impl_native_exception!(PyPermissionError, "PermissionError" ); |
80 | impl_native_exception!(PyProcessLookupError, "ProcessLookupError" ); |
81 | impl_native_exception!(PyRecursionError, "RecursionError" ); |
82 | impl_native_exception!(PyReferenceError, "ReferenceError" ); |
83 | impl_native_exception!(PyResourceWarning, "ResourceWarning" ); |
84 | impl_native_exception!(PyRuntimeError, "RuntimeError" ); |
85 | impl_native_exception!(PyRuntimeWarning, "RuntimeWarning" ); |
86 | impl_native_exception!(PyStopAsyncIteration, "StopAsyncIteration" ); |
87 | impl_native_exception!(PyStopIteration, "StopIteration" ); |
88 | impl_native_exception!(PySyntaxError, "SyntaxError" ); |
89 | impl_native_exception!(PySyntaxWarning, "SyntaxWarning" ); |
90 | impl_native_exception!(PySystemError, "SystemError" ); |
91 | impl_native_exception!(PySystemExit, "SystemExit" ); |
92 | impl_native_exception!(PyTimeoutError, "TimeoutError" ); |
93 | impl_native_exception!(PyTypeError, "TypeError" ); |
94 | impl_native_exception!(PyUnboundLocalError, "UnboundLocalError" ); |
95 | impl_native_exception!(PyUnicodeDecodeError, "UnicodeDecodeError" ); |
96 | impl_native_exception!(PyUnicodeEncodeError, "UnicodeEncodeError" ); |
97 | impl_native_exception!(PyUnicodeError, "UnicodeError" ); |
98 | impl_native_exception!(PyUnicodeTranslateError, "UnicodeTranslateError" ); |
99 | impl_native_exception!(PyUnicodeWarning, "UnicodeWarning" ); |
100 | impl_native_exception!(PyUserWarning, "UserWarning" ); |
101 | impl_native_exception!(PyValueError, "ValueError" ); |
102 | impl_native_exception!(PyWarning, "Warning" ); |
103 | impl_native_exception!(PyZeroDivisionError, "ZeroDivisionError" ); |
104 | |