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::inventory::submit! {
26            $crate::type_info::PyClassInfo {
27                pyclass_name: stringify!($name),
28                struct_id: std::any::TypeId::of::<$name>,
29                getters: &[],
30                setters: &[],
31                module: Some(stringify!($module)),
32                doc: $doc,
33                bases: &[|| <$base as $crate::PyStubType>::type_output()],
34                has_eq: false,
35                has_ord: false,
36                has_hash: false,
37                has_str: false,
38            }
39        }
40    };
41}
42
43// Direct PyStubType implementations for PyO3 exception types
44macro_rules! impl_exception_stub_type {
45    ($name:ident, $type_name:literal) => {
46        impl crate::PyStubType for $name {
47            fn type_output() -> crate::TypeInfo {
48                crate::TypeInfo::builtin($type_name)
49            }
50        }
51    };
52}
53
54impl_exception_stub_type!(PyArithmeticError, "ArithmeticError");
55impl_exception_stub_type!(PyAssertionError, "AssertionError");
56impl_exception_stub_type!(PyAttributeError, "AttributeError");
57impl_exception_stub_type!(PyBaseException, "BaseException");
58impl_exception_stub_type!(PyBlockingIOError, "BlockingIOError");
59impl_exception_stub_type!(PyBrokenPipeError, "BrokenPipeError");
60impl_exception_stub_type!(PyBufferError, "BufferError");
61impl_exception_stub_type!(PyBytesWarning, "BytesWarning");
62impl_exception_stub_type!(PyChildProcessError, "ChildProcessError");
63impl_exception_stub_type!(PyConnectionAbortedError, "ConnectionAbortedError");
64impl_exception_stub_type!(PyConnectionError, "ConnectionError");
65impl_exception_stub_type!(PyConnectionRefusedError, "ConnectionRefusedError");
66impl_exception_stub_type!(PyConnectionResetError, "ConnectionResetError");
67impl_exception_stub_type!(PyDeprecationWarning, "DeprecationWarning");
68impl_exception_stub_type!(PyEOFError, "EOFError");
69impl_exception_stub_type!(PyEncodingWarning, "EncodingWarning");
70impl_exception_stub_type!(PyEnvironmentError, "EnvironmentError");
71impl_exception_stub_type!(PyException, "Exception");
72impl_exception_stub_type!(PyFileExistsError, "FileExistsError");
73impl_exception_stub_type!(PyFileNotFoundError, "FileNotFoundError");
74impl_exception_stub_type!(PyFloatingPointError, "FloatingPointError");
75impl_exception_stub_type!(PyFutureWarning, "FutureWarning");
76impl_exception_stub_type!(PyGeneratorExit, "GeneratorExit");
77impl_exception_stub_type!(PyIOError, "IOError");
78impl_exception_stub_type!(PyImportError, "ImportError");
79impl_exception_stub_type!(PyImportWarning, "ImportWarning");
80impl_exception_stub_type!(PyIndexError, "IndexError");
81impl_exception_stub_type!(PyInterruptedError, "InterruptedError");
82impl_exception_stub_type!(PyIsADirectoryError, "IsADirectoryError");
83impl_exception_stub_type!(PyKeyError, "KeyError");
84impl_exception_stub_type!(PyKeyboardInterrupt, "KeyboardInterrupt");
85impl_exception_stub_type!(PyLookupError, "LookupError");
86impl_exception_stub_type!(PyMemoryError, "MemoryError");
87impl_exception_stub_type!(PyModuleNotFoundError, "ModuleNotFoundError");
88impl_exception_stub_type!(PyNameError, "NameError");
89impl_exception_stub_type!(PyNotADirectoryError, "NotADirectoryError");
90impl_exception_stub_type!(PyNotImplementedError, "NotImplementedError");
91impl_exception_stub_type!(PyOSError, "OSError");
92impl_exception_stub_type!(PyOverflowError, "OverflowError");
93impl_exception_stub_type!(PyPendingDeprecationWarning, "PendingDeprecationWarning");
94impl_exception_stub_type!(PyPermissionError, "PermissionError");
95impl_exception_stub_type!(PyProcessLookupError, "ProcessLookupError");
96impl_exception_stub_type!(PyRecursionError, "RecursionError");
97impl_exception_stub_type!(PyReferenceError, "ReferenceError");
98impl_exception_stub_type!(PyResourceWarning, "ResourceWarning");
99impl_exception_stub_type!(PyRuntimeError, "RuntimeError");
100impl_exception_stub_type!(PyRuntimeWarning, "RuntimeWarning");
101impl_exception_stub_type!(PyStopAsyncIteration, "StopAsyncIteration");
102impl_exception_stub_type!(PyStopIteration, "StopIteration");
103impl_exception_stub_type!(PySyntaxError, "SyntaxError");
104impl_exception_stub_type!(PySyntaxWarning, "SyntaxWarning");
105impl_exception_stub_type!(PySystemError, "SystemError");
106impl_exception_stub_type!(PySystemExit, "SystemExit");
107impl_exception_stub_type!(PyTimeoutError, "TimeoutError");
108impl_exception_stub_type!(PyTypeError, "TypeError");
109impl_exception_stub_type!(PyUnboundLocalError, "UnboundLocalError");
110impl_exception_stub_type!(PyUnicodeDecodeError, "UnicodeDecodeError");
111impl_exception_stub_type!(PyUnicodeEncodeError, "UnicodeEncodeError");
112impl_exception_stub_type!(PyUnicodeError, "UnicodeError");
113impl_exception_stub_type!(PyUnicodeTranslateError, "UnicodeTranslateError");
114impl_exception_stub_type!(PyUnicodeWarning, "UnicodeWarning");
115impl_exception_stub_type!(PyUserWarning, "UserWarning");
116impl_exception_stub_type!(PyValueError, "ValueError");
117impl_exception_stub_type!(PyWarning, "Warning");
118impl_exception_stub_type!(PyZeroDivisionError, "ZeroDivisionError");