1 | use crate::ffi::{self, Py_ssize_t, PY_SSIZE_T_MAX}; |
2 | |
3 | macro_rules! pyo3_exception { |
4 | ($doc: expr, $name: ident, $base: ty) => { |
5 | #[doc = $doc] |
6 | #[repr(transparent)] |
7 | #[allow(non_camel_case_types)] |
8 | pub struct $name($crate::PyAny); |
9 | |
10 | $crate::impl_exception_boilerplate!($name); |
11 | |
12 | $crate::create_exception_type_object!(pyo3_runtime, $name, $base, Some($doc)); |
13 | }; |
14 | } |
15 | |
16 | /// Convert an usize index into a Py_ssize_t index, clamping overflow to |
17 | /// PY_SSIZE_T_MAX. |
18 | pub(crate) fn get_ssize_index(index: usize) -> Py_ssize_t { |
19 | index.min(PY_SSIZE_T_MAX as usize) as Py_ssize_t |
20 | } |
21 | |
22 | // TODO: use ptr::from_ref on MSRV 1.76 |
23 | #[inline ] |
24 | pub(crate) const fn ptr_from_ref<T>(t: &T) -> *const T { |
25 | t as *const T |
26 | } |
27 | |
28 | // TODO: use ptr::from_mut on MSRV 1.76 |
29 | #[inline ] |
30 | pub(crate) fn ptr_from_mut<T>(t: &mut T) -> *mut T { |
31 | t as *mut T |
32 | } |
33 | |
34 | // TODO: use ptr::fn_addr_eq on MSRV 1.85 |
35 | pub(crate) fn clear_eq(f: Option<ffi::inquiry>, g: ffi::inquiry) -> bool { |
36 | #[cfg (fn_ptr_eq)] |
37 | #[allow (clippy::incompatible_msrv)] |
38 | { |
39 | let Some(f: unsafe fn(*mut PyObject) -> …) = f else { return false }; |
40 | std::ptr::fn_addr_eq(f, g) |
41 | } |
42 | |
43 | #[cfg (not(fn_ptr_eq))] |
44 | { |
45 | f == Some(g) |
46 | } |
47 | } |
48 | |
49 | // TODO: use ptr::fn_addr_eq on MSRV 1.85 |
50 | pub(crate) fn traverse_eq(f: Option<ffi::traverseproc>, g: ffi::traverseproc) -> bool { |
51 | #[cfg (fn_ptr_eq)] |
52 | #[allow (clippy::incompatible_msrv)] |
53 | { |
54 | let Some(f: unsafe fn(*mut PyObject, …) -> …) = f else { return false }; |
55 | std::ptr::fn_addr_eq(f, g) |
56 | } |
57 | |
58 | #[cfg (not(fn_ptr_eq))] |
59 | { |
60 | f == Some(g) |
61 | } |
62 | } |
63 | |