diff --git a/src/types/dict.rs b/src/types/dict.rs index 8a356545166..f1d03a5bf2f 100644 --- a/src/types/dict.rs +++ b/src/types/dict.rs @@ -56,9 +56,14 @@ pyobject_native_type_core!( ); impl PyDict { - /// Creates a new empty dictionary. + /// Deprecated form of [`PyDict::new_bound`]. pub fn new(py: Python<'_>) -> &PyDict { - unsafe { py.from_owned_ptr(ffi::PyDict_New()) } + Self::new_bound(py).into_gil_ref() + } + + /// Creates a new empty dictionary. + pub fn new_bound(py: Python<'_>) -> Bound<'_, PyDict> { + unsafe { ffi::PyDict_New().assume_owned(py).downcast_into_unchecked() } } /// Creates a new dictionary from the sequence given. diff --git a/src/types/list.rs b/src/types/list.rs index f8db0e62ee6..429690eda06 100644 --- a/src/types/list.rs +++ b/src/types/list.rs @@ -56,6 +56,15 @@ pub(crate) fn new_from_iter<'py>( } impl PyList { + /// Deprecated form of [`PyList::new_bound`]. + pub fn new(py: Python<'_>, elements: impl IntoIterator) -> &PyList + where + T: ToPyObject, + U: ExactSizeIterator, + { + Self::new_bound(py, elements).into_gil_ref() + } + /// Constructs a new list with the given elements. /// /// If you want to create a [`PyList`] with elements of different or unknown types, or from an @@ -82,18 +91,30 @@ impl PyList { /// All standard library structures implement this trait correctly, if they do, so calling this /// function with (for example) [`Vec`]`` or `&[T]` will always succeed. #[track_caller] - pub fn new(py: Python<'_>, elements: impl IntoIterator) -> &PyList + pub fn new_bound( + py: Python<'_>, + elements: impl IntoIterator, + ) -> Bound<'_, PyList> where T: ToPyObject, U: ExactSizeIterator, { let mut iter = elements.into_iter().map(|e| e.to_object(py)); - new_from_iter(py, &mut iter).into_gil_ref() + new_from_iter(py, &mut iter) } - /// Constructs a new empty list. + /// Deprecated form of [`PyList::empty_bound`]. pub fn empty(py: Python<'_>) -> &PyList { - unsafe { py.from_owned_ptr(ffi::PyList_New(0)) } + Self::empty_bound(py).into_gil_ref() + } + + /// Constructs a new empty list. + pub fn empty_bound(py: Python<'_>) -> Bound<'_, PyList> { + unsafe { + ffi::PyList_New(0) + .assume_owned(py) + .downcast_into_unchecked() + } } /// Returns the length of the list.