pyo3_ffi/
descrobject.rs

1use crate::methodobject::PyMethodDef;
2use crate::object::{PyObject, PyTypeObject};
3use crate::Py_ssize_t;
4use std::os::raw::{c_char, c_int, c_void};
5use std::ptr;
6
7pub type getter = unsafe extern "C" fn(slf: *mut PyObject, closure: *mut c_void) -> *mut PyObject;
8pub type setter =
9    unsafe extern "C" fn(slf: *mut PyObject, value: *mut PyObject, closure: *mut c_void) -> c_int;
10
11/// Represents the [PyGetSetDef](https://docs.python.org/3/c-api/structures.html#c.PyGetSetDef)
12/// structure.
13///
14/// Note that CPython may leave fields uninitialized. You must ensure that
15/// `name` != NULL before dereferencing or reading other fields.
16#[repr(C)]
17#[derive(Copy, Clone, Debug, Eq, PartialEq)]
18pub struct PyGetSetDef {
19    pub name: *const c_char,
20    pub get: Option<getter>,
21    pub set: Option<setter>,
22    pub doc: *const c_char,
23    pub closure: *mut c_void,
24}
25
26impl Default for PyGetSetDef {
27    fn default() -> PyGetSetDef {
28        PyGetSetDef {
29            name: ptr::null(),
30            get: None,
31            set: None,
32            doc: ptr::null(),
33            closure: ptr::null_mut(),
34        }
35    }
36}
37
38#[cfg_attr(windows, link(name = "pythonXY"))]
39extern "C" {
40    #[cfg_attr(PyPy, link_name = "PyPyClassMethodDescr_Type")]
41    pub static mut PyClassMethodDescr_Type: PyTypeObject;
42    #[cfg_attr(PyPy, link_name = "PyPyGetSetDescr_Type")]
43    pub static mut PyGetSetDescr_Type: PyTypeObject;
44    #[cfg_attr(PyPy, link_name = "PyPyMemberDescr_Type")]
45    pub static mut PyMemberDescr_Type: PyTypeObject;
46    #[cfg_attr(PyPy, link_name = "PyPyMethodDescr_Type")]
47    pub static mut PyMethodDescr_Type: PyTypeObject;
48    #[cfg_attr(PyPy, link_name = "PyPyWrapperDescr_Type")]
49    pub static mut PyWrapperDescr_Type: PyTypeObject;
50    #[cfg_attr(PyPy, link_name = "PyPyDictProxy_Type")]
51    pub static mut PyDictProxy_Type: PyTypeObject;
52    #[cfg_attr(PyPy, link_name = "PyPyProperty_Type")]
53    pub static mut PyProperty_Type: PyTypeObject;
54}
55
56extern "C" {
57    pub fn PyDescr_NewMethod(arg1: *mut PyTypeObject, arg2: *mut PyMethodDef) -> *mut PyObject;
58    #[cfg_attr(PyPy, link_name = "PyPyDescr_NewClassMethod")]
59    pub fn PyDescr_NewClassMethod(arg1: *mut PyTypeObject, arg2: *mut PyMethodDef)
60        -> *mut PyObject;
61    #[cfg_attr(PyPy, link_name = "PyPyDescr_NewMember")]
62    pub fn PyDescr_NewMember(arg1: *mut PyTypeObject, arg2: *mut PyMemberDef) -> *mut PyObject;
63    #[cfg_attr(PyPy, link_name = "PyPyDescr_NewGetSet")]
64    pub fn PyDescr_NewGetSet(arg1: *mut PyTypeObject, arg2: *mut PyGetSetDef) -> *mut PyObject;
65
66    #[cfg_attr(PyPy, link_name = "PyPyDictProxy_New")]
67    pub fn PyDictProxy_New(arg1: *mut PyObject) -> *mut PyObject;
68    pub fn PyWrapper_New(arg1: *mut PyObject, arg2: *mut PyObject) -> *mut PyObject;
69}
70
71/// Represents the [PyMemberDef](https://docs.python.org/3/c-api/structures.html#c.PyMemberDef)
72/// structure.
73///
74/// Note that CPython may leave fields uninitialized. You must always ensure that
75/// `name` != NULL before dereferencing or reading other fields.
76#[repr(C)]
77#[derive(Copy, Clone, Eq, PartialEq)]
78pub struct PyMemberDef {
79    pub name: *const c_char,
80    pub type_code: c_int,
81    pub offset: Py_ssize_t,
82    pub flags: c_int,
83    pub doc: *const c_char,
84}
85
86impl Default for PyMemberDef {
87    fn default() -> PyMemberDef {
88        PyMemberDef {
89            name: ptr::null_mut(),
90            type_code: 0,
91            offset: 0,
92            flags: 0,
93            doc: ptr::null_mut(),
94        }
95    }
96}
97
98/* Types */
99pub const Py_T_SHORT: c_int = 0;
100pub const Py_T_INT: c_int = 1;
101pub const Py_T_LONG: c_int = 2;
102pub const Py_T_FLOAT: c_int = 3;
103pub const Py_T_DOUBLE: c_int = 4;
104pub const Py_T_STRING: c_int = 5;
105#[deprecated(note = "Use Py_T_OBJECT_EX instead")]
106pub const _Py_T_OBJECT: c_int = 6;
107pub const Py_T_CHAR: c_int = 7;
108pub const Py_T_BYTE: c_int = 8;
109pub const Py_T_UBYTE: c_int = 9;
110pub const Py_T_USHORT: c_int = 10;
111pub const Py_T_UINT: c_int = 11;
112pub const Py_T_ULONG: c_int = 12;
113pub const Py_T_STRING_INPLACE: c_int = 13;
114pub const Py_T_BOOL: c_int = 14;
115pub const Py_T_OBJECT_EX: c_int = 16;
116pub const Py_T_LONGLONG: c_int = 17;
117pub const Py_T_ULONGLONG: c_int = 18;
118pub const Py_T_PYSSIZET: c_int = 19;
119#[deprecated(note = "Value is always none")]
120pub const _Py_T_NONE: c_int = 20;
121
122/* Flags */
123pub const Py_READONLY: c_int = 1;
124#[cfg(Py_3_10)]
125pub const Py_AUDIT_READ: c_int = 2; // Added in 3.10, harmless no-op before that
126#[deprecated]
127pub const _Py_WRITE_RESTRICTED: c_int = 4; // Deprecated, no-op. Do not reuse the value.
128pub const Py_RELATIVE_OFFSET: c_int = 8;
129
130extern "C" {
131    pub fn PyMember_GetOne(addr: *const c_char, l: *mut PyMemberDef) -> *mut PyObject;
132    pub fn PyMember_SetOne(addr: *mut c_char, l: *mut PyMemberDef, value: *mut PyObject) -> c_int;
133}