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
// Copyright (c) 2016 Daniel Grunwald
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

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};

// TODO: consider moving CompareOp to a different module, so that it isn't exported via two paths
#[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,
}

/// Trait implemented by the types produced by the `py_class!()` macro.
///
/// This is an unstable implementation detail; do not implement manually!
pub trait PythonObjectFromPyClassMacro: python::PythonObjectWithTypeObject {
    /// Initializes the class.
    ///
    /// module_name: the name of the parent module into which the class will be placed.
    fn initialize(py: Python, module_name: Option<&str>) -> PyResult<PyType>;

    /// Initializes the class and adds it to the module.
    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>();
    // round base_size up to next multiple of align
    (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
}

/// A PythonObject that is usable as a base type with the `py_class!()` macro.
pub trait BaseObject: PythonObject {
    /// Gets the size of the object, in bytes.
    fn size() -> usize;

    type InitType;

    /// Allocates a new object (usually by calling ty->tp_alloc),
    /// and initializes it using init_val.
    /// `ty` must be derived from the Self type, and the resulting object
    /// must be of type `ty`.
    unsafe fn alloc(py: Python, ty: &PyType, init_val: Self::InitType) -> PyResult<PyObject>;

    /// Calls the rust destructor for the object and frees the memory
    /// (usually by calling ptr->ob_type->tp_free).
    /// This function is used as tp_dealloc implementation.
    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);
        //println!("BaseObject::alloc({:?}) = {:?}", ty.as_type_ptr(), ptr);
        err::result_from_owned_ptr(py, ptr)
    }

    unsafe fn dealloc(_py: Python, obj: *mut ffi::PyObject) {
        //println!("BaseObject::dealloc({:?})", ptr);
        // Unfortunately, there is no PyType_GenericFree, so
        // we have to manually un-do the work of PyType_GenericAlloc:
        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);
        }
        // For heap types, PyType_GenericAlloc calls INCREF on the type objects,
        // so we need to call DECREF here:
        if ffi::PyType_HasFeature(ty, ffi::Py_TPFLAGS_HEAPTYPE) != 0 {
            ffi::Py_DECREF(ty as *mut ffi::PyObject);
        }
    }
}