pyo3_ffi/
pyerrors.rs

1use crate::object::*;
2use crate::pyport::Py_ssize_t;
3use std::os::raw::{c_char, c_int};
4
5extern "C" {
6    #[cfg_attr(PyPy, link_name = "PyPyErr_SetNone")]
7    pub fn PyErr_SetNone(arg1: *mut PyObject);
8    #[cfg_attr(PyPy, link_name = "PyPyErr_SetObject")]
9    pub fn PyErr_SetObject(arg1: *mut PyObject, arg2: *mut PyObject);
10    #[cfg_attr(PyPy, link_name = "PyPyErr_SetString")]
11    pub fn PyErr_SetString(exception: *mut PyObject, string: *const c_char);
12    #[cfg_attr(PyPy, link_name = "PyPyErr_Occurred")]
13    pub fn PyErr_Occurred() -> *mut PyObject;
14    #[cfg_attr(PyPy, link_name = "PyPyErr_Clear")]
15    pub fn PyErr_Clear();
16    #[cfg_attr(Py_3_12, deprecated(note = "Use PyErr_GetRaisedException() instead."))]
17    #[cfg_attr(PyPy, link_name = "PyPyErr_Fetch")]
18    pub fn PyErr_Fetch(
19        arg1: *mut *mut PyObject,
20        arg2: *mut *mut PyObject,
21        arg3: *mut *mut PyObject,
22    );
23    #[cfg_attr(Py_3_12, deprecated(note = "Use PyErr_SetRaisedException() instead."))]
24    #[cfg_attr(PyPy, link_name = "PyPyErr_Restore")]
25    pub fn PyErr_Restore(arg1: *mut PyObject, arg2: *mut PyObject, arg3: *mut PyObject);
26    #[cfg_attr(PyPy, link_name = "PyPyErr_GetExcInfo")]
27    pub fn PyErr_GetExcInfo(
28        arg1: *mut *mut PyObject,
29        arg2: *mut *mut PyObject,
30        arg3: *mut *mut PyObject,
31    );
32    #[cfg_attr(PyPy, link_name = "PyPyErr_SetExcInfo")]
33    pub fn PyErr_SetExcInfo(arg1: *mut PyObject, arg2: *mut PyObject, arg3: *mut PyObject);
34    #[cfg_attr(PyPy, link_name = "PyPy_FatalError")]
35    pub fn Py_FatalError(message: *const c_char) -> !;
36    #[cfg_attr(PyPy, link_name = "PyPyErr_GivenExceptionMatches")]
37    pub fn PyErr_GivenExceptionMatches(arg1: *mut PyObject, arg2: *mut PyObject) -> c_int;
38    #[cfg_attr(PyPy, link_name = "PyPyErr_ExceptionMatches")]
39    pub fn PyErr_ExceptionMatches(arg1: *mut PyObject) -> c_int;
40    #[cfg_attr(
41        Py_3_12,
42        deprecated(
43            note = "Use PyErr_GetRaisedException() instead, to avoid any possible de-normalization."
44        )
45    )]
46    #[cfg_attr(PyPy, link_name = "PyPyErr_NormalizeException")]
47    pub fn PyErr_NormalizeException(
48        arg1: *mut *mut PyObject,
49        arg2: *mut *mut PyObject,
50        arg3: *mut *mut PyObject,
51    );
52    #[cfg(Py_3_12)]
53    pub fn PyErr_GetRaisedException() -> *mut PyObject;
54    #[cfg(Py_3_12)]
55    pub fn PyErr_SetRaisedException(exc: *mut PyObject);
56    #[cfg_attr(PyPy, link_name = "PyPyException_SetTraceback")]
57    pub fn PyException_SetTraceback(arg1: *mut PyObject, arg2: *mut PyObject) -> c_int;
58    #[cfg_attr(PyPy, link_name = "PyPyException_GetTraceback")]
59    pub fn PyException_GetTraceback(arg1: *mut PyObject) -> *mut PyObject;
60    #[cfg_attr(PyPy, link_name = "PyPyException_GetCause")]
61    pub fn PyException_GetCause(arg1: *mut PyObject) -> *mut PyObject;
62    #[cfg_attr(PyPy, link_name = "PyPyException_SetCause")]
63    pub fn PyException_SetCause(arg1: *mut PyObject, arg2: *mut PyObject);
64    #[cfg_attr(PyPy, link_name = "PyPyException_GetContext")]
65    pub fn PyException_GetContext(arg1: *mut PyObject) -> *mut PyObject;
66    #[cfg_attr(PyPy, link_name = "PyPyException_SetContext")]
67    pub fn PyException_SetContext(arg1: *mut PyObject, arg2: *mut PyObject);
68
69    #[cfg(PyPy)]
70    #[link_name = "PyPyExceptionInstance_Class"]
71    pub fn PyExceptionInstance_Class(x: *mut PyObject) -> *mut PyObject;
72}
73
74#[inline]
75pub unsafe fn PyExceptionClass_Check(x: *mut PyObject) -> c_int {
76    (PyType_Check(x) != 0
77        && PyType_FastSubclass(x as *mut PyTypeObject, Py_TPFLAGS_BASE_EXC_SUBCLASS) != 0)
78        as c_int
79}
80
81#[inline]
82pub unsafe fn PyExceptionInstance_Check(x: *mut PyObject) -> c_int {
83    PyType_FastSubclass(Py_TYPE(x), Py_TPFLAGS_BASE_EXC_SUBCLASS)
84}
85
86#[inline]
87#[cfg(not(PyPy))]
88pub unsafe fn PyExceptionInstance_Class(x: *mut PyObject) -> *mut PyObject {
89    Py_TYPE(x) as *mut PyObject
90}
91
92// ported from cpython exception.c (line 2096)
93#[cfg(PyPy)]
94pub unsafe fn PyUnicodeDecodeError_Create(
95    encoding: *const c_char,
96    object: *const c_char,
97    length: Py_ssize_t,
98    start: Py_ssize_t,
99    end: Py_ssize_t,
100    reason: *const c_char,
101) -> *mut PyObject {
102    crate::_PyObject_CallFunction_SizeT(
103        PyExc_UnicodeDecodeError,
104        c_str!("sy#nns").as_ptr(),
105        encoding,
106        object,
107        length,
108        start,
109        end,
110        reason,
111    )
112}
113
114#[cfg_attr(windows, link(name = "pythonXY"))]
115extern "C" {
116    #[cfg_attr(PyPy, link_name = "PyPyExc_BaseException")]
117    pub static mut PyExc_BaseException: *mut PyObject;
118    #[cfg(Py_3_11)]
119    #[cfg_attr(PyPy, link_name = "PyPyExc_BaseExceptionGroup")]
120    pub static mut PyExc_BaseExceptionGroup: *mut PyObject;
121    #[cfg_attr(PyPy, link_name = "PyPyExc_Exception")]
122    pub static mut PyExc_Exception: *mut PyObject;
123    #[cfg_attr(PyPy, link_name = "PyPyExc_StopAsyncIteration")]
124    pub static mut PyExc_StopAsyncIteration: *mut PyObject;
125
126    #[cfg_attr(PyPy, link_name = "PyPyExc_StopIteration")]
127    pub static mut PyExc_StopIteration: *mut PyObject;
128    #[cfg_attr(PyPy, link_name = "PyPyExc_GeneratorExit")]
129    pub static mut PyExc_GeneratorExit: *mut PyObject;
130    #[cfg_attr(PyPy, link_name = "PyPyExc_ArithmeticError")]
131    pub static mut PyExc_ArithmeticError: *mut PyObject;
132    #[cfg_attr(PyPy, link_name = "PyPyExc_LookupError")]
133    pub static mut PyExc_LookupError: *mut PyObject;
134
135    #[cfg_attr(PyPy, link_name = "PyPyExc_AssertionError")]
136    pub static mut PyExc_AssertionError: *mut PyObject;
137    #[cfg_attr(PyPy, link_name = "PyPyExc_AttributeError")]
138    pub static mut PyExc_AttributeError: *mut PyObject;
139    #[cfg_attr(PyPy, link_name = "PyPyExc_BufferError")]
140    pub static mut PyExc_BufferError: *mut PyObject;
141    #[cfg_attr(PyPy, link_name = "PyPyExc_EOFError")]
142    pub static mut PyExc_EOFError: *mut PyObject;
143    #[cfg_attr(PyPy, link_name = "PyPyExc_FloatingPointError")]
144    pub static mut PyExc_FloatingPointError: *mut PyObject;
145    #[cfg_attr(PyPy, link_name = "PyPyExc_OSError")]
146    pub static mut PyExc_OSError: *mut PyObject;
147    #[cfg_attr(PyPy, link_name = "PyPyExc_ImportError")]
148    pub static mut PyExc_ImportError: *mut PyObject;
149    #[cfg_attr(PyPy, link_name = "PyPyExc_ModuleNotFoundError")]
150    pub static mut PyExc_ModuleNotFoundError: *mut PyObject;
151    #[cfg_attr(PyPy, link_name = "PyPyExc_IndexError")]
152    pub static mut PyExc_IndexError: *mut PyObject;
153    #[cfg_attr(PyPy, link_name = "PyPyExc_KeyError")]
154    pub static mut PyExc_KeyError: *mut PyObject;
155    #[cfg_attr(PyPy, link_name = "PyPyExc_KeyboardInterrupt")]
156    pub static mut PyExc_KeyboardInterrupt: *mut PyObject;
157    #[cfg_attr(PyPy, link_name = "PyPyExc_MemoryError")]
158    pub static mut PyExc_MemoryError: *mut PyObject;
159    #[cfg_attr(PyPy, link_name = "PyPyExc_NameError")]
160    pub static mut PyExc_NameError: *mut PyObject;
161    #[cfg_attr(PyPy, link_name = "PyPyExc_OverflowError")]
162    pub static mut PyExc_OverflowError: *mut PyObject;
163    #[cfg_attr(PyPy, link_name = "PyPyExc_RuntimeError")]
164    pub static mut PyExc_RuntimeError: *mut PyObject;
165    #[cfg_attr(PyPy, link_name = "PyPyExc_RecursionError")]
166    pub static mut PyExc_RecursionError: *mut PyObject;
167    #[cfg_attr(PyPy, link_name = "PyPyExc_NotImplementedError")]
168    pub static mut PyExc_NotImplementedError: *mut PyObject;
169    #[cfg_attr(PyPy, link_name = "PyPyExc_SyntaxError")]
170    pub static mut PyExc_SyntaxError: *mut PyObject;
171    #[cfg_attr(PyPy, link_name = "PyPyExc_IndentationError")]
172    pub static mut PyExc_IndentationError: *mut PyObject;
173    #[cfg_attr(PyPy, link_name = "PyPyExc_TabError")]
174    pub static mut PyExc_TabError: *mut PyObject;
175    #[cfg_attr(PyPy, link_name = "PyPyExc_ReferenceError")]
176    pub static mut PyExc_ReferenceError: *mut PyObject;
177    #[cfg_attr(PyPy, link_name = "PyPyExc_SystemError")]
178    pub static mut PyExc_SystemError: *mut PyObject;
179    #[cfg_attr(PyPy, link_name = "PyPyExc_SystemExit")]
180    pub static mut PyExc_SystemExit: *mut PyObject;
181    #[cfg_attr(PyPy, link_name = "PyPyExc_TypeError")]
182    pub static mut PyExc_TypeError: *mut PyObject;
183    #[cfg_attr(PyPy, link_name = "PyPyExc_UnboundLocalError")]
184    pub static mut PyExc_UnboundLocalError: *mut PyObject;
185    #[cfg_attr(PyPy, link_name = "PyPyExc_UnicodeError")]
186    pub static mut PyExc_UnicodeError: *mut PyObject;
187    #[cfg_attr(PyPy, link_name = "PyPyExc_UnicodeEncodeError")]
188    pub static mut PyExc_UnicodeEncodeError: *mut PyObject;
189    #[cfg_attr(PyPy, link_name = "PyPyExc_UnicodeDecodeError")]
190    pub static mut PyExc_UnicodeDecodeError: *mut PyObject;
191    #[cfg_attr(PyPy, link_name = "PyPyExc_UnicodeTranslateError")]
192    pub static mut PyExc_UnicodeTranslateError: *mut PyObject;
193    #[cfg_attr(PyPy, link_name = "PyPyExc_ValueError")]
194    pub static mut PyExc_ValueError: *mut PyObject;
195    #[cfg_attr(PyPy, link_name = "PyPyExc_ZeroDivisionError")]
196    pub static mut PyExc_ZeroDivisionError: *mut PyObject;
197
198    #[cfg_attr(PyPy, link_name = "PyPyExc_BlockingIOError")]
199    pub static mut PyExc_BlockingIOError: *mut PyObject;
200    #[cfg_attr(PyPy, link_name = "PyPyExc_BrokenPipeError")]
201    pub static mut PyExc_BrokenPipeError: *mut PyObject;
202    #[cfg_attr(PyPy, link_name = "PyPyExc_ChildProcessError")]
203    pub static mut PyExc_ChildProcessError: *mut PyObject;
204    #[cfg_attr(PyPy, link_name = "PyPyExc_ConnectionError")]
205    pub static mut PyExc_ConnectionError: *mut PyObject;
206    #[cfg_attr(PyPy, link_name = "PyPyExc_ConnectionAbortedError")]
207    pub static mut PyExc_ConnectionAbortedError: *mut PyObject;
208    #[cfg_attr(PyPy, link_name = "PyPyExc_ConnectionRefusedError")]
209    pub static mut PyExc_ConnectionRefusedError: *mut PyObject;
210    #[cfg_attr(PyPy, link_name = "PyPyExc_ConnectionResetError")]
211    pub static mut PyExc_ConnectionResetError: *mut PyObject;
212    #[cfg_attr(PyPy, link_name = "PyPyExc_FileExistsError")]
213    pub static mut PyExc_FileExistsError: *mut PyObject;
214    #[cfg_attr(PyPy, link_name = "PyPyExc_FileNotFoundError")]
215    pub static mut PyExc_FileNotFoundError: *mut PyObject;
216    #[cfg_attr(PyPy, link_name = "PyPyExc_InterruptedError")]
217    pub static mut PyExc_InterruptedError: *mut PyObject;
218    #[cfg_attr(PyPy, link_name = "PyPyExc_IsADirectoryError")]
219    pub static mut PyExc_IsADirectoryError: *mut PyObject;
220    #[cfg_attr(PyPy, link_name = "PyPyExc_NotADirectoryError")]
221    pub static mut PyExc_NotADirectoryError: *mut PyObject;
222    #[cfg_attr(PyPy, link_name = "PyPyExc_PermissionError")]
223    pub static mut PyExc_PermissionError: *mut PyObject;
224    #[cfg_attr(PyPy, link_name = "PyPyExc_ProcessLookupError")]
225    pub static mut PyExc_ProcessLookupError: *mut PyObject;
226    #[cfg_attr(PyPy, link_name = "PyPyExc_TimeoutError")]
227    pub static mut PyExc_TimeoutError: *mut PyObject;
228
229    #[cfg_attr(PyPy, link_name = "PyPyExc_OSError")]
230    pub static mut PyExc_EnvironmentError: *mut PyObject;
231    #[cfg_attr(PyPy, link_name = "PyPyExc_OSError")]
232    pub static mut PyExc_IOError: *mut PyObject;
233    #[cfg(windows)]
234    #[cfg_attr(PyPy, link_name = "PyPyExc_OSError")]
235    pub static mut PyExc_WindowsError: *mut PyObject;
236
237    pub static mut PyExc_RecursionErrorInst: *mut PyObject;
238
239    /* Predefined warning categories */
240    #[cfg_attr(PyPy, link_name = "PyPyExc_Warning")]
241    pub static mut PyExc_Warning: *mut PyObject;
242    #[cfg_attr(PyPy, link_name = "PyPyExc_UserWarning")]
243    pub static mut PyExc_UserWarning: *mut PyObject;
244    #[cfg_attr(PyPy, link_name = "PyPyExc_DeprecationWarning")]
245    pub static mut PyExc_DeprecationWarning: *mut PyObject;
246    #[cfg_attr(PyPy, link_name = "PyPyExc_PendingDeprecationWarning")]
247    pub static mut PyExc_PendingDeprecationWarning: *mut PyObject;
248    #[cfg_attr(PyPy, link_name = "PyPyExc_SyntaxWarning")]
249    pub static mut PyExc_SyntaxWarning: *mut PyObject;
250    #[cfg_attr(PyPy, link_name = "PyPyExc_RuntimeWarning")]
251    pub static mut PyExc_RuntimeWarning: *mut PyObject;
252    #[cfg_attr(PyPy, link_name = "PyPyExc_FutureWarning")]
253    pub static mut PyExc_FutureWarning: *mut PyObject;
254    #[cfg_attr(PyPy, link_name = "PyPyExc_ImportWarning")]
255    pub static mut PyExc_ImportWarning: *mut PyObject;
256    #[cfg_attr(PyPy, link_name = "PyPyExc_UnicodeWarning")]
257    pub static mut PyExc_UnicodeWarning: *mut PyObject;
258    #[cfg_attr(PyPy, link_name = "PyPyExc_BytesWarning")]
259    pub static mut PyExc_BytesWarning: *mut PyObject;
260    #[cfg_attr(PyPy, link_name = "PyPyExc_ResourceWarning")]
261    pub static mut PyExc_ResourceWarning: *mut PyObject;
262    #[cfg(Py_3_10)]
263    #[cfg_attr(PyPy, link_name = "PyPyExc_EncodingWarning")]
264    pub static mut PyExc_EncodingWarning: *mut PyObject;
265}
266
267extern "C" {
268    #[cfg_attr(PyPy, link_name = "PyPyErr_BadArgument")]
269    pub fn PyErr_BadArgument() -> c_int;
270    #[cfg_attr(PyPy, link_name = "PyPyErr_NoMemory")]
271    pub fn PyErr_NoMemory() -> *mut PyObject;
272    #[cfg_attr(PyPy, link_name = "PyPyErr_SetFromErrno")]
273    pub fn PyErr_SetFromErrno(arg1: *mut PyObject) -> *mut PyObject;
274    #[cfg_attr(PyPy, link_name = "PyPyErr_SetFromErrnoWithFilenameObject")]
275    pub fn PyErr_SetFromErrnoWithFilenameObject(
276        arg1: *mut PyObject,
277        arg2: *mut PyObject,
278    ) -> *mut PyObject;
279    pub fn PyErr_SetFromErrnoWithFilenameObjects(
280        arg1: *mut PyObject,
281        arg2: *mut PyObject,
282        arg3: *mut PyObject,
283    ) -> *mut PyObject;
284    pub fn PyErr_SetFromErrnoWithFilename(
285        exc: *mut PyObject,
286        filename: *const c_char,
287    ) -> *mut PyObject;
288    #[cfg_attr(PyPy, link_name = "PyPyErr_Format")]
289    pub fn PyErr_Format(exception: *mut PyObject, format: *const c_char, ...) -> *mut PyObject;
290    pub fn PyErr_SetImportErrorSubclass(
291        arg1: *mut PyObject,
292        arg2: *mut PyObject,
293        arg3: *mut PyObject,
294        arg4: *mut PyObject,
295    ) -> *mut PyObject;
296    pub fn PyErr_SetImportError(
297        arg1: *mut PyObject,
298        arg2: *mut PyObject,
299        arg3: *mut PyObject,
300    ) -> *mut PyObject;
301    #[cfg_attr(PyPy, link_name = "PyPyErr_BadInternalCall")]
302    pub fn PyErr_BadInternalCall();
303    pub fn _PyErr_BadInternalCall(filename: *const c_char, lineno: c_int);
304    #[cfg_attr(PyPy, link_name = "PyPyErr_NewException")]
305    pub fn PyErr_NewException(
306        name: *const c_char,
307        base: *mut PyObject,
308        dict: *mut PyObject,
309    ) -> *mut PyObject;
310    #[cfg_attr(PyPy, link_name = "PyPyErr_NewExceptionWithDoc")]
311    pub fn PyErr_NewExceptionWithDoc(
312        name: *const c_char,
313        doc: *const c_char,
314        base: *mut PyObject,
315        dict: *mut PyObject,
316    ) -> *mut PyObject;
317    #[cfg_attr(PyPy, link_name = "PyPyErr_WriteUnraisable")]
318    pub fn PyErr_WriteUnraisable(arg1: *mut PyObject);
319    #[cfg_attr(PyPy, link_name = "PyPyErr_CheckSignals")]
320    pub fn PyErr_CheckSignals() -> c_int;
321    #[cfg_attr(PyPy, link_name = "PyPyErr_SetInterrupt")]
322    pub fn PyErr_SetInterrupt();
323    #[cfg(Py_3_10)]
324    #[cfg_attr(PyPy, link_name = "PyPyErr_SetInterruptEx")]
325    pub fn PyErr_SetInterruptEx(signum: c_int);
326    #[cfg_attr(PyPy, link_name = "PyPyErr_SyntaxLocation")]
327    pub fn PyErr_SyntaxLocation(filename: *const c_char, lineno: c_int);
328    #[cfg_attr(PyPy, link_name = "PyPyErr_SyntaxLocationEx")]
329    pub fn PyErr_SyntaxLocationEx(filename: *const c_char, lineno: c_int, col_offset: c_int);
330    #[cfg_attr(PyPy, link_name = "PyPyErr_ProgramText")]
331    pub fn PyErr_ProgramText(filename: *const c_char, lineno: c_int) -> *mut PyObject;
332    #[cfg(not(PyPy))]
333    pub fn PyUnicodeDecodeError_Create(
334        encoding: *const c_char,
335        object: *const c_char,
336        length: Py_ssize_t,
337        start: Py_ssize_t,
338        end: Py_ssize_t,
339        reason: *const c_char,
340    ) -> *mut PyObject;
341    pub fn PyUnicodeEncodeError_GetEncoding(arg1: *mut PyObject) -> *mut PyObject;
342    pub fn PyUnicodeDecodeError_GetEncoding(arg1: *mut PyObject) -> *mut PyObject;
343    pub fn PyUnicodeEncodeError_GetObject(arg1: *mut PyObject) -> *mut PyObject;
344    pub fn PyUnicodeDecodeError_GetObject(arg1: *mut PyObject) -> *mut PyObject;
345    pub fn PyUnicodeTranslateError_GetObject(arg1: *mut PyObject) -> *mut PyObject;
346    pub fn PyUnicodeEncodeError_GetStart(arg1: *mut PyObject, arg2: *mut Py_ssize_t) -> c_int;
347    pub fn PyUnicodeDecodeError_GetStart(arg1: *mut PyObject, arg2: *mut Py_ssize_t) -> c_int;
348    pub fn PyUnicodeTranslateError_GetStart(arg1: *mut PyObject, arg2: *mut Py_ssize_t) -> c_int;
349    pub fn PyUnicodeEncodeError_SetStart(arg1: *mut PyObject, arg2: Py_ssize_t) -> c_int;
350    pub fn PyUnicodeDecodeError_SetStart(arg1: *mut PyObject, arg2: Py_ssize_t) -> c_int;
351    pub fn PyUnicodeTranslateError_SetStart(arg1: *mut PyObject, arg2: Py_ssize_t) -> c_int;
352    pub fn PyUnicodeEncodeError_GetEnd(arg1: *mut PyObject, arg2: *mut Py_ssize_t) -> c_int;
353    pub fn PyUnicodeDecodeError_GetEnd(arg1: *mut PyObject, arg2: *mut Py_ssize_t) -> c_int;
354    pub fn PyUnicodeTranslateError_GetEnd(arg1: *mut PyObject, arg2: *mut Py_ssize_t) -> c_int;
355    pub fn PyUnicodeEncodeError_SetEnd(arg1: *mut PyObject, arg2: Py_ssize_t) -> c_int;
356    pub fn PyUnicodeDecodeError_SetEnd(arg1: *mut PyObject, arg2: Py_ssize_t) -> c_int;
357    pub fn PyUnicodeTranslateError_SetEnd(arg1: *mut PyObject, arg2: Py_ssize_t) -> c_int;
358    pub fn PyUnicodeEncodeError_GetReason(arg1: *mut PyObject) -> *mut PyObject;
359    pub fn PyUnicodeDecodeError_GetReason(arg1: *mut PyObject) -> *mut PyObject;
360    pub fn PyUnicodeTranslateError_GetReason(arg1: *mut PyObject) -> *mut PyObject;
361    pub fn PyUnicodeEncodeError_SetReason(exc: *mut PyObject, reason: *const c_char) -> c_int;
362    pub fn PyUnicodeDecodeError_SetReason(exc: *mut PyObject, reason: *const c_char) -> c_int;
363    pub fn PyUnicodeTranslateError_SetReason(exc: *mut PyObject, reason: *const c_char) -> c_int;
364}