1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
pub mod gc;
#[doc(hidden)]
pub mod members;
#[allow(clippy::module_inception)]
mod py_class;
#[cfg(feature = "python27-sys")]
#[rustfmt::skip]
mod py_class_impl2;
#[cfg(feature = "python3-sys")]
#[rustfmt::skip]
mod py_class_impl3;
#[doc(hidden)]
pub mod slots;
use std::{cell, mem, ptr};
use crate::err::{self, PyResult};
use crate::ffi;
use crate::objects::{PyModule, PyObject, PyType};
use crate::python::{self, Python, PythonObject};
#[derive(Debug)]
pub enum CompareOp {
Lt = ffi::Py_LT as isize,
Le = ffi::Py_LE as isize,
Eq = ffi::Py_EQ as isize,
Ne = ffi::Py_NE as isize,
Gt = ffi::Py_GT as isize,
Ge = ffi::Py_GE as isize,
}
pub trait PythonObjectFromPyClassMacro: python::PythonObjectWithTypeObject {
fn initialize(py: Python, module_name: Option<&str>) -> PyResult<PyType>;
fn add_to_module(py: Python, module: &PyModule) -> PyResult<()>;
}
#[inline]
#[doc(hidden)]
pub fn data_offset<T>(base_size: usize) -> usize {
let align = mem::align_of::<T>();
(base_size + align - 1) / align * align
}
#[inline]
#[doc(hidden)]
pub fn data_new_size<T>(base_size: usize) -> usize {
data_offset::<T>(base_size) + mem::size_of::<T>()
}
#[inline]
#[doc(hidden)]
pub unsafe fn data_get<'a, T>(_py: Python<'a>, obj: &'a PyObject, offset: usize) -> &'a T {
let ptr = (obj.as_ptr() as *const u8).add(offset) as *const T;
&*ptr
}
#[inline]
#[doc(hidden)]
pub unsafe fn data_init<'a, T>(_py: Python<'a>, obj: &'a PyObject, offset: usize, value: T)
where
T: Send + 'static,
{
let ptr = (obj.as_ptr() as *mut u8).add(offset) as *mut T;
ptr::write(ptr, value)
}
#[inline]
#[doc(hidden)]
pub unsafe fn data_drop<T>(_py: Python<'_>, obj: *mut ffi::PyObject, offset: usize) {
let ptr = (obj as *mut u8).add(offset) as *mut T;
ptr::drop_in_place(ptr)
}
#[inline]
#[doc(hidden)]
pub fn is_ready(_py: Python, ty: &ffi::PyTypeObject) -> bool {
(ty.tp_flags & ffi::Py_TPFLAGS_READY) != 0
}
pub trait BaseObject: PythonObject {
fn size() -> usize;
type InitType;
unsafe fn alloc(py: Python, ty: &PyType, init_val: Self::InitType) -> PyResult<PyObject>;
unsafe fn dealloc(py: Python, obj: *mut ffi::PyObject);
}
impl BaseObject for PyObject {
#[inline]
fn size() -> usize {
mem::size_of::<ffi::PyObject>()
}
type InitType = ();
unsafe fn alloc(py: Python, ty: &PyType, _init_val: ()) -> PyResult<PyObject> {
let ptr = ffi::PyType_GenericAlloc(ty.as_type_ptr(), 0);
err::result_from_owned_ptr(py, ptr)
}
unsafe fn dealloc(_py: Python, obj: *mut ffi::PyObject) {
let ty = ffi::Py_TYPE(obj);
if ffi::PyType_IS_GC(ty) != 0 {
ffi::PyObject_GC_Del(obj as *mut libc::c_void);
} else {
ffi::PyObject_Free(obj as *mut libc::c_void);
}
if ffi::PyType_HasFeature(ty, ffi::Py_TPFLAGS_HEAPTYPE) != 0 {
ffi::Py_DECREF(ty as *mut ffi::PyObject);
}
}
}