Skip to content

Commit

Permalink
ffi: cleanup tupleobject
Browse files Browse the repository at this point in the history
* Remove function `PyTuple_ClearFreeList` from python 3.9 above
* Move non-limited api into ffi/cpython/
  • Loading branch information
deantvv committed Sep 25, 2021
1 parent 59678d3 commit fb19cea
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 33 deletions.
2 changes: 1 addition & 1 deletion src/ffi/cpython/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ pub unsafe fn PyCode_Check(op: *mut PyObject) -> c_int {
#[inline]
#[cfg(not(PyPy))]
pub unsafe fn PyCode_GetNumFree(op: *mut PyCodeObject) -> Py_ssize_t {
crate::ffi::tupleobject::PyTuple_GET_SIZE((*op).co_freevars)
crate::ffi::PyTuple_GET_SIZE((*op).co_freevars)
}

extern "C" {
Expand Down
4 changes: 4 additions & 0 deletions src/ffi/cpython/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub(crate) mod compile;
#[cfg(not(PyPy))]
pub(crate) mod dictobject;
// skipped fileobject.h
// skipped fileutils.h
pub(crate) mod frameobject;
pub(crate) mod import;
#[cfg(all(Py_3_8, not(PyPy)))]
Expand All @@ -21,6 +22,8 @@ pub(crate) mod pydebug;
pub(crate) mod pylifecycle;
pub(crate) mod pystate;
pub(crate) mod pythonrun;
// skipped sysmodule.h
pub(crate) mod tupleobject;
pub(crate) mod unicodeobject;

pub use self::abstract_::*;
Expand All @@ -43,4 +46,5 @@ pub use self::pydebug::*;
pub use self::pylifecycle::*;
pub use self::pystate::*;
pub use self::pythonrun::*;
pub use self::tupleobject::*;
pub use self::unicodeobject::*;
43 changes: 43 additions & 0 deletions src/ffi/cpython/tupleobject.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use crate::ffi::object::*;
#[cfg(not(PyPy))]
use crate::ffi::pyport::Py_ssize_t;

#[repr(C)]
pub struct PyTupleObject {
pub ob_base: PyVarObject,
pub ob_item: [*mut PyObject; 1],
}

// skipped _PyTuple_Resize
// skipped _PyTuple_MaybeUntrack

/// Macro, trading safety for speed

// skipped _PyTuple_CAST

#[inline]
#[cfg(not(PyPy))]
pub unsafe fn PyTuple_GET_SIZE(op: *mut PyObject) -> Py_ssize_t {
Py_SIZE(op)
}

#[inline]
#[cfg(not(PyPy))]
pub unsafe fn PyTuple_GET_ITEM(op: *mut PyObject, i: Py_ssize_t) -> *mut PyObject {
*(*(op as *mut PyTupleObject))
.ob_item
.as_ptr()
.offset(i as isize)
}

/// Macro, *only* to be used to fill in brand new tuples
#[inline]
#[cfg(not(PyPy))]
pub unsafe fn PyTuple_SET_ITEM(op: *mut PyObject, i: Py_ssize_t, v: *mut PyObject) {
*(*(op as *mut PyTupleObject))
.ob_item
.as_mut_ptr()
.offset(i as isize) = v;
}

// skipped _PyTuple_DebugMallocStats
33 changes: 1 addition & 32 deletions src/ffi/tupleobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@ use crate::ffi::object::*;
use crate::ffi::pyport::Py_ssize_t;
use std::os::raw::c_int;

#[repr(C)]
pub struct PyTupleObject {
pub ob_base: PyVarObject,
pub ob_item: [*mut PyObject; 1],
}

#[cfg_attr(windows, link(name = "pythonXY"))]
extern "C" {
#[cfg_attr(PyPy, link_name = "PyPyTuple_Type")]
Expand Down Expand Up @@ -42,31 +36,6 @@ extern "C" {
) -> *mut PyObject;
#[cfg_attr(PyPy, link_name = "PyPyTuple_Pack")]
pub fn PyTuple_Pack(arg1: Py_ssize_t, ...) -> *mut PyObject;
#[cfg(not(Py_3_9))]
pub fn PyTuple_ClearFreeList() -> c_int;
}

/// Macro, trading safety for speed
#[inline]
#[cfg(not(any(Py_LIMITED_API, PyPy)))]
pub unsafe fn PyTuple_GET_ITEM(op: *mut PyObject, i: Py_ssize_t) -> *mut PyObject {
*(*(op as *mut PyTupleObject))
.ob_item
.as_ptr()
.offset(i as isize)
}

#[inline]
#[cfg(not(any(Py_LIMITED_API, PyPy)))]
pub unsafe fn PyTuple_GET_SIZE(op: *mut PyObject) -> Py_ssize_t {
Py_SIZE(op)
}

/// Macro, *only* to be used to fill in brand new tuples
#[inline]
#[cfg(not(any(Py_LIMITED_API, PyPy)))]
pub unsafe fn PyTuple_SET_ITEM(op: *mut PyObject, i: Py_ssize_t, v: *mut PyObject) {
*(*(op as *mut PyTupleObject))
.ob_item
.as_mut_ptr()
.offset(i as isize) = v;
}

0 comments on commit fb19cea

Please sign in to comment.