Skip to content

Commit

Permalink
Merge pull request #447 from birkenfeld/tuple_new
Browse files Browse the repository at this point in the history
Make PyTuple constructors return &PyTuple
  • Loading branch information
kngwyu committed May 25, 2019
2 parents a5a2059 + e4287e4 commit 4d7ca3a
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 20 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
* `PySet::new` and `PyFrozenSet::new` now return `PyResult<&Py[Frozen]Set>`; exceptions are raised if
the items are not hashable.
* Fixed building using `venv` on Windows.
* `PyTuple::new` now returns `&PyTuple` instead of `Py<PyTuple>`.

## [0.6.0] - 2018-03-28

Expand Down
12 changes: 6 additions & 6 deletions examples/rustapi_module/src/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ fn make_date<'p>(py: Python<'p>, year: i32, month: u8, day: u8) -> PyResult<&'p
}

#[pyfunction]
fn get_date_tuple(py: Python<'_>, d: &PyDate) -> Py<PyTuple> {
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],
Expand Down Expand Up @@ -65,7 +65,7 @@ fn time_with_fold<'p>(
}

#[pyfunction]
fn get_time_tuple(py: Python<'_>, dt: &PyTime) -> Py<PyTuple> {
fn get_time_tuple<'p>(py: Python<'p>, dt: &PyTime) -> &'p PyTuple {
PyTuple::new(
py,
&[
Expand All @@ -79,7 +79,7 @@ fn get_time_tuple(py: Python<'_>, dt: &PyTime) -> Py<PyTuple> {

#[cfg(Py_3_6)]
#[pyfunction]
fn get_time_tuple_fold(py: Python, dt: &PyTime) -> Py<PyTuple> {
fn get_time_tuple_fold<'p>(py: Python<'p>, dt: &PyTime) -> &'p PyTuple {
PyTuple::new(
py,
&[
Expand All @@ -103,7 +103,7 @@ fn make_delta<'p>(
}

#[pyfunction]
fn get_delta_tuple(py: Python<'_>, delta: &PyDelta) -> Py<PyTuple> {
fn get_delta_tuple<'p>(py: Python<'p>, delta: &PyDelta) -> &'p PyTuple {
PyTuple::new(
py,
&[
Expand Down Expand Up @@ -140,7 +140,7 @@ fn make_datetime<'p>(
}

#[pyfunction]
fn get_datetime_tuple(py: Python<'_>, dt: &PyDateTime) -> Py<PyTuple> {
fn get_datetime_tuple<'p>(py: Python<'p>, dt: &PyDateTime) -> &'p PyTuple {
PyTuple::new(
py,
&[
Expand All @@ -157,7 +157,7 @@ fn get_datetime_tuple(py: Python<'_>, dt: &PyDateTime) -> Py<PyTuple> {

#[cfg(Py_3_6)]
#[pyfunction]
fn get_datetime_tuple_fold(py: Python, dt: &PyDateTime) -> Py<PyTuple> {
fn get_datetime_tuple_fold<'p>(py: Python<'p>, dt: &PyDateTime) -> &'p PyTuple {
PyTuple::new(
py,
&[
Expand Down
2 changes: 1 addition & 1 deletion src/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ where
/// Converts `()` to an empty Python tuple.
impl FromPy<()> for Py<PyTuple> {
fn from_py(_: (), py: Python) -> Py<PyTuple> {
PyTuple::empty(py)
Py::from_py(PyTuple::empty(py), py)
}
}

Expand Down
14 changes: 8 additions & 6 deletions src/types/tuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ pyobject_native_type!(PyTuple, ffi::PyTuple_Type, ffi::PyTuple_Check);

impl PyTuple {
/// Construct a new tuple with the given elements.
pub fn new<T, U>(py: Python, elements: impl IntoIterator<Item = T, IntoIter = U>) -> Py<PyTuple>
pub fn new<'p, T, U>(
py: Python<'p>,
elements: impl IntoIterator<Item = T, IntoIter = U>,
) -> &'p PyTuple
where
T: ToPyObject,
U: ExactSizeIterator<Item = T>,
Expand All @@ -33,13 +36,13 @@ impl PyTuple {
for (i, e) in elements_iter.enumerate() {
ffi::PyTuple_SetItem(ptr, i as Py_ssize_t, e.to_object(py).into_ptr());
}
Py::from_owned_ptr_or_panic(ptr)
py.from_owned_ptr(ptr)
}
}

/// Retrieves the empty tuple.
pub fn empty(_py: Python) -> Py<PyTuple> {
unsafe { Py::from_owned_ptr_or_panic(ffi::PyTuple_New(0)) }
pub fn empty<'p>(py: Python<'p>) -> &'p PyTuple {
unsafe { py.from_owned_ptr(ffi::PyTuple_New(0)) }
}

/// Gets the length of the tuple.
Expand Down Expand Up @@ -264,8 +267,7 @@ mod test {
fn test_new() {
let gil = Python::acquire_gil();
let py = gil.python();
let pyob = PyTuple::new(py, &[1, 2, 3]);
let ob = pyob.as_ref(py);
let ob = PyTuple::new(py, &[1, 2, 3]);
assert_eq!(3, ob.len());
let ob: &PyAny = ob.into();
assert_eq!((1, 2, 3), ob.extract().unwrap());
Expand Down
9 changes: 5 additions & 4 deletions tests/test_gc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ impl Drop for ClassWithDrop {
unsafe {
let py = Python::assume_gil_acquired();

let _empty1 = PyTuple::empty(py);
let _empty2: PyObject = PyTuple::empty(py).into();
let _empty1: Py<PyTuple> = FromPy::from_py(PyTuple::empty(py), py);
let _empty2 = PyTuple::empty(py).into_object(py);
let _empty3: &PyAny = py.from_owned_ptr(ffi::PyTuple_New(0));
}
}
Expand All @@ -110,9 +110,10 @@ fn create_pointers_in_drop() {
{
let gil = Python::acquire_gil();
let py = gil.python();
let empty = PyTuple::empty(py);
let empty = PyTuple::empty(py).into_object(py);
ptr = empty.as_ptr();
cnt = empty.get_refcnt() - 1;
// substract 2, because `PyTuple::empty(py).into_object(py)` increases the refcnt by 2
cnt = empty.get_refcnt() - 2;
let inst = Py::new(py, ClassWithDrop {}).unwrap();
drop(inst);
}
Expand Down
7 changes: 4 additions & 3 deletions tests/test_various.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,10 @@ impl PickleSupport {
obj.init({ PickleSupport {} });
}

pub fn __reduce__(slf: PyRef<Self>) -> PyResult<(PyObject, Py<PyTuple>, PyObject)> {
let gil = Python::acquire_gil();
let py = gil.python();
pub fn __reduce__<'py>(
slf: PyRef<Self>,
py: Python<'py>,
) -> PyResult<(PyObject, &'py PyTuple, PyObject)> {
let cls = slf.to_object(py).getattr(py, "__class__")?;
let dict = slf.to_object(py).getattr(py, "__dict__")?;
Ok((cls, PyTuple::empty(py), dict))
Expand Down

0 comments on commit 4d7ca3a

Please sign in to comment.