Skip to content

implement PyTupleMethods #3681

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions guide/src/conversions/traits.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ struct RustyTuple(String, String);
# use pyo3::types::PyTuple;
# fn main() -> PyResult<()> {
# Python::with_gil(|py| -> PyResult<()> {
# let tuple = PyTuple::new(py, vec!["test", "test2"]);
# let tuple = PyTuple::new_bound(py, vec!["test", "test2"]);
#
# let rustytuple: RustyTuple = tuple.extract()?;
# assert_eq!(rustytuple.0, "test");
Expand All @@ -204,7 +204,7 @@ struct RustyTuple((String,));
# use pyo3::types::PyTuple;
# fn main() -> PyResult<()> {
# Python::with_gil(|py| -> PyResult<()> {
# let tuple = PyTuple::new(py, vec!["test"]);
# let tuple = PyTuple::new_bound(py, vec!["test"]);
#
# let rustytuple: RustyTuple = tuple.extract()?;
# assert_eq!((rustytuple.0).0, "test");
Expand Down Expand Up @@ -482,7 +482,7 @@ If the input is neither a string nor an integer, the error message will be:
- retrieve the field from a mapping, possibly with the custom key specified as an argument.
- can be any literal that implements `ToBorrowedObject`
- `pyo3(from_py_with = "...")`
- apply a custom function to convert the field from Python the desired Rust type.
- apply a custom function to convert the field from Python the desired Rust type.
- the argument must be the name of the function as a string.
- the function signature must be `fn(&PyAny) -> PyResult<T>` where `T` is the Rust type of the argument.

Expand Down
2 changes: 1 addition & 1 deletion guide/src/python_from_rust.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ fn main() -> PyResult<()> {
fun.call0(py)?;

// call object with PyTuple
let args = PyTuple::new(py, &[arg1, arg2, arg3]);
let args = PyTuple::new_bound(py, &[arg1, arg2, arg3]);
fun.call1(py, args)?;

// pass arguments as rust tuple
Expand Down
24 changes: 12 additions & 12 deletions pytests/src/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ fn make_date(py: Python<'_>, year: i32, month: u8, day: u8) -> PyResult<&PyDate>
}

#[pyfunction]
fn get_date_tuple<'p>(py: Python<'p>, d: &PyDate) -> &'p PyTuple {
PyTuple::new(py, [d.get_year(), d.get_month() as i32, d.get_day() as i32])
fn get_date_tuple<'p>(py: Python<'p>, d: &PyDate) -> Bound<'p, PyTuple> {
PyTuple::new_bound(py, [d.get_year(), d.get_month() as i32, d.get_day() as i32])
}

#[pyfunction]
Expand Down Expand Up @@ -48,8 +48,8 @@ fn time_with_fold<'p>(
}

#[pyfunction]
fn get_time_tuple<'p>(py: Python<'p>, dt: &PyTime) -> &'p PyTuple {
PyTuple::new(
fn get_time_tuple<'p>(py: Python<'p>, dt: &PyTime) -> Bound<'p, PyTuple> {
PyTuple::new_bound(
py,
[
dt.get_hour() as u32,
Expand All @@ -61,8 +61,8 @@ fn get_time_tuple<'p>(py: Python<'p>, dt: &PyTime) -> &'p PyTuple {
}

#[pyfunction]
fn get_time_tuple_fold<'p>(py: Python<'p>, dt: &PyTime) -> &'p PyTuple {
PyTuple::new(
fn get_time_tuple_fold<'p>(py: Python<'p>, dt: &PyTime) -> Bound<'p, PyTuple> {
PyTuple::new_bound(
py,
[
dt.get_hour() as u32,
Expand All @@ -80,8 +80,8 @@ fn make_delta(py: Python<'_>, days: i32, seconds: i32, microseconds: i32) -> PyR
}

#[pyfunction]
fn get_delta_tuple<'p>(py: Python<'p>, delta: &PyDelta) -> &'p PyTuple {
PyTuple::new(
fn get_delta_tuple<'p>(py: Python<'p>, delta: &PyDelta) -> Bound<'p, PyTuple> {
PyTuple::new_bound(
py,
[
delta.get_days(),
Expand Down Expand Up @@ -118,8 +118,8 @@ fn make_datetime<'p>(
}

#[pyfunction]
fn get_datetime_tuple<'p>(py: Python<'p>, dt: &PyDateTime) -> &'p PyTuple {
PyTuple::new(
fn get_datetime_tuple<'p>(py: Python<'p>, dt: &PyDateTime) -> Bound<'p, PyTuple> {
PyTuple::new_bound(
py,
[
dt.get_year(),
Expand All @@ -134,8 +134,8 @@ fn get_datetime_tuple<'p>(py: Python<'p>, dt: &PyDateTime) -> &'p PyTuple {
}

#[pyfunction]
fn get_datetime_tuple_fold<'p>(py: Python<'p>, dt: &PyDateTime) -> &'p PyTuple {
PyTuple::new(
fn get_datetime_tuple_fold<'p>(py: Python<'p>, dt: &PyDateTime) -> Bound<'p, PyTuple> {
PyTuple::new_bound(
py,
[
dt.get_year(),
Expand Down
2 changes: 1 addition & 1 deletion src/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ mod implementations {
/// Converts `()` to an empty Python tuple.
impl IntoPy<Py<PyTuple>> for () {
fn into_py(self, py: Python<'_>) -> Py<PyTuple> {
PyTuple::empty(py).into()
PyTuple::empty_bound(py).unbind()
}
}

Expand Down
14 changes: 7 additions & 7 deletions src/impl_/extract_argument.rs
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,7 @@ impl<'py> VarargsHandler<'py> for TupleVarargs {
varargs: &[Option<&PyAny>],
_function_description: &FunctionDescription,
) -> PyResult<Self::Varargs> {
Ok(PyTuple::new(py, varargs))
Ok(PyTuple::new_bound(py, varargs).into_gil_ref())
}

#[inline]
Expand Down Expand Up @@ -697,7 +697,7 @@ fn push_parameter_list(msg: &mut String, parameter_names: &[&str]) {
mod tests {
use crate::{
types::{IntoPyDict, PyTuple},
PyAny, Python, ToPyObject,
PyAny, Python,
};

use super::{push_parameter_list, FunctionDescription, NoVarargs, NoVarkeywords};
Expand All @@ -714,8 +714,8 @@ mod tests {
};

Python::with_gil(|py| {
let args = PyTuple::new(py, Vec::<&PyAny>::new());
let kwargs = [("foo".to_object(py).into_ref(py), 0u8)].into_py_dict(py);
let args = PyTuple::new_bound(py, Vec::<&PyAny>::new());
let kwargs = [("foo", 0u8)].into_py_dict(py);
let err = unsafe {
function_description
.extract_arguments_tuple_dict::<NoVarargs, NoVarkeywords>(
Expand Down Expand Up @@ -745,8 +745,8 @@ mod tests {
};

Python::with_gil(|py| {
let args = PyTuple::new(py, Vec::<&PyAny>::new());
let kwargs = [(1u8.to_object(py).into_ref(py), 1u8)].into_py_dict(py);
let args = PyTuple::new_bound(py, Vec::<&PyAny>::new());
let kwargs = [(1u8, 1u8)].into_py_dict(py);
let err = unsafe {
function_description
.extract_arguments_tuple_dict::<NoVarargs, NoVarkeywords>(
Expand Down Expand Up @@ -776,7 +776,7 @@ mod tests {
};

Python::with_gil(|py| {
let args = PyTuple::new(py, Vec::<&PyAny>::new());
let args = PyTuple::new_bound(py, Vec::<&PyAny>::new());
let mut output = [None, None];
let err = unsafe {
function_description.extract_arguments_tuple_dict::<NoVarargs, NoVarkeywords>(
Expand Down
2 changes: 1 addition & 1 deletion src/impl_/pymodule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl ModuleDef {
.import("sys")?
.getattr("implementation")?
.getattr("version")?;
if version.lt(crate::types::PyTuple::new(py, PYPY_GOOD_VERSION))? {
if version.lt(crate::types::PyTuple::new_bound(py, PYPY_GOOD_VERSION))? {
let warn = py.import("warnings")?.getattr("warn")?;
warn.call1((
"PyPy 3.7 versions older than 7.3.8 are known to have binary \
Expand Down
8 changes: 4 additions & 4 deletions src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,10 +325,10 @@ impl<'py, T> Borrowed<'py, 'py, T>
where
T: HasPyGilRef,
{
// pub(crate) fn into_gil_ref(self) -> &'py T::AsRefTarget {
// // Safety: self is a borrow over `'py`.
// unsafe { self.py().from_borrowed_ptr(self.0.as_ptr()) }
// }
pub(crate) fn into_gil_ref(self) -> &'py T::AsRefTarget {
// Safety: self is a borrow over `'py`.
unsafe { self.py().from_borrowed_ptr(self.0.as_ptr()) }
}
}

impl<T> std::fmt::Debug for Borrowed<'_, '_, T> {
Expand Down
1 change: 1 addition & 0 deletions src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,4 @@ pub use crate::types::module::PyModuleMethods;
pub use crate::types::sequence::PySequenceMethods;
pub use crate::types::set::PySetMethods;
pub use crate::types::string::PyStringMethods;
pub use crate::types::tuple::PyTupleMethods;
2 changes: 1 addition & 1 deletion src/types/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ impl PyDate {
///
/// This is equivalent to `datetime.date.fromtimestamp`
pub fn from_timestamp(py: Python<'_>, timestamp: i64) -> PyResult<&PyDate> {
let time_tuple = PyTuple::new(py, [timestamp]);
let time_tuple = PyTuple::new_bound(py, [timestamp]);

// safety ensure that the API is loaded
let _api = ensure_datetime_api(py);
Expand Down
2 changes: 1 addition & 1 deletion src/types/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1231,7 +1231,7 @@ mod tests {
Python::with_gil(|py| {
let list = PyList::new(py, vec![1, 2, 3]);
let tuple = list.to_tuple();
let tuple_expected = PyTuple::new(py, vec![1, 2, 3]);
let tuple_expected = PyTuple::new_bound(py, vec![1, 2, 3]);
assert!(tuple.eq(tuple_expected).unwrap());
})
}
Expand Down
4 changes: 2 additions & 2 deletions src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ pub mod iter {
pub use super::frozenset::{BoundFrozenSetIterator, PyFrozenSetIterator};
pub use super::list::{BoundListIterator, PyListIterator};
pub use super::set::{BoundSetIterator, PySetIterator};
pub use super::tuple::PyTupleIterator;
pub use super::tuple::{BorrowedTupleIterator, BoundTupleIterator, PyTupleIterator};
}

// Implementations core to all native types
Expand Down Expand Up @@ -305,5 +305,5 @@ pub(crate) mod set;
mod slice;
pub(crate) mod string;
mod traceback;
mod tuple;
pub(crate) mod tuple;
mod typeobject;
8 changes: 6 additions & 2 deletions src/types/sequence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1009,7 +1009,7 @@ mod tests {
assert!(seq
.to_tuple()
.unwrap()
.eq(PyTuple::new(py, ["foo", "bar"]))
.eq(PyTuple::new_bound(py, ["foo", "bar"]))
.unwrap());
});
}
Expand All @@ -1020,7 +1020,11 @@ mod tests {
let v = vec!["foo", "bar"];
let ob = v.to_object(py);
let seq = ob.downcast::<PySequence>(py).unwrap();
assert!(seq.to_tuple().unwrap().eq(PyTuple::new(py, &v)).unwrap());
assert!(seq
.to_tuple()
.unwrap()
.eq(PyTuple::new_bound(py, &v))
.unwrap());
});
}

Expand Down
Loading