python27_sys/
objimpl.rs

1use libc::{c_char, c_int, c_void, size_t};
2
3use crate::object::*;
4use crate::pyport::Py_ssize_t;
5
6#[cfg_attr(windows, link(name = "pythonXY"))]
7extern "C" {
8    #[cfg(not(py_sys_config = "Py_DEBUG"))]
9    pub fn PyObject_Malloc(arg1: size_t) -> *mut c_void;
10    #[cfg(not(py_sys_config = "Py_DEBUG"))]
11    pub fn PyObject_Realloc(arg1: *mut c_void, arg2: size_t) -> *mut c_void;
12    #[cfg(not(py_sys_config = "Py_DEBUG"))]
13    pub fn PyObject_Free(arg1: *mut c_void);
14
15    #[cfg(py_sys_config = "Py_DEBUG")]
16    pub fn _PyObject_DebugMalloc(arg1: size_t) -> *mut c_void;
17    #[cfg(py_sys_config = "Py_DEBUG")]
18    pub fn _PyObject_DebugRealloc(arg1: *mut c_void, arg2: size_t) -> *mut c_void;
19    #[cfg(py_sys_config = "Py_DEBUG")]
20    pub fn _PyObject_DebugFree(arg1: *mut c_void);
21
22    pub fn PyObject_Init(arg1: *mut PyObject, arg2: *mut PyTypeObject) -> *mut PyObject;
23    pub fn PyObject_InitVar(
24        arg1: *mut PyVarObject,
25        arg2: *mut PyTypeObject,
26        arg3: Py_ssize_t,
27    ) -> *mut PyVarObject;
28    pub fn _PyObject_New(arg1: *mut PyTypeObject) -> *mut PyObject;
29    pub fn _PyObject_NewVar(arg1: *mut PyTypeObject, arg2: Py_ssize_t) -> *mut PyVarObject;
30
31    // GC Support
32    pub fn PyGC_Collect() -> Py_ssize_t;
33    pub fn _PyObject_GC_Resize(arg1: *mut PyVarObject, arg2: Py_ssize_t) -> *mut PyVarObject;
34    pub fn _PyObject_GC_Malloc(arg1: size_t) -> *mut PyObject;
35    pub fn _PyObject_GC_New(arg1: *mut PyTypeObject) -> *mut PyObject;
36    pub fn _PyObject_GC_NewVar(arg1: *mut PyTypeObject, arg2: Py_ssize_t) -> *mut PyVarObject;
37    pub fn PyObject_GC_Track(arg1: *mut c_void);
38    pub fn PyObject_GC_UnTrack(arg1: *mut c_void);
39    pub fn PyObject_GC_Del(arg1: *mut c_void);
40}
41
42#[cfg(py_sys_config = "Py_DEBUG")]
43pub use self::_PyObject_DebugFree as PyObject_Free;
44#[cfg(py_sys_config = "Py_DEBUG")]
45pub use self::_PyObject_DebugMalloc as PyObject_Malloc;
46#[cfg(py_sys_config = "Py_DEBUG")]
47pub use self::_PyObject_DebugRealloc as PyObject_Realloc;
48
49/// Test if a type has a GC head
50#[inline(always)]
51pub unsafe fn PyType_IS_GC(t: *mut PyTypeObject) -> c_int {
52    PyType_HasFeature(t, Py_TPFLAGS_HAVE_GC)
53}
54
55/// Test if an object has a GC head
56#[inline(always)]
57pub unsafe fn PyObject_IS_GC(o: *mut PyObject) -> c_int {
58    (PyType_IS_GC(Py_TYPE(o)) != 0
59        && match (*Py_TYPE(o)).tp_is_gc {
60            Some(tp_is_gc) => tp_is_gc(o) != 0,
61            None => true,
62        }) as c_int
63}
64
65/* Test if a type supports weak references */
66#[inline(always)]
67pub unsafe fn PyType_SUPPORTS_WEAKREFS(t: *mut PyTypeObject) -> c_int {
68    (PyType_HasFeature(t, Py_TPFLAGS_HAVE_WEAKREFS) != 0 && ((*t).tp_weaklistoffset > 0)) as c_int
69}
70
71#[inline(always)]
72pub unsafe fn PyObject_GET_WEAKREFS_LISTPTR(o: *mut PyObject) -> *mut *mut PyObject {
73    let weaklistoffset = (*Py_TYPE(o)).tp_weaklistoffset as isize;
74    (o as *mut c_char).offset(weaklistoffset) as *mut *mut PyObject
75}