python27_sys/
methodobject.rs

1use core::ptr;
2use libc::{c_char, c_int};
3
4use crate::object::{PyObject, PyTypeObject, Py_TYPE};
5
6#[cfg_attr(windows, link(name = "pythonXY"))]
7extern "C" {
8    pub static mut PyCFunction_Type: PyTypeObject;
9}
10
11#[inline(always)]
12pub unsafe fn PyCFunction_Check(op: *mut PyObject) -> c_int {
13    let u: *mut PyTypeObject = &mut PyCFunction_Type;
14    (Py_TYPE(op) == u) as c_int
15}
16
17pub type PyCFunction =
18    unsafe extern "C" fn(slf: *mut PyObject, args: *mut PyObject) -> *mut PyObject;
19pub type PyCFunctionWithKeywords = unsafe extern "C" fn(
20    slf: *mut PyObject,
21    args: *mut PyObject,
22    kwds: *mut PyObject,
23) -> *mut PyObject;
24pub type PyNoArgsFunction = unsafe extern "C" fn(slf: *mut PyObject) -> *mut PyObject;
25
26#[cfg_attr(windows, link(name = "pythonXY"))]
27extern "C" {
28    pub fn PyCFunction_GetFunction(f: *mut PyObject) -> Option<PyCFunction>;
29    pub fn PyCFunction_GetSelf(f: *mut PyObject) -> *mut PyObject;
30    pub fn PyCFunction_GetFlags(f: *mut PyObject) -> c_int;
31    pub fn PyCFunction_Call(
32        f: *mut PyObject,
33        args: *mut PyObject,
34        kwds: *mut PyObject,
35    ) -> *mut PyObject;
36}
37
38#[repr(C)]
39#[derive(Copy)]
40pub struct PyMethodDef {
41    pub ml_name: *const c_char,
42    pub ml_meth: Option<PyCFunction>,
43    pub ml_flags: c_int,
44    pub ml_doc: *const c_char,
45}
46
47impl Clone for PyMethodDef {
48    #[inline]
49    fn clone(&self) -> PyMethodDef {
50        *self
51    }
52}
53
54/* Flag passed to newmethodobject */
55pub const METH_OLDARGS: c_int = 0x0000;
56pub const METH_VARARGS: c_int = 0x0001;
57pub const METH_KEYWORDS: c_int = 0x0002;
58/* METH_NOARGS and METH_O must not be combined with the flags above. */
59pub const METH_NOARGS: c_int = 0x0004;
60pub const METH_O: c_int = 0x0008;
61
62/* METH_CLASS and METH_STATIC are a little different; these control
63the construction of methods for a class.  These cannot be used for
64functions in modules. */
65pub const METH_CLASS: c_int = 0x0010;
66pub const METH_STATIC: c_int = 0x0020;
67
68/* METH_COEXIST allows a method to be entered eventhough a slot has
69already filled the entry.  When defined, the flag allows a separate
70method, "__contains__" for example, to coexist with a defined
71slot like sq_contains. */
72
73pub const METH_COEXIST: c_int = 0x0040;
74
75#[repr(C)]
76#[derive(Copy, Clone)]
77pub struct PyMethodChain {
78    pub methods: *mut PyMethodDef,
79    pub link: *mut PyMethodChain,
80}
81
82/*
83#[repr(C)]
84#[derive(Copy)]
85struct PyCFunctionObject {
86    #[cfg(py_sys_config="Py_TRACE_REFS")]
87    pub _ob_next: *mut PyObject,
88    #[cfg(py_sys_config="Py_TRACE_REFS")]
89    pub _ob_prev: *mut PyObject,
90    pub ob_refcnt: Py_ssize_t,
91    pub ob_type: *mut PyTypeObject,
92    pub m_ml: *mut PyMethodDef,
93    pub m_self: *mut PyObject,
94    pub m_module: *mut PyObject,
95}
96*/
97
98#[cfg_attr(windows, link(name = "pythonXY"))]
99extern "C" {
100    pub fn Py_FindMethod(
101        methods: *mut PyMethodDef,
102        slf: *mut PyObject,
103        name: *const c_char,
104    ) -> *mut PyObject;
105    pub fn PyCFunction_NewEx(
106        ml: *mut PyMethodDef,
107        slf: *mut PyObject,
108        module: *mut PyObject,
109    ) -> *mut PyObject;
110    pub fn Py_FindMethodInChain(
111        chain: *mut PyMethodChain,
112        slf: *mut PyObject,
113        name: *const c_char,
114    ) -> *mut PyObject;
115    pub fn PyCFunction_ClearFreeList() -> c_int;
116}
117
118#[inline(always)]
119pub unsafe fn PyCFunction_New(ml: *mut PyMethodDef, slf: *mut PyObject) -> *mut PyObject {
120    PyCFunction_NewEx(ml, slf, ptr::null_mut())
121}