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    pub static mut PyExc_BaseExceptionGroup: *mut PyObject;
120    #[cfg_attr(PyPy, link_name = "PyPyExc_Exception")]
121    pub static mut PyExc_Exception: *mut PyObject;
122    #[cfg_attr(PyPy, link_name = "PyPyExc_StopAsyncIteration")]
123    pub static mut PyExc_StopAsyncIteration: *mut PyObject;
124
125    #[cfg_attr(PyPy, link_name = "PyPyExc_StopIteration")]
126    pub static mut PyExc_StopIteration: *mut PyObject;
127    #[cfg_attr(PyPy, link_name = "PyPyExc_GeneratorExit")]
128    pub static mut PyExc_GeneratorExit: *mut PyObject;
129    #[cfg_attr(PyPy, link_name = "PyPyExc_ArithmeticError")]
130    pub static mut PyExc_ArithmeticError: *mut PyObject;
131    #[cfg_attr(PyPy, link_name = "PyPyExc_LookupError")]
132    pub static mut PyExc_LookupError: *mut PyObject;
133
134    #[cfg_attr(PyPy, link_name = "PyPyExc_AssertionError")]
135    pub static mut PyExc_AssertionError: *mut PyObject;
136    #[cfg_attr(PyPy, link_name = "PyPyExc_AttributeError")]
137    pub static mut PyExc_AttributeError: *mut PyObject;
138    #[cfg_attr(PyPy, link_name = "PyPyExc_BufferError")]
139    pub static mut PyExc_BufferError: *mut PyObject;
140    #[cfg_attr(PyPy, link_name = "PyPyExc_EOFError")]
141    pub static mut PyExc_EOFError: *mut PyObject;
142    #[cfg_attr(PyPy, link_name = "PyPyExc_FloatingPointError")]
143    pub static mut PyExc_FloatingPointError: *mut PyObject;
144    #[cfg_attr(PyPy, link_name = "PyPyExc_OSError")]
145    pub static mut PyExc_OSError: *mut PyObject;
146    #[cfg_attr(PyPy, link_name = "PyPyExc_ImportError")]
147    pub static mut PyExc_ImportError: *mut PyObject;
148    #[cfg_attr(PyPy, link_name = "PyPyExc_ModuleNotFoundError")]
149    pub static mut PyExc_ModuleNotFoundError: *mut PyObject;
150    #[cfg_attr(PyPy, link_name = "PyPyExc_IndexError")]
151    pub static mut PyExc_IndexError: *mut PyObject;
152    #[cfg_attr(PyPy, link_name = "PyPyExc_KeyError")]
153    pub static mut PyExc_KeyError: *mut PyObject;
154    #[cfg_attr(PyPy, link_name = "PyPyExc_KeyboardInterrupt")]
155    pub static mut PyExc_KeyboardInterrupt: *mut PyObject;
156    #[cfg_attr(PyPy, link_name = "PyPyExc_MemoryError")]
157    pub static mut PyExc_MemoryError: *mut PyObject;
158    #[cfg_attr(PyPy, link_name = "PyPyExc_NameError")]
159    pub static mut PyExc_NameError: *mut PyObject;
160    #[cfg_attr(PyPy, link_name = "PyPyExc_OverflowError")]
161    pub static mut PyExc_OverflowError: *mut PyObject;
162    #[cfg_attr(PyPy, link_name = "PyPyExc_RuntimeError")]
163    pub static mut PyExc_RuntimeError: *mut PyObject;
164    #[cfg_attr(PyPy, link_name = "PyPyExc_RecursionError")]
165    pub static mut PyExc_RecursionError: *mut PyObject;
166    #[cfg_attr(PyPy, link_name = "PyPyExc_NotImplementedError")]
167    pub static mut PyExc_NotImplementedError: *mut PyObject;
168    #[cfg_attr(PyPy, link_name = "PyPyExc_SyntaxError")]
169    pub static mut PyExc_SyntaxError: *mut PyObject;
170    #[cfg_attr(PyPy, link_name = "PyPyExc_IndentationError")]
171    pub static mut PyExc_IndentationError: *mut PyObject;
172    #[cfg_attr(PyPy, link_name = "PyPyExc_TabError")]
173    pub static mut PyExc_TabError: *mut PyObject;
174    #[cfg_attr(PyPy, link_name = "PyPyExc_ReferenceError")]
175    pub static mut PyExc_ReferenceError: *mut PyObject;
176    #[cfg_attr(PyPy, link_name = "PyPyExc_SystemError")]
177    pub static mut PyExc_SystemError: *mut PyObject;
178    #[cfg_attr(PyPy, link_name = "PyPyExc_SystemExit")]
179    pub static mut PyExc_SystemExit: *mut PyObject;
180    #[cfg_attr(PyPy, link_name = "PyPyExc_TypeError")]
181    pub static mut PyExc_TypeError: *mut PyObject;
182    #[cfg_attr(PyPy, link_name = "PyPyExc_UnboundLocalError")]
183    pub static mut PyExc_UnboundLocalError: *mut PyObject;
184    #[cfg_attr(PyPy, link_name = "PyPyExc_UnicodeError")]
185    pub static mut PyExc_UnicodeError: *mut PyObject;
186    #[cfg_attr(PyPy, link_name = "PyPyExc_UnicodeEncodeError")]
187    pub static mut PyExc_UnicodeEncodeError: *mut PyObject;
188    #[cfg_attr(PyPy, link_name = "PyPyExc_UnicodeDecodeError")]
189    pub static mut PyExc_UnicodeDecodeError: *mut PyObject;
190    #[cfg_attr(PyPy, link_name = "PyPyExc_UnicodeTranslateError")]
191    pub static mut PyExc_UnicodeTranslateError: *mut PyObject;
192    #[cfg_attr(PyPy, link_name = "PyPyExc_ValueError")]
193    pub static mut PyExc_ValueError: *mut PyObject;
194    #[cfg_attr(PyPy, link_name = "PyPyExc_ZeroDivisionError")]
195    pub static mut PyExc_ZeroDivisionError: *mut PyObject;
196
197    #[cfg_attr(PyPy, link_name = "PyPyExc_BlockingIOError")]
198    pub static mut PyExc_BlockingIOError: *mut PyObject;
199    #[cfg_attr(PyPy, link_name = "PyPyExc_BrokenPipeError")]
200    pub static mut PyExc_BrokenPipeError: *mut PyObject;
201    #[cfg_attr(PyPy, link_name = "PyPyExc_ChildProcessError")]
202    pub static mut PyExc_ChildProcessError: *mut PyObject;
203    #[cfg_attr(PyPy, link_name = "PyPyExc_ConnectionError")]
204    pub static mut PyExc_ConnectionError: *mut PyObject;
205    #[cfg_attr(PyPy, link_name = "PyPyExc_ConnectionAbortedError")]
206    pub static mut PyExc_ConnectionAbortedError: *mut PyObject;
207    #[cfg_attr(PyPy, link_name = "PyPyExc_ConnectionRefusedError")]
208    pub static mut PyExc_ConnectionRefusedError: *mut PyObject;
209    #[cfg_attr(PyPy, link_name = "PyPyExc_ConnectionResetError")]
210    pub static mut PyExc_ConnectionResetError: *mut PyObject;
211    #[cfg_attr(PyPy, link_name = "PyPyExc_FileExistsError")]
212    pub static mut PyExc_FileExistsError: *mut PyObject;
213    #[cfg_attr(PyPy, link_name = "PyPyExc_FileNotFoundError")]
214    pub static mut PyExc_FileNotFoundError: *mut PyObject;
215    #[cfg_attr(PyPy, link_name = "PyPyExc_InterruptedError")]
216    pub static mut PyExc_InterruptedError: *mut PyObject;
217    #[cfg_attr(PyPy, link_name = "PyPyExc_IsADirectoryError")]
218    pub static mut PyExc_IsADirectoryError: *mut PyObject;
219    #[cfg_attr(PyPy, link_name = "PyPyExc_NotADirectoryError")]
220    pub static mut PyExc_NotADirectoryError: *mut PyObject;
221    #[cfg_attr(PyPy, link_name = "PyPyExc_PermissionError")]
222    pub static mut PyExc_PermissionError: *mut PyObject;
223    #[cfg_attr(PyPy, link_name = "PyPyExc_ProcessLookupError")]
224    pub static mut PyExc_ProcessLookupError: *mut PyObject;
225    #[cfg_attr(PyPy, link_name = "PyPyExc_TimeoutError")]
226    pub static mut PyExc_TimeoutError: *mut PyObject;
227
228    #[cfg_attr(PyPy, link_name = "PyPyExc_OSError")]
229    pub static mut PyExc_EnvironmentError: *mut PyObject;
230    #[cfg_attr(PyPy, link_name = "PyPyExc_OSError")]
231    pub static mut PyExc_IOError: *mut PyObject;
232    #[cfg(windows)]
233    #[cfg_attr(PyPy, link_name = "PyPyExc_OSError")]
234    pub static mut PyExc_WindowsError: *mut PyObject;
235
236    pub static mut PyExc_RecursionErrorInst: *mut PyObject;
237
238    /* Predefined warning categories */
239    #[cfg_attr(PyPy, link_name = "PyPyExc_Warning")]
240    pub static mut PyExc_Warning: *mut PyObject;
241    #[cfg_attr(PyPy, link_name = "PyPyExc_UserWarning")]
242    pub static mut PyExc_UserWarning: *mut PyObject;
243    #[cfg_attr(PyPy, link_name = "PyPyExc_DeprecationWarning")]
244    pub static mut PyExc_DeprecationWarning: *mut PyObject;
245    #[cfg_attr(PyPy, link_name = "PyPyExc_PendingDeprecationWarning")]
246    pub static mut PyExc_PendingDeprecationWarning: *mut PyObject;
247    #[cfg_attr(PyPy, link_name = "PyPyExc_SyntaxWarning")]
248    pub static mut PyExc_SyntaxWarning: *mut PyObject;
249    #[cfg_attr(PyPy, link_name = "PyPyExc_RuntimeWarning")]
250    pub static mut PyExc_RuntimeWarning: *mut PyObject;
251    #[cfg_attr(PyPy, link_name = "PyPyExc_FutureWarning")]
252    pub static mut PyExc_FutureWarning: *mut PyObject;
253    #[cfg_attr(PyPy, link_name = "PyPyExc_ImportWarning")]
254    pub static mut PyExc_ImportWarning: *mut PyObject;
255    #[cfg_attr(PyPy, link_name = "PyPyExc_UnicodeWarning")]
256    pub static mut PyExc_UnicodeWarning: *mut PyObject;
257    #[cfg_attr(PyPy, link_name = "PyPyExc_BytesWarning")]
258    pub static mut PyExc_BytesWarning: *mut PyObject;
259    #[cfg_attr(PyPy, link_name = "PyPyExc_ResourceWarning")]
260    pub static mut PyExc_ResourceWarning: *mut PyObject;
261    #[cfg(Py_3_10)]
262    #[cfg_attr(PyPy, link_name = "PyPyExc_EncodingWarning")]
263    pub static mut PyExc_EncodingWarning: *mut PyObject;
264}
265
266extern "C" {
267    #[cfg_attr(PyPy, link_name = "PyPyErr_BadArgument")]
268    pub fn PyErr_BadArgument() -> c_int;
269    #[cfg_attr(PyPy, link_name = "PyPyErr_NoMemory")]
270    pub fn PyErr_NoMemory() -> *mut PyObject;
271    #[cfg_attr(PyPy, link_name = "PyPyErr_SetFromErrno")]
272    pub fn PyErr_SetFromErrno(arg1: *mut PyObject) -> *mut PyObject;
273    #[cfg_attr(PyPy, link_name = "PyPyErr_SetFromErrnoWithFilenameObject")]
274    pub fn PyErr_SetFromErrnoWithFilenameObject(
275        arg1: *mut PyObject,
276        arg2: *mut PyObject,
277    ) -> *mut PyObject;
278    pub fn PyErr_SetFromErrnoWithFilenameObjects(
279        arg1: *mut PyObject,
280        arg2: *mut PyObject,
281        arg3: *mut PyObject,
282    ) -> *mut PyObject;
283    pub fn PyErr_SetFromErrnoWithFilename(
284        exc: *mut PyObject,
285        filename: *const c_char,
286    ) -> *mut PyObject;
287    #[cfg_attr(PyPy, link_name = "PyPyErr_Format")]
288    pub fn PyErr_Format(exception: *mut PyObject, format: *const c_char, ...) -> *mut PyObject;
289    pub fn PyErr_SetImportErrorSubclass(
290        arg1: *mut PyObject,
291        arg2: *mut PyObject,
292        arg3: *mut PyObject,
293        arg4: *mut PyObject,
294    ) -> *mut PyObject;
295    pub fn PyErr_SetImportError(
296        arg1: *mut PyObject,
297        arg2: *mut PyObject,
298        arg3: *mut PyObject,
299    ) -> *mut PyObject;
300    #[cfg_attr(PyPy, link_name = "PyPyErr_BadInternalCall")]
301    pub fn PyErr_BadInternalCall();
302    pub fn _PyErr_BadInternalCall(filename: *const c_char, lineno: c_int);
303    #[cfg_attr(PyPy, link_name = "PyPyErr_NewException")]
304    pub fn PyErr_NewException(
305        name: *const c_char,
306        base: *mut PyObject,
307        dict: *mut PyObject,
308    ) -> *mut PyObject;
309    #[cfg_attr(PyPy, link_name = "PyPyErr_NewExceptionWithDoc")]
310    pub fn PyErr_NewExceptionWithDoc(
311        name: *const c_char,
312        doc: *const c_char,
313        base: *mut PyObject,
314        dict: *mut PyObject,
315    ) -> *mut PyObject;
316    #[cfg_attr(PyPy, link_name = "PyPyErr_WriteUnraisable")]
317    pub fn PyErr_WriteUnraisable(arg1: *mut PyObject);
318    #[cfg_attr(PyPy, link_name = "PyPyErr_CheckSignals")]
319    pub fn PyErr_CheckSignals() -> c_int;
320    #[cfg_attr(PyPy, link_name = "PyPyErr_SetInterrupt")]
321    pub fn PyErr_SetInterrupt();
322    #[cfg(Py_3_10)]
323    #[cfg_attr(PyPy, link_name = "PyPyErr_SetInterruptEx")]
324    pub fn PyErr_SetInterruptEx(signum: c_int);
325    #[cfg_attr(PyPy, link_name = "PyPyErr_SyntaxLocation")]
326    pub fn PyErr_SyntaxLocation(filename: *const c_char, lineno: c_int);
327    #[cfg_attr(PyPy, link_name = "PyPyErr_SyntaxLocationEx")]
328    pub fn PyErr_SyntaxLocationEx(filename: *const c_char, lineno: c_int, col_offset: c_int);
329    #[cfg_attr(PyPy, link_name = "PyPyErr_ProgramText")]
330    pub fn PyErr_ProgramText(filename: *const c_char, lineno: c_int) -> *mut PyObject;
331    #[cfg(not(PyPy))]
332    pub fn PyUnicodeDecodeError_Create(
333        encoding: *const c_char,
334        object: *const c_char,
335        length: Py_ssize_t,
336        start: Py_ssize_t,
337        end: Py_ssize_t,
338        reason: *const c_char,
339    ) -> *mut PyObject;
340    pub fn PyUnicodeEncodeError_GetEncoding(arg1: *mut PyObject) -> *mut PyObject;
341    pub fn PyUnicodeDecodeError_GetEncoding(arg1: *mut PyObject) -> *mut PyObject;
342    pub fn PyUnicodeEncodeError_GetObject(arg1: *mut PyObject) -> *mut PyObject;
343    pub fn PyUnicodeDecodeError_GetObject(arg1: *mut PyObject) -> *mut PyObject;
344    pub fn PyUnicodeTranslateError_GetObject(arg1: *mut PyObject) -> *mut PyObject;
345    pub fn PyUnicodeEncodeError_GetStart(arg1: *mut PyObject, arg2: *mut Py_ssize_t) -> c_int;
346    pub fn PyUnicodeDecodeError_GetStart(arg1: *mut PyObject, arg2: *mut Py_ssize_t) -> c_int;
347    pub fn PyUnicodeTranslateError_GetStart(arg1: *mut PyObject, arg2: *mut Py_ssize_t) -> c_int;
348    pub fn PyUnicodeEncodeError_SetStart(arg1: *mut PyObject, arg2: Py_ssize_t) -> c_int;
349    pub fn PyUnicodeDecodeError_SetStart(arg1: *mut PyObject, arg2: Py_ssize_t) -> c_int;
350    pub fn PyUnicodeTranslateError_SetStart(arg1: *mut PyObject, arg2: Py_ssize_t) -> c_int;
351    pub fn PyUnicodeEncodeError_GetEnd(arg1: *mut PyObject, arg2: *mut Py_ssize_t) -> c_int;
352    pub fn PyUnicodeDecodeError_GetEnd(arg1: *mut PyObject, arg2: *mut Py_ssize_t) -> c_int;
353    pub fn PyUnicodeTranslateError_GetEnd(arg1: *mut PyObject, arg2: *mut Py_ssize_t) -> c_int;
354    pub fn PyUnicodeEncodeError_SetEnd(arg1: *mut PyObject, arg2: Py_ssize_t) -> c_int;
355    pub fn PyUnicodeDecodeError_SetEnd(arg1: *mut PyObject, arg2: Py_ssize_t) -> c_int;
356    pub fn PyUnicodeTranslateError_SetEnd(arg1: *mut PyObject, arg2: Py_ssize_t) -> c_int;
357    pub fn PyUnicodeEncodeError_GetReason(arg1: *mut PyObject) -> *mut PyObject;
358    pub fn PyUnicodeDecodeError_GetReason(arg1: *mut PyObject) -> *mut PyObject;
359    pub fn PyUnicodeTranslateError_GetReason(arg1: *mut PyObject) -> *mut PyObject;
360    pub fn PyUnicodeEncodeError_SetReason(exc: *mut PyObject, reason: *const c_char) -> c_int;
361    pub fn PyUnicodeDecodeError_SetReason(exc: *mut PyObject, reason: *const c_char) -> c_int;
362    pub fn PyUnicodeTranslateError_SetReason(exc: *mut PyObject, reason: *const c_char) -> c_int;
363}