pyforge_ffi/
methodobject.rs1use crate::object::{PyObject, PyTypeObject, Py_TYPE};
2use crate::PyObject_TypeCheck;
3use std::ffi::{c_char, c_int, c_void};
4use std::{mem, ptr};
5
6#[cfg(not(Py_LIMITED_API))]
7pub struct PyCFunctionObject {
8 pub ob_base: PyObject,
9 pub m_ml: *mut PyMethodDef,
10 pub m_self: *mut PyObject,
11 pub m_module: *mut PyObject,
12 pub m_weakreflist: *mut PyObject,
13 pub vectorcall: Option<crate::vectorcallfunc>,
14}
15
16extern_libpython! {
17 pub static mut PyCFunction_Type: PyTypeObject;
18}
19
20#[inline]
21pub unsafe fn PyCFunction_CheckExact(op: *mut PyObject) -> c_int {
22 (Py_TYPE(op) == &raw mut PyCFunction_Type) as c_int
23}
24
25#[inline]
26pub unsafe fn PyCFunction_Check(op: *mut PyObject) -> c_int {
27 PyObject_TypeCheck(op, &raw mut PyCFunction_Type)
28}
29
30pub type PyCFunction =
31 unsafe extern "C" fn(slf: *mut PyObject, args: *mut PyObject) -> *mut PyObject;
32
33#[cfg(any(Py_3_10, not(Py_LIMITED_API)))]
34pub type PyCFunctionFast = unsafe extern "C" fn(
35 slf: *mut PyObject,
36 args: *mut *mut PyObject,
37 nargs: crate::pyport::Py_ssize_t,
38) -> *mut PyObject;
39
40#[cfg(any(Py_3_10, not(Py_LIMITED_API)))]
41#[deprecated(note = "renamed to `PyCFunctionFast`")]
42pub type _PyCFunctionFast = PyCFunctionFast;
43
44pub type PyCFunctionWithKeywords = unsafe extern "C" fn(
45 slf: *mut PyObject,
46 args: *mut PyObject,
47 kwds: *mut PyObject,
48) -> *mut PyObject;
49
50#[cfg(any(Py_3_10, not(Py_LIMITED_API)))]
51pub type PyCFunctionFastWithKeywords = unsafe extern "C" fn(
52 slf: *mut PyObject,
53 args: *const *mut PyObject,
54 nargs: crate::pyport::Py_ssize_t,
55 kwnames: *mut PyObject,
56) -> *mut PyObject;
57
58#[cfg(any(Py_3_10, not(Py_LIMITED_API)))]
59#[deprecated(note = "renamed to `PyCFunctionFastWithKeywords`")]
60pub type _PyCFunctionFastWithKeywords = PyCFunctionFastWithKeywords;
61
62#[cfg(not(Py_LIMITED_API))]
63pub type PyCMethod = unsafe extern "C" fn(
64 slf: *mut PyObject,
65 defining_class: *mut PyTypeObject,
66 args: *const *mut PyObject,
67 nargs: crate::pyport::Py_ssize_t,
68 kwnames: *mut PyObject,
69) -> *mut PyObject;
70
71extern_libpython! {
72 pub fn PyCFunction_GetFunction(f: *mut PyObject) -> Option<PyCFunction>;
73 pub fn PyCFunction_GetSelf(f: *mut PyObject) -> *mut PyObject;
74 pub fn PyCFunction_GetFlags(f: *mut PyObject) -> c_int;
75 #[cfg(not(Py_3_13))]
76 #[deprecated(note = "Python 3.9")]
77 pub fn PyCFunction_Call(
78 f: *mut PyObject,
79 args: *mut PyObject,
80 kwds: *mut PyObject,
81 ) -> *mut PyObject;
82}
83
84#[repr(C)]
90#[derive(Copy, Clone, PartialEq, Eq)]
91pub struct PyMethodDef {
92 pub ml_name: *const c_char,
93 pub ml_meth: PyMethodDefPointer,
94 pub ml_flags: c_int,
95 pub ml_doc: *const c_char,
96}
97
98impl PyMethodDef {
99 pub const fn zeroed() -> PyMethodDef {
100 PyMethodDef {
101 ml_name: ptr::null(),
102 ml_meth: PyMethodDefPointer {
103 Void: ptr::null_mut(),
104 },
105 ml_flags: 0,
106 ml_doc: ptr::null(),
107 }
108 }
109}
110
111impl Default for PyMethodDef {
112 fn default() -> PyMethodDef {
113 PyMethodDef {
114 ml_name: ptr::null(),
115 ml_meth: PyMethodDefPointer {
116 Void: ptr::null_mut(),
117 },
118 ml_flags: 0,
119 ml_doc: ptr::null(),
120 }
121 }
122}
123
124#[repr(C)]
133#[derive(Copy, Clone, Eq)]
134pub union PyMethodDefPointer {
135 pub PyCFunction: PyCFunction,
137
138 pub PyCFunctionWithKeywords: PyCFunctionWithKeywords,
140
141 #[cfg(any(Py_3_10, not(Py_LIMITED_API)))]
143 #[deprecated(note = "renamed to `PyCFunctionFast`")]
144 pub _PyCFunctionFast: PyCFunctionFast,
145
146 #[cfg(any(Py_3_10, not(Py_LIMITED_API)))]
148 pub PyCFunctionFast: PyCFunctionFast,
149
150 #[cfg(any(Py_3_10, not(Py_LIMITED_API)))]
152 #[deprecated(note = "renamed to `PyCFunctionFastWithKeywords`")]
153 pub _PyCFunctionFastWithKeywords: PyCFunctionFastWithKeywords,
154
155 #[cfg(any(Py_3_10, not(Py_LIMITED_API)))]
157 pub PyCFunctionFastWithKeywords: PyCFunctionFastWithKeywords,
158
159 #[cfg(not(Py_LIMITED_API))]
161 pub PyCMethod: PyCMethod,
162
163 Void: *mut c_void,
164}
165
166impl PyMethodDefPointer {
167 pub fn as_ptr(&self) -> *mut c_void {
168 unsafe { self.Void }
169 }
170
171 pub fn is_null(&self) -> bool {
172 self.as_ptr().is_null()
173 }
174
175 pub const fn zeroed() -> PyMethodDefPointer {
176 PyMethodDefPointer {
177 Void: ptr::null_mut(),
178 }
179 }
180}
181
182impl PartialEq for PyMethodDefPointer {
183 fn eq(&self, other: &Self) -> bool {
184 unsafe { self.Void == other.Void }
185 }
186}
187
188impl std::fmt::Pointer for PyMethodDefPointer {
189 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
190 let ptr = unsafe { self.Void };
191 std::fmt::Pointer::fmt(&ptr, f)
192 }
193}
194
195const _: () =
196 assert!(mem::size_of::<PyMethodDefPointer>() == mem::size_of::<Option<extern "C" fn()>>());
197
198#[inline]
199pub unsafe fn PyCFunction_New(ml: *mut PyMethodDef, slf: *mut PyObject) -> *mut PyObject {
200 PyCFunction_NewEx(ml, slf, std::ptr::null_mut())
201}
202
203#[inline]
204pub unsafe fn PyCFunction_NewEx(
205 ml: *mut PyMethodDef,
206 slf: *mut PyObject,
207 module: *mut PyObject,
208) -> *mut PyObject {
209 PyCMethod_New(ml, slf, module, std::ptr::null_mut())
210}
211
212extern_libpython! {
213 pub fn PyCMethod_New(
214 ml: *mut PyMethodDef,
215 slf: *mut PyObject,
216 module: *mut PyObject,
217 cls: *mut PyTypeObject,
218 ) -> *mut PyObject;
219}
220
221pub const METH_VARARGS: c_int = 0x0001;
223pub const METH_KEYWORDS: c_int = 0x0002;
224pub const METH_NOARGS: c_int = 0x0004;
226pub const METH_O: c_int = 0x0008;
227
228pub const METH_CLASS: c_int = 0x0010;
232pub const METH_STATIC: c_int = 0x0020;
233
234pub const METH_COEXIST: c_int = 0x0040;
240
241#[cfg(any(Py_3_10, not(Py_LIMITED_API)))]
244pub const METH_FASTCALL: c_int = 0x0080;
245
246#[cfg(not(Py_LIMITED_API))]
249pub const METH_METHOD: c_int = 0x0200;
250