1use crate::object::{PyObject, PyTypeObject};
2#[cfg(Py_3_9)]
3#[cfg(not(RustPython))]
4use crate::PyObject_TypeCheck;
5#[cfg(not(RustPython))]
6use crate::Py_IS_TYPE;
7use core::ffi::{c_char, c_int, c_void};
8use core::{mem, ptr};
9
10#[cfg(all(Py_3_9, not(Py_LIMITED_API), not(GraalPy)))]
11pub struct PyCFunctionObject {
12 pub ob_base: PyObject,
13 pub m_ml: *mut PyMethodDef,
14 pub m_self: *mut PyObject,
15 pub m_module: *mut PyObject,
16 pub m_weakreflist: *mut PyObject,
17 #[cfg(not(PyPy))]
18 pub vectorcall: Option<crate::vectorcallfunc>,
19}
20
21extern_libpython! {
22 #[cfg(not(RustPython))]
23 #[cfg_attr(PyPy, link_name = "PyPyCFunction_Type")]
24 pub static mut PyCFunction_Type: PyTypeObject;
25
26 #[cfg(RustPython)]
27 pub fn PyCFunction_CheckExact(op: *mut PyObject) -> c_int;
28 #[cfg(RustPython)]
29 pub fn PyCFunction_Check(op: *mut PyObject) -> c_int;
30}
31
32#[cfg(all(Py_3_9, not(RustPython)))]
33#[inline]
34pub unsafe fn PyCFunction_CheckExact(op: *mut PyObject) -> c_int {
35 Py_IS_TYPE(op, &raw mut PyCFunction_Type)
36}
37
38#[cfg(all(Py_3_9, not(RustPython)))]
39#[inline]
40pub unsafe fn PyCFunction_Check(op: *mut PyObject) -> c_int {
41 PyObject_TypeCheck(op, &raw mut PyCFunction_Type)
42}
43
44#[cfg(not(any(Py_3_9, RustPython)))]
45#[inline]
46pub unsafe fn PyCFunction_Check(op: *mut PyObject) -> c_int {
47 Py_IS_TYPE(op, &raw mut PyCFunction_Type)
48}
49
50pub type PyCFunction =
51 unsafe extern "C" fn(slf: *mut PyObject, args: *mut PyObject) -> *mut PyObject;
52
53#[cfg(any(Py_3_10, not(Py_LIMITED_API)))]
54pub type PyCFunctionFast = unsafe extern "C" fn(
55 slf: *mut PyObject,
56 args: *mut *mut PyObject,
57 nargs: crate::pyport::Py_ssize_t,
58) -> *mut PyObject;
59
60#[cfg(any(Py_3_10, not(Py_LIMITED_API)))]
61#[deprecated(note = "renamed to `PyCFunctionFast`")]
62pub type _PyCFunctionFast = PyCFunctionFast;
63
64pub type PyCFunctionWithKeywords = unsafe extern "C" fn(
65 slf: *mut PyObject,
66 args: *mut PyObject,
67 kwds: *mut PyObject,
68) -> *mut PyObject;
69
70#[cfg(any(Py_3_10, not(Py_LIMITED_API)))]
71pub type PyCFunctionFastWithKeywords = unsafe extern "C" fn(
72 slf: *mut PyObject,
73 args: *const *mut PyObject,
74 nargs: crate::pyport::Py_ssize_t,
75 kwnames: *mut PyObject,
76) -> *mut PyObject;
77
78#[cfg(any(Py_3_10, not(Py_LIMITED_API)))]
79#[deprecated(note = "renamed to `PyCFunctionFastWithKeywords`")]
80pub type _PyCFunctionFastWithKeywords = PyCFunctionFastWithKeywords;
81
82#[cfg(all(Py_3_9, not(Py_LIMITED_API)))]
83pub type PyCMethod = unsafe extern "C" fn(
84 slf: *mut PyObject,
85 defining_class: *mut PyTypeObject,
86 args: *const *mut PyObject,
87 nargs: crate::pyport::Py_ssize_t,
88 kwnames: *mut PyObject,
89) -> *mut PyObject;
90
91extern_libpython! {
92 #[cfg_attr(PyPy, link_name = "PyPyCFunction_GetFunction")]
93 pub fn PyCFunction_GetFunction(f: *mut PyObject) -> Option<PyCFunction>;
94 pub fn PyCFunction_GetSelf(f: *mut PyObject) -> *mut PyObject;
95 pub fn PyCFunction_GetFlags(f: *mut PyObject) -> c_int;
96 #[cfg(not(Py_3_13))]
97 #[cfg_attr(Py_3_9, deprecated(note = "Python 3.9"))]
98 pub fn PyCFunction_Call(
99 f: *mut PyObject,
100 args: *mut PyObject,
101 kwds: *mut PyObject,
102 ) -> *mut PyObject;
103}
104
105#[repr(C)]
111#[derive(Copy, Clone, PartialEq, Eq)]
112pub struct PyMethodDef {
113 pub ml_name: *const c_char,
114 pub ml_meth: PyMethodDefPointer,
115 pub ml_flags: c_int,
116 pub ml_doc: *const c_char,
117}
118
119impl PyMethodDef {
120 pub const fn zeroed() -> PyMethodDef {
121 PyMethodDef {
122 ml_name: ptr::null(),
123 ml_meth: PyMethodDefPointer {
124 Void: ptr::null_mut(),
125 },
126 ml_flags: 0,
127 ml_doc: ptr::null(),
128 }
129 }
130}
131
132impl Default for PyMethodDef {
133 fn default() -> PyMethodDef {
134 PyMethodDef {
135 ml_name: ptr::null(),
136 ml_meth: PyMethodDefPointer {
137 Void: ptr::null_mut(),
138 },
139 ml_flags: 0,
140 ml_doc: ptr::null(),
141 }
142 }
143}
144
145#[repr(C)]
154#[derive(Copy, Clone, Eq)]
155pub union PyMethodDefPointer {
156 pub PyCFunction: PyCFunction,
158
159 pub PyCFunctionWithKeywords: PyCFunctionWithKeywords,
161
162 #[cfg(any(Py_3_10, not(Py_LIMITED_API)))]
164 #[deprecated(note = "renamed to `PyCFunctionFast`")]
165 pub _PyCFunctionFast: PyCFunctionFast,
166
167 #[cfg(any(Py_3_10, not(Py_LIMITED_API)))]
169 pub PyCFunctionFast: PyCFunctionFast,
170
171 #[cfg(any(Py_3_10, not(Py_LIMITED_API)))]
173 #[deprecated(note = "renamed to `PyCFunctionFastWithKeywords`")]
174 pub _PyCFunctionFastWithKeywords: PyCFunctionFastWithKeywords,
175
176 #[cfg(any(Py_3_10, not(Py_LIMITED_API)))]
178 pub PyCFunctionFastWithKeywords: PyCFunctionFastWithKeywords,
179
180 #[cfg(all(Py_3_9, not(Py_LIMITED_API)))]
182 pub PyCMethod: PyCMethod,
183
184 Void: *mut c_void,
185}
186
187impl PyMethodDefPointer {
188 pub fn as_ptr(&self) -> *mut c_void {
189 unsafe { self.Void }
190 }
191
192 pub fn is_null(&self) -> bool {
193 self.as_ptr().is_null()
194 }
195
196 pub const fn zeroed() -> PyMethodDefPointer {
197 PyMethodDefPointer {
198 Void: ptr::null_mut(),
199 }
200 }
201}
202
203impl PartialEq for PyMethodDefPointer {
204 fn eq(&self, other: &Self) -> bool {
205 unsafe { self.Void == other.Void }
206 }
207}
208
209impl core::fmt::Pointer for PyMethodDefPointer {
210 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
211 let ptr = unsafe { self.Void };
212 core::fmt::Pointer::fmt(&ptr, f)
213 }
214}
215
216const _: () =
217 assert!(mem::size_of::<PyMethodDefPointer>() == mem::size_of::<Option<extern "C" fn()>>());
218
219#[cfg(not(Py_3_9))]
220extern_libpython! {
221 #[cfg_attr(PyPy, link_name = "PyPyCFunction_New")]
222 pub fn PyCFunction_New(ml: *mut PyMethodDef, slf: *mut PyObject) -> *mut PyObject;
223
224 #[cfg_attr(PyPy, link_name = "PyPyCFunction_NewEx")]
225 pub fn PyCFunction_NewEx(
226 ml: *mut PyMethodDef,
227 slf: *mut PyObject,
228 module: *mut PyObject,
229 ) -> *mut PyObject;
230}
231
232#[cfg(Py_3_9)]
233#[inline]
234pub unsafe fn PyCFunction_New(ml: *mut PyMethodDef, slf: *mut PyObject) -> *mut PyObject {
235 PyCFunction_NewEx(ml, slf, core::ptr::null_mut())
236}
237
238#[cfg(Py_3_9)]
239#[inline]
240pub unsafe fn PyCFunction_NewEx(
241 ml: *mut PyMethodDef,
242 slf: *mut PyObject,
243 module: *mut PyObject,
244) -> *mut PyObject {
245 PyCMethod_New(ml, slf, module, core::ptr::null_mut())
246}
247
248#[cfg(Py_3_9)]
249extern_libpython! {
250 #[cfg_attr(PyPy, link_name = "PyPyCMethod_New")]
251 pub fn PyCMethod_New(
252 ml: *mut PyMethodDef,
253 slf: *mut PyObject,
254 module: *mut PyObject,
255 cls: *mut PyTypeObject,
256 ) -> *mut PyObject;
257}
258
259pub const METH_VARARGS: c_int = 0x0001;
261pub const METH_KEYWORDS: c_int = 0x0002;
262pub const METH_NOARGS: c_int = 0x0004;
264pub const METH_O: c_int = 0x0008;
265
266pub const METH_CLASS: c_int = 0x0010;
270pub const METH_STATIC: c_int = 0x0020;
271
272pub const METH_COEXIST: c_int = 0x0040;
278
279#[cfg(any(Py_3_10, not(Py_LIMITED_API)))]
282pub const METH_FASTCALL: c_int = 0x0080;
283
284#[cfg(all(Py_3_9, not(Py_LIMITED_API)))]
287pub const METH_METHOD: c_int = 0x0200;
288
289extern_libpython! {
290 #[cfg(not(Py_3_9))]
291 pub fn PyCFunction_ClearFreeList() -> c_int;
292}