Skip to main content

pyforge_ffi/
descrobject.rs

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