Skip to content

Commit

Permalink
collect arrays of objects prior to filling tuple in fixed-size conver…
Browse files Browse the repository at this point in the history
…sions
  • Loading branch information
davidhewitt committed Jul 16, 2023
1 parent ab6c27e commit 18d0b1b
Showing 1 changed file with 33 additions and 15 deletions.
48 changes: 33 additions & 15 deletions src/types/tuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,22 +284,34 @@ fn wrong_tuple_length(t: &PyTuple, expected_length: usize) -> PyErr {
macro_rules! tuple_conversion ({$length:expr,$(($refN:ident, $n:tt, $T:ident)),+} => {
impl <$($T: ToPyObject),+> ToPyObject for ($($T,)+) {
fn to_object(&self, py: Python<'_>) -> PyObject {
unsafe {
let ptr = ffi::PyTuple_New($length);
let ret = PyObject::from_owned_ptr(py, ptr);
$(ffi::PyTuple_SetItem(ptr, $n, self.$n.to_object(py).into_ptr());)+
ret
fn inner(py: Python<'_>, [$($refN),+]: [PyObject; $length]) -> PyObject {
unsafe {
let ptr = ffi::PyTuple_New($length);
let tup = Py::from_owned_ptr(py, ptr);
$(
#[cfg(not(any(Py_LIMITED_API, PyPy)))] ffi::PyTuple_SET_ITEM(ptr, $n, $refN.into_ptr());
#[cfg(any(Py_LIMITED_API, PyPy))] ffi::PyTuple_SetItem(ptr, $n, $refN.into_ptr());
)+
tup
}
}
inner(py, [$(self.$n.to_object(py)),+])
}
}
impl <$($T: IntoPy<PyObject>),+> IntoPy<PyObject> for ($($T,)+) {
fn into_py(self, py: Python<'_>) -> PyObject {
unsafe {
let ptr = ffi::PyTuple_New($length);
let ret = PyObject::from_owned_ptr(py, ptr);
$(ffi::PyTuple_SetItem(ptr, $n, self.$n.into_py(py).into_ptr());)+
ret
fn inner(py: Python<'_>, [$($refN),+]: [PyObject; $length]) -> PyObject {
unsafe {
let ptr = ffi::PyTuple_New($length);
let tup = Py::from_owned_ptr(py, ptr);
$(
#[cfg(not(any(Py_LIMITED_API, PyPy)))] ffi::PyTuple_SET_ITEM(ptr, $n, $refN.into_ptr());
#[cfg(any(Py_LIMITED_API, PyPy))] ffi::PyTuple_SetItem(ptr, $n, $refN.into_ptr());
)+
tup
}
}
inner(py, [$(self.$n.into_py(py)),+])
}

#[cfg(feature = "experimental-inspect")]
Expand All @@ -310,12 +322,18 @@ fn type_output() -> TypeInfo {

impl <$($T: IntoPy<PyObject>),+> IntoPy<Py<PyTuple>> for ($($T,)+) {
fn into_py(self, py: Python<'_>) -> Py<PyTuple> {
unsafe {
let ptr = ffi::PyTuple_New($length);
let ret = Py::from_owned_ptr(py, ptr);
$(ffi::PyTuple_SetItem(ptr, $n, self.$n.into_py(py).into_ptr());)+
ret
fn inner(py: Python<'_>, [$($refN),+]: [PyObject; $length]) -> Py<PyTuple> {
unsafe {
let ptr = ffi::PyTuple_New($length);
let tup = Py::from_owned_ptr(py, ptr);
$(
#[cfg(not(any(Py_LIMITED_API, PyPy)))] ffi::PyTuple_SET_ITEM(ptr, $n, $refN.into_ptr());
#[cfg(any(Py_LIMITED_API, PyPy))] ffi::PyTuple_SetItem(ptr, $n, $refN.into_ptr());
)+
tup
}
}
inner(py, [$(self.$n.into_py(py)),+])
}

#[cfg(feature = "experimental-inspect")]
Expand Down

0 comments on commit 18d0b1b

Please sign in to comment.