pyo3_stub_gen/
exception.rs

1use pyo3::exceptions::*;
2
3/// Wrapper of [pyo3::create_exception] macro to create a custom exception with [crate::PyStubType] support.
4///
5/// Note
6/// -----
7/// [pyo3::create_exception!] macro creates a new exception type as [pyo3::PyErr],
8/// which does not implement [pyo3::PyClass] trait. So it is not a "class" in PyO3 sense,
9/// but we create a [crate::type_info::PyClassInfo] since it will be treated as a class eventually in Python side.
10#[macro_export]
11macro_rules! create_exception {
12    ($module: expr, $name: ident, $base: ty) => {
13        $crate::create_exception!($module, $name, $base, "");
14    };
15    ($module: expr, $name: ident, $base: ty, $doc: expr) => {
16        ::pyo3::create_exception!($module, $name, $base, $doc);
17
18        // Add PyStubType implementation for the created exception
19        impl $crate::PyStubType for $name {
20            fn type_output() -> $crate::TypeInfo {
21                $crate::TypeInfo::builtin(stringify!($name))
22            }
23        }
24
25        $crate::impl_py_runtime_type!($name);
26
27        $crate::inventory::submit! {
28            $crate::type_info::PyClassInfo {
29                pyclass_name: stringify!($name),
30                struct_id: std::any::TypeId::of::<$name>,
31                getters: &[],
32                setters: &[],
33                module: Some(stringify!($module)),
34                doc: $doc,
35                bases: &[|| <$base as $crate::PyStubType>::type_output()],
36                has_eq: false,
37                has_ord: false,
38                has_hash: false,
39                has_str: false,
40                subclass: true,
41            }
42        }
43    };
44}
45
46// Direct PyStubType and PyRuntimeType implementations for PyO3 exception types
47macro_rules! impl_exception_stub_type {
48    ($name:ident, $type_name:literal) => {
49        impl crate::PyStubType for $name {
50            fn type_output() -> crate::TypeInfo {
51                crate::TypeInfo::builtin($type_name)
52            }
53        }
54        impl crate::runtime::PyRuntimeType for $name {
55            fn runtime_type_object(
56                py: ::pyo3::Python<'_>,
57            ) -> ::pyo3::PyResult<::pyo3::Bound<'_, ::pyo3::PyAny>> {
58                Ok(py.get_type::<$name>().into_any())
59            }
60        }
61    };
62}
63
64impl_exception_stub_type!(PyArithmeticError, "ArithmeticError");
65impl_exception_stub_type!(PyAssertionError, "AssertionError");
66impl_exception_stub_type!(PyAttributeError, "AttributeError");
67impl_exception_stub_type!(PyBaseException, "BaseException");
68impl_exception_stub_type!(PyBlockingIOError, "BlockingIOError");
69impl_exception_stub_type!(PyBrokenPipeError, "BrokenPipeError");
70impl_exception_stub_type!(PyBufferError, "BufferError");
71impl_exception_stub_type!(PyBytesWarning, "BytesWarning");
72impl_exception_stub_type!(PyChildProcessError, "ChildProcessError");
73impl_exception_stub_type!(PyConnectionAbortedError, "ConnectionAbortedError");
74impl_exception_stub_type!(PyConnectionError, "ConnectionError");
75impl_exception_stub_type!(PyConnectionRefusedError, "ConnectionRefusedError");
76impl_exception_stub_type!(PyConnectionResetError, "ConnectionResetError");
77impl_exception_stub_type!(PyDeprecationWarning, "DeprecationWarning");
78impl_exception_stub_type!(PyEOFError, "EOFError");
79impl_exception_stub_type!(PyEncodingWarning, "EncodingWarning");
80impl_exception_stub_type!(PyEnvironmentError, "EnvironmentError");
81impl_exception_stub_type!(PyException, "Exception");
82impl_exception_stub_type!(PyFileExistsError, "FileExistsError");
83impl_exception_stub_type!(PyFileNotFoundError, "FileNotFoundError");
84impl_exception_stub_type!(PyFloatingPointError, "FloatingPointError");
85impl_exception_stub_type!(PyFutureWarning, "FutureWarning");
86impl_exception_stub_type!(PyGeneratorExit, "GeneratorExit");
87impl_exception_stub_type!(PyIOError, "IOError");
88impl_exception_stub_type!(PyImportError, "ImportError");
89impl_exception_stub_type!(PyImportWarning, "ImportWarning");
90impl_exception_stub_type!(PyIndexError, "IndexError");
91impl_exception_stub_type!(PyInterruptedError, "InterruptedError");
92impl_exception_stub_type!(PyIsADirectoryError, "IsADirectoryError");
93impl_exception_stub_type!(PyKeyError, "KeyError");
94impl_exception_stub_type!(PyKeyboardInterrupt, "KeyboardInterrupt");
95impl_exception_stub_type!(PyLookupError, "LookupError");
96impl_exception_stub_type!(PyMemoryError, "MemoryError");
97impl_exception_stub_type!(PyModuleNotFoundError, "ModuleNotFoundError");
98impl_exception_stub_type!(PyNameError, "NameError");
99impl_exception_stub_type!(PyNotADirectoryError, "NotADirectoryError");
100impl_exception_stub_type!(PyNotImplementedError, "NotImplementedError");
101impl_exception_stub_type!(PyOSError, "OSError");
102impl_exception_stub_type!(PyOverflowError, "OverflowError");
103impl_exception_stub_type!(PyPendingDeprecationWarning, "PendingDeprecationWarning");
104impl_exception_stub_type!(PyPermissionError, "PermissionError");
105impl_exception_stub_type!(PyProcessLookupError, "ProcessLookupError");
106impl_exception_stub_type!(PyRecursionError, "RecursionError");
107impl_exception_stub_type!(PyReferenceError, "ReferenceError");
108impl_exception_stub_type!(PyResourceWarning, "ResourceWarning");
109impl_exception_stub_type!(PyRuntimeError, "RuntimeError");
110impl_exception_stub_type!(PyRuntimeWarning, "RuntimeWarning");
111impl_exception_stub_type!(PyStopAsyncIteration, "StopAsyncIteration");
112impl_exception_stub_type!(PyStopIteration, "StopIteration");
113impl_exception_stub_type!(PySyntaxError, "SyntaxError");
114impl_exception_stub_type!(PySyntaxWarning, "SyntaxWarning");
115impl_exception_stub_type!(PySystemError, "SystemError");
116impl_exception_stub_type!(PySystemExit, "SystemExit");
117impl_exception_stub_type!(PyTimeoutError, "TimeoutError");
118impl_exception_stub_type!(PyTypeError, "TypeError");
119impl_exception_stub_type!(PyUnboundLocalError, "UnboundLocalError");
120impl_exception_stub_type!(PyUnicodeDecodeError, "UnicodeDecodeError");
121impl_exception_stub_type!(PyUnicodeEncodeError, "UnicodeEncodeError");
122impl_exception_stub_type!(PyUnicodeError, "UnicodeError");
123impl_exception_stub_type!(PyUnicodeTranslateError, "UnicodeTranslateError");
124impl_exception_stub_type!(PyUnicodeWarning, "UnicodeWarning");
125impl_exception_stub_type!(PyUserWarning, "UserWarning");
126impl_exception_stub_type!(PyValueError, "ValueError");
127impl_exception_stub_type!(PyWarning, "Warning");
128impl_exception_stub_type!(PyZeroDivisionError, "ZeroDivisionError");