diff --git a/src/conversion.rs b/src/conversion.rs index 21c80669..f1438b98 100644 --- a/src/conversion.rs +++ b/src/conversion.rs @@ -426,7 +426,7 @@ where /// Converts `()` to an empty Python tuple. impl FromPy<()> for Py { fn from_py(_: (), py: Python) -> Py { - PyTuple::empty(py) + Py::from_py(PyTuple::empty(py), py) } } diff --git a/src/types/tuple.rs b/src/types/tuple.rs index 5f67cf69..63e956dc 100644 --- a/src/types/tuple.rs +++ b/src/types/tuple.rs @@ -21,7 +21,7 @@ pyobject_native_type!(PyTuple, ffi::PyTuple_Type, ffi::PyTuple_Check); impl PyTuple { /// Construct a new tuple with the given elements. - pub fn new(py: Python, elements: impl IntoIterator) -> Py + pub fn new<'p, T, U>(py: Python<'p>, elements: impl IntoIterator) -> &'p PyTuple where T: ToPyObject, U: ExactSizeIterator, @@ -33,13 +33,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 { - 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. @@ -264,8 +264,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()); diff --git a/tests/test_gc.rs b/tests/test_gc.rs index 7a9d032c..b3e6a112 100644 --- a/tests/test_gc.rs +++ b/tests/test_gc.rs @@ -93,8 +93,9 @@ 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 = FromPy::from_py(PyTuple::empty(py), py); + let _empty2: Py = FromPy::from_py(PyTuple::empty(py), py); + let _empty2: PyObject = _empty2.into(); let _empty3: &PyAny = py.from_owned_ptr(ffi::PyTuple_New(0)); } } @@ -110,7 +111,7 @@ fn create_pointers_in_drop() { { let gil = Python::acquire_gil(); let py = gil.python(); - let empty = PyTuple::empty(py); + let empty: Py = FromPy::from_py(PyTuple::empty(py), py); ptr = empty.as_ptr(); cnt = empty.get_refcnt() - 1; let inst = Py::new(py, ClassWithDrop {}).unwrap();