Skip to content

Commit 0ca97b5

Browse files
authored
Merge pull request #3681 from davidhewitt/tuple2-try2
implement `PyTupleMethods`
2 parents 54390bc + 4cf58c8 commit 0ca97b5

15 files changed

+555
-112
lines changed

guide/src/conversions/traits.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ struct RustyTuple(String, String);
181181
# use pyo3::types::PyTuple;
182182
# fn main() -> PyResult<()> {
183183
# Python::with_gil(|py| -> PyResult<()> {
184-
# let tuple = PyTuple::new(py, vec!["test", "test2"]);
184+
# let tuple = PyTuple::new_bound(py, vec!["test", "test2"]);
185185
#
186186
# let rustytuple: RustyTuple = tuple.extract()?;
187187
# assert_eq!(rustytuple.0, "test");
@@ -204,7 +204,7 @@ struct RustyTuple((String,));
204204
# use pyo3::types::PyTuple;
205205
# fn main() -> PyResult<()> {
206206
# Python::with_gil(|py| -> PyResult<()> {
207-
# let tuple = PyTuple::new(py, vec!["test"]);
207+
# let tuple = PyTuple::new_bound(py, vec!["test"]);
208208
#
209209
# let rustytuple: RustyTuple = tuple.extract()?;
210210
# assert_eq!((rustytuple.0).0, "test");
@@ -482,7 +482,7 @@ If the input is neither a string nor an integer, the error message will be:
482482
- retrieve the field from a mapping, possibly with the custom key specified as an argument.
483483
- can be any literal that implements `ToBorrowedObject`
484484
- `pyo3(from_py_with = "...")`
485-
- apply a custom function to convert the field from Python the desired Rust type.
485+
- apply a custom function to convert the field from Python the desired Rust type.
486486
- the argument must be the name of the function as a string.
487487
- the function signature must be `fn(&PyAny) -> PyResult<T>` where `T` is the Rust type of the argument.
488488

guide/src/python_from_rust.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ fn main() -> PyResult<()> {
5151
fun.call0(py)?;
5252

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

5757
// pass arguments as rust tuple

pytests/src/datetime.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ fn make_date(py: Python<'_>, year: i32, month: u8, day: u8) -> PyResult<&PyDate>
1212
}
1313

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

1919
#[pyfunction]
@@ -48,8 +48,8 @@ fn time_with_fold<'p>(
4848
}
4949

5050
#[pyfunction]
51-
fn get_time_tuple<'p>(py: Python<'p>, dt: &PyTime) -> &'p PyTuple {
52-
PyTuple::new(
51+
fn get_time_tuple<'p>(py: Python<'p>, dt: &PyTime) -> Bound<'p, PyTuple> {
52+
PyTuple::new_bound(
5353
py,
5454
[
5555
dt.get_hour() as u32,
@@ -61,8 +61,8 @@ fn get_time_tuple<'p>(py: Python<'p>, dt: &PyTime) -> &'p PyTuple {
6161
}
6262

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

8282
#[pyfunction]
83-
fn get_delta_tuple<'p>(py: Python<'p>, delta: &PyDelta) -> &'p PyTuple {
84-
PyTuple::new(
83+
fn get_delta_tuple<'p>(py: Python<'p>, delta: &PyDelta) -> Bound<'p, PyTuple> {
84+
PyTuple::new_bound(
8585
py,
8686
[
8787
delta.get_days(),
@@ -118,8 +118,8 @@ fn make_datetime<'p>(
118118
}
119119

120120
#[pyfunction]
121-
fn get_datetime_tuple<'p>(py: Python<'p>, dt: &PyDateTime) -> &'p PyTuple {
122-
PyTuple::new(
121+
fn get_datetime_tuple<'p>(py: Python<'p>, dt: &PyDateTime) -> Bound<'p, PyTuple> {
122+
PyTuple::new_bound(
123123
py,
124124
[
125125
dt.get_year(),
@@ -134,8 +134,8 @@ fn get_datetime_tuple<'p>(py: Python<'p>, dt: &PyDateTime) -> &'p PyTuple {
134134
}
135135

136136
#[pyfunction]
137-
fn get_datetime_tuple_fold<'p>(py: Python<'p>, dt: &PyDateTime) -> &'p PyTuple {
138-
PyTuple::new(
137+
fn get_datetime_tuple_fold<'p>(py: Python<'p>, dt: &PyDateTime) -> Bound<'p, PyTuple> {
138+
PyTuple::new_bound(
139139
py,
140140
[
141141
dt.get_year(),

src/conversion.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ mod implementations {
465465
/// Converts `()` to an empty Python tuple.
466466
impl IntoPy<Py<PyTuple>> for () {
467467
fn into_py(self, py: Python<'_>) -> Py<PyTuple> {
468-
PyTuple::empty(py).into()
468+
PyTuple::empty_bound(py).unbind()
469469
}
470470
}
471471

src/impl_/extract_argument.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,7 @@ impl<'py> VarargsHandler<'py> for TupleVarargs {
615615
varargs: &[Option<&PyAny>],
616616
_function_description: &FunctionDescription,
617617
) -> PyResult<Self::Varargs> {
618-
Ok(PyTuple::new(py, varargs))
618+
Ok(PyTuple::new_bound(py, varargs).into_gil_ref())
619619
}
620620

621621
#[inline]
@@ -697,7 +697,7 @@ fn push_parameter_list(msg: &mut String, parameter_names: &[&str]) {
697697
mod tests {
698698
use crate::{
699699
types::{IntoPyDict, PyTuple},
700-
PyAny, Python, ToPyObject,
700+
PyAny, Python,
701701
};
702702

703703
use super::{push_parameter_list, FunctionDescription, NoVarargs, NoVarkeywords};
@@ -714,8 +714,8 @@ mod tests {
714714
};
715715

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

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

778778
Python::with_gil(|py| {
779-
let args = PyTuple::new(py, Vec::<&PyAny>::new());
779+
let args = PyTuple::new_bound(py, Vec::<&PyAny>::new());
780780
let mut output = [None, None];
781781
let err = unsafe {
782782
function_description.extract_arguments_tuple_dict::<NoVarargs, NoVarkeywords>(

src/impl_/pymodule.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ impl ModuleDef {
7272
.import("sys")?
7373
.getattr("implementation")?
7474
.getattr("version")?;
75-
if version.lt(crate::types::PyTuple::new(py, PYPY_GOOD_VERSION))? {
75+
if version.lt(crate::types::PyTuple::new_bound(py, PYPY_GOOD_VERSION))? {
7676
let warn = py.import("warnings")?.getattr("warn")?;
7777
warn.call1((
7878
"PyPy 3.7 versions older than 7.3.8 are known to have binary \

src/instance.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -325,10 +325,10 @@ impl<'py, T> Borrowed<'py, 'py, T>
325325
where
326326
T: HasPyGilRef,
327327
{
328-
// pub(crate) fn into_gil_ref(self) -> &'py T::AsRefTarget {
329-
// // Safety: self is a borrow over `'py`.
330-
// unsafe { self.py().from_borrowed_ptr(self.0.as_ptr()) }
331-
// }
328+
pub(crate) fn into_gil_ref(self) -> &'py T::AsRefTarget {
329+
// Safety: self is a borrow over `'py`.
330+
unsafe { self.py().from_borrowed_ptr(self.0.as_ptr()) }
331+
}
332332
}
333333

334334
impl<T> std::fmt::Debug for Borrowed<'_, '_, T> {

src/prelude.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,4 @@ pub use crate::types::module::PyModuleMethods;
3838
pub use crate::types::sequence::PySequenceMethods;
3939
pub use crate::types::set::PySetMethods;
4040
pub use crate::types::string::PyStringMethods;
41+
pub use crate::types::tuple::PyTupleMethods;

src/types/datetime.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ impl PyDate {
212212
///
213213
/// This is equivalent to `datetime.date.fromtimestamp`
214214
pub fn from_timestamp(py: Python<'_>, timestamp: i64) -> PyResult<&PyDate> {
215-
let time_tuple = PyTuple::new(py, [timestamp]);
215+
let time_tuple = PyTuple::new_bound(py, [timestamp]);
216216

217217
// safety ensure that the API is loaded
218218
let _api = ensure_datetime_api(py);

src/types/list.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1231,7 +1231,7 @@ mod tests {
12311231
Python::with_gil(|py| {
12321232
let list = PyList::new(py, vec![1, 2, 3]);
12331233
let tuple = list.to_tuple();
1234-
let tuple_expected = PyTuple::new(py, vec![1, 2, 3]);
1234+
let tuple_expected = PyTuple::new_bound(py, vec![1, 2, 3]);
12351235
assert!(tuple.eq(tuple_expected).unwrap());
12361236
})
12371237
}

src/types/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ pub mod iter {
8080
pub use super::frozenset::{BoundFrozenSetIterator, PyFrozenSetIterator};
8181
pub use super::list::{BoundListIterator, PyListIterator};
8282
pub use super::set::{BoundSetIterator, PySetIterator};
83-
pub use super::tuple::PyTupleIterator;
83+
pub use super::tuple::{BorrowedTupleIterator, BoundTupleIterator, PyTupleIterator};
8484
}
8585

8686
// Implementations core to all native types
@@ -305,5 +305,5 @@ pub(crate) mod set;
305305
mod slice;
306306
pub(crate) mod string;
307307
mod traceback;
308-
mod tuple;
308+
pub(crate) mod tuple;
309309
mod typeobject;

src/types/sequence.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,7 +1009,7 @@ mod tests {
10091009
assert!(seq
10101010
.to_tuple()
10111011
.unwrap()
1012-
.eq(PyTuple::new(py, ["foo", "bar"]))
1012+
.eq(PyTuple::new_bound(py, ["foo", "bar"]))
10131013
.unwrap());
10141014
});
10151015
}
@@ -1020,7 +1020,11 @@ mod tests {
10201020
let v = vec!["foo", "bar"];
10211021
let ob = v.to_object(py);
10221022
let seq = ob.downcast::<PySequence>(py).unwrap();
1023-
assert!(seq.to_tuple().unwrap().eq(PyTuple::new(py, &v)).unwrap());
1023+
assert!(seq
1024+
.to_tuple()
1025+
.unwrap()
1026+
.eq(PyTuple::new_bound(py, &v))
1027+
.unwrap());
10241028
});
10251029
}
10261030

0 commit comments

Comments
 (0)