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