Skip to content

Commit

Permalink
migrate PyTuple::new
Browse files Browse the repository at this point in the history
  • Loading branch information
Icxolu committed Sep 26, 2024
1 parent cf9407e commit dd87350
Show file tree
Hide file tree
Showing 16 changed files with 122 additions and 77 deletions.
4 changes: 2 additions & 2 deletions guide/src/conversions/traits.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,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(py, vec!["test", "test2"])?;
#
# let rustytuple: RustyTuple = tuple.extract()?;
# assert_eq!(rustytuple.0, "test");
Expand All @@ -206,7 +206,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(py, vec!["test"])?;
#
# let rustytuple: RustyTuple = tuple.extract()?;
# assert_eq!((rustytuple.0).0, "test");
Expand Down
2 changes: 1 addition & 1 deletion guide/src/python-from-rust/function-calls.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ fn main() -> PyResult<()> {
fun.call1(py, args)?;

// call object with Python tuple of positional arguments
let args = PyTuple::new(py, &[arg1, arg2, arg3]);
let args = PyTuple::new(py, &[arg1, arg2, arg3])?;
fun.call1(py, args)?;
Ok(())
})
Expand Down
4 changes: 2 additions & 2 deletions guide/src/types.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ use pyo3::types::PyTuple;

# fn example<'py>(py: Python<'py>) -> PyResult<()> {
// Create a new tuple with the elements (0, 1, 2)
let t = PyTuple::new(py, [0, 1, 2]);
let t = PyTuple::new(py, [0, 1, 2])?;
for i in 0..=2 {
let entry: Borrowed<'_, 'py, PyAny> = t.get_borrowed_item(i)?;
// `PyAnyMethods::extract` is available on `Borrowed`
Expand Down Expand Up @@ -295,7 +295,7 @@ For example, the following snippet extracts a Rust tuple of integers from a Pyth
# use pyo3::types::PyTuple;
# fn example<'py>(py: Python<'py>) -> PyResult<()> {
// create a new Python `tuple`, and use `.into_any()` to erase the type
let obj: Bound<'py, PyAny> = PyTuple::new(py, [1, 2, 3]).into_any();
let obj: Bound<'py, PyAny> = PyTuple::new(py, [1, 2, 3])?.into_any();

// extracting the Python `tuple` to a rust `(i32, i32, i32)` tuple
let (x, y, z) = obj.extract::<(i32, i32, i32)>()?;
Expand Down
12 changes: 6 additions & 6 deletions pytests/src/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fn make_date(py: Python<'_>, year: i32, month: u8, day: u8) -> PyResult<Bound<'_
}

#[pyfunction]
fn get_date_tuple<'py>(d: &Bound<'py, PyDate>) -> Bound<'py, PyTuple> {
fn get_date_tuple<'py>(d: &Bound<'py, PyDate>) -> PyResult<Bound<'py, PyTuple>> {
PyTuple::new(
d.py(),
[d.get_year(), d.get_month() as i32, d.get_day() as i32],
Expand Down Expand Up @@ -52,7 +52,7 @@ fn time_with_fold<'py>(
}

#[pyfunction]
fn get_time_tuple<'py>(dt: &Bound<'py, PyTime>) -> Bound<'py, PyTuple> {
fn get_time_tuple<'py>(dt: &Bound<'py, PyTime>) -> PyResult<Bound<'py, PyTuple>> {
PyTuple::new(
dt.py(),
[
Expand All @@ -65,7 +65,7 @@ fn get_time_tuple<'py>(dt: &Bound<'py, PyTime>) -> Bound<'py, PyTuple> {
}

#[pyfunction]
fn get_time_tuple_fold<'py>(dt: &Bound<'py, PyTime>) -> Bound<'py, PyTuple> {
fn get_time_tuple_fold<'py>(dt: &Bound<'py, PyTime>) -> PyResult<Bound<'py, PyTuple>> {
PyTuple::new(
dt.py(),
[
Expand All @@ -89,7 +89,7 @@ fn make_delta(
}

#[pyfunction]
fn get_delta_tuple<'py>(delta: &Bound<'py, PyDelta>) -> Bound<'py, PyTuple> {
fn get_delta_tuple<'py>(delta: &Bound<'py, PyDelta>) -> PyResult<Bound<'py, PyTuple>> {
PyTuple::new(
delta.py(),
[
Expand Down Expand Up @@ -128,7 +128,7 @@ fn make_datetime<'py>(
}

#[pyfunction]
fn get_datetime_tuple<'py>(dt: &Bound<'py, PyDateTime>) -> Bound<'py, PyTuple> {
fn get_datetime_tuple<'py>(dt: &Bound<'py, PyDateTime>) -> PyResult<Bound<'py, PyTuple>> {
PyTuple::new(
dt.py(),
[
Expand All @@ -144,7 +144,7 @@ fn get_datetime_tuple<'py>(dt: &Bound<'py, PyDateTime>) -> Bound<'py, PyTuple> {
}

#[pyfunction]
fn get_datetime_tuple_fold<'py>(dt: &Bound<'py, PyDateTime>) -> Bound<'py, PyTuple> {
fn get_datetime_tuple_fold<'py>(dt: &Bound<'py, PyDateTime>) -> PyResult<Bound<'py, PyTuple>> {
PyTuple::new(
dt.py(),
[
Expand Down
2 changes: 1 addition & 1 deletion src/impl_/extract_argument.rs
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,7 @@ impl<'py> VarargsHandler<'py> for TupleVarargs {
varargs: &[Option<PyArg<'py>>],
_function_description: &FunctionDescription,
) -> PyResult<Self::Varargs> {
Ok(PyTuple::new(py, varargs))
PyTuple::new(py, varargs)
}

#[inline]
Expand Down
2 changes: 1 addition & 1 deletion src/impl_/pymodule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,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(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
2 changes: 1 addition & 1 deletion src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,7 @@ impl<'a, 'py, T> Borrowed<'a, 'py, T> {
///
/// # fn main() -> PyResult<()> {
/// Python::with_gil(|py| -> PyResult<()> {
/// let tuple = PyTuple::new(py, [1, 2, 3]);
/// let tuple = PyTuple::new(py, [1, 2, 3])?;
///
/// // borrows from `tuple`, so can only be
/// // used while `tuple` stays alive
Expand Down
2 changes: 1 addition & 1 deletion src/types/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ impl PyDate {
///
/// This is equivalent to `datetime.date.fromtimestamp`
pub fn from_timestamp(py: Python<'_>, timestamp: i64) -> PyResult<Bound<'_, PyDate>> {
let time_tuple = PyTuple::new(py, [timestamp]);
let time_tuple = PyTuple::new(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 @@ -1108,7 +1108,7 @@ mod tests {
Python::with_gil(|py| {
let list = PyList::new(py, vec![1, 2, 3]).unwrap();
let tuple = list.to_tuple();
let tuple_expected = PyTuple::new(py, vec![1, 2, 3]);
let tuple_expected = PyTuple::new(py, vec![1, 2, 3]).unwrap();
assert!(tuple.eq(tuple_expected).unwrap());
})
}
Expand Down
8 changes: 6 additions & 2 deletions src/types/sequence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -777,7 +777,7 @@ mod tests {
assert!(seq
.to_tuple()
.unwrap()
.eq(PyTuple::new(py, ["foo", "bar"]))
.eq(PyTuple::new(py, ["foo", "bar"]).unwrap())
.unwrap());
});
}
Expand All @@ -788,7 +788,11 @@ mod tests {
let v = vec!["foo", "bar"];
let ob = (&v).into_pyobject(py).unwrap();
let seq = ob.downcast::<PySequence>().unwrap();
assert!(seq.to_tuple().unwrap().eq(PyTuple::new(py, &v)).unwrap());
assert!(seq
.to_tuple()
.unwrap()
.eq(PyTuple::new(py, &v).unwrap())
.unwrap());
});
}

Expand Down
Loading

0 comments on commit dd87350

Please sign in to comment.