pyo3_stub_gen/
exception.rs

1use pyo3::exceptions::*;
2
3#[macro_export]
4macro_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
22pub trait NativeException {
23    /// Type name in Python side
24    fn type_name() -> &'static str;
25}
26
27macro_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
37impl_native_exception!(PyArithmeticError, "ArithmeticError");
38impl_native_exception!(PyAssertionError, "AssertionError");
39impl_native_exception!(PyAttributeError, "AttributeError");
40impl_native_exception!(PyBaseException, "BaseException");
41impl_native_exception!(PyBlockingIOError, "BlockingIOError");
42impl_native_exception!(PyBrokenPipeError, "BrokenPipeError");
43impl_native_exception!(PyBufferError, "BufferError");
44impl_native_exception!(PyBytesWarning, "BytesWarning");
45impl_native_exception!(PyChildProcessError, "ChildProcessError");
46impl_native_exception!(PyConnectionAbortedError, "ConnectionAbortedError");
47impl_native_exception!(PyConnectionError, "ConnectionError");
48impl_native_exception!(PyConnectionRefusedError, "ConnectionRefusedError");
49impl_native_exception!(PyConnectionResetError, "ConnectionResetError");
50impl_native_exception!(PyDeprecationWarning, "DeprecationWarning");
51impl_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");
55impl_native_exception!(PyEnvironmentError, "EnvironmentError");
56impl_native_exception!(PyException, "Exception");
57impl_native_exception!(PyFileExistsError, "FileExistsError");
58impl_native_exception!(PyFileNotFoundError, "FileNotFoundError");
59impl_native_exception!(PyFloatingPointError, "FloatingPointError");
60impl_native_exception!(PyFutureWarning, "FutureWarning");
61impl_native_exception!(PyGeneratorExit, "GeneratorExit");
62impl_native_exception!(PyIOError, "IOError");
63impl_native_exception!(PyImportError, "ImportError");
64impl_native_exception!(PyImportWarning, "ImportWarning");
65impl_native_exception!(PyIndexError, "IndexError");
66impl_native_exception!(PyInterruptedError, "InterruptedError");
67impl_native_exception!(PyIsADirectoryError, "IsADirectoryError");
68impl_native_exception!(PyKeyError, "KeyError");
69impl_native_exception!(PyKeyboardInterrupt, "KeyboardInterrupt");
70impl_native_exception!(PyLookupError, "LookupError");
71impl_native_exception!(PyMemoryError, "MemoryError");
72impl_native_exception!(PyModuleNotFoundError, "ModuleNotFoundError");
73impl_native_exception!(PyNameError, "NameError");
74impl_native_exception!(PyNotADirectoryError, "NotADirectoryError");
75impl_native_exception!(PyNotImplementedError, "NotImplementedError");
76impl_native_exception!(PyOSError, "OSError");
77impl_native_exception!(PyOverflowError, "OverflowError");
78impl_native_exception!(PyPendingDeprecationWarning, "PendingDeprecationWarning");
79impl_native_exception!(PyPermissionError, "PermissionError");
80impl_native_exception!(PyProcessLookupError, "ProcessLookupError");
81impl_native_exception!(PyRecursionError, "RecursionError");
82impl_native_exception!(PyReferenceError, "ReferenceError");
83impl_native_exception!(PyResourceWarning, "ResourceWarning");
84impl_native_exception!(PyRuntimeError, "RuntimeError");
85impl_native_exception!(PyRuntimeWarning, "RuntimeWarning");
86impl_native_exception!(PyStopAsyncIteration, "StopAsyncIteration");
87impl_native_exception!(PyStopIteration, "StopIteration");
88impl_native_exception!(PySyntaxError, "SyntaxError");
89impl_native_exception!(PySyntaxWarning, "SyntaxWarning");
90impl_native_exception!(PySystemError, "SystemError");
91impl_native_exception!(PySystemExit, "SystemExit");
92impl_native_exception!(PyTimeoutError, "TimeoutError");
93impl_native_exception!(PyTypeError, "TypeError");
94impl_native_exception!(PyUnboundLocalError, "UnboundLocalError");
95impl_native_exception!(PyUnicodeDecodeError, "UnicodeDecodeError");
96impl_native_exception!(PyUnicodeEncodeError, "UnicodeEncodeError");
97impl_native_exception!(PyUnicodeError, "UnicodeError");
98impl_native_exception!(PyUnicodeTranslateError, "UnicodeTranslateError");
99impl_native_exception!(PyUnicodeWarning, "UnicodeWarning");
100impl_native_exception!(PyUserWarning, "UserWarning");
101impl_native_exception!(PyValueError, "ValueError");
102impl_native_exception!(PyWarning, "Warning");
103impl_native_exception!(PyZeroDivisionError, "ZeroDivisionError");