Skip to main content

pyo3_ffi/
descrobject.rs

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