diff --git a/pyo3-macros-backend/src/frompyobject.rs b/pyo3-macros-backend/src/frompyobject.rs index 2b527dc8..f4774d88 100644 --- a/pyo3-macros-backend/src/frompyobject.rs +++ b/pyo3-macros-backend/src/frompyobject.rs @@ -568,8 +568,8 @@ pub fn build_derive_from_pyobject(tokens: &DeriveInput) -> Result { let lt_param = if let Some(lt) = verify_and_get_lifetime(generics)? { lt.clone() } else { - trait_generics.params.push(parse_quote!('source)); - parse_quote!('source) + trait_generics.params.push(parse_quote!('py)); + parse_quote!('py) }; let mut where_clause: syn::WhereClause = parse_quote!(where); for param in generics.type_params() { diff --git a/src/buffer.rs b/src/buffer.rs index 40bf1a1e..d18f05e2 100644 --- a/src/buffer.rs +++ b/src/buffer.rs @@ -182,7 +182,7 @@ pub unsafe trait Element: Copy { fn is_compatible_format(format: &CStr) -> bool; } -impl<'source, T: Element> FromPyObject<'source> for PyBuffer { +impl<'py, T: Element> FromPyObject<'py> for PyBuffer { fn extract_bound(obj: &Bound<'_, PyAny>) -> PyResult> { Self::get(obj.as_gil_ref()) } diff --git a/src/conversion.rs b/src/conversion.rs index 3af038af..d9536aa9 100644 --- a/src/conversion.rs +++ b/src/conversion.rs @@ -209,7 +209,7 @@ pub trait IntoPy: Sized { /// depend on the lifetime of the `obj` or the `prepared` variable. /// /// For example, when extracting `&str` from a Python byte string, the resulting string slice will -/// point to the existing string data (lifetime: `'source`). +/// point to the existing string data (lifetime: `'py`). /// On the other hand, when extracting `&str` from a Python Unicode string, the preparation step /// will convert the string to UTF-8, and the resulting string slice will have lifetime `'prepared`. /// Since which case applies depends on the runtime type of the Python object, @@ -219,12 +219,12 @@ pub trait IntoPy: Sized { /// has two methods `extract` and `extract_bound` which are defaulted to call each other. To avoid /// infinite recursion, implementors must implement at least one of these methods. The recommendation /// is to implement `extract_bound` and leave `extract` as the default implementation. -pub trait FromPyObject<'source>: Sized { +pub trait FromPyObject<'py>: Sized { /// Extracts `Self` from the source GIL Ref `obj`. /// /// Implementors are encouraged to implement `extract_bound` and leave this method as the /// default implementation, which will forward calls to `extract_bound`. - fn extract(ob: &'source PyAny) -> PyResult { + fn extract(ob: &'py PyAny) -> PyResult { Self::extract_bound(&ob.as_borrowed()) } @@ -232,7 +232,7 @@ pub trait FromPyObject<'source>: Sized { /// /// Implementors are encouraged to implement this method and leave `extract` defaulted, as /// this will be most compatible with PyO3's future API. - fn extract_bound(ob: &Bound<'source, PyAny>) -> PyResult { + fn extract_bound(ob: &Bound<'py, PyAny>) -> PyResult { Self::extract(ob.clone().into_gil_ref()) } @@ -308,56 +308,56 @@ impl> IntoPy for Cell { } } -impl<'a, T: FromPyObject<'a>> FromPyObject<'a> for Cell { - fn extract(ob: &'a PyAny) -> PyResult { +impl<'py, T: FromPyObject<'py>> FromPyObject<'py> for Cell { + fn extract(ob: &'py PyAny) -> PyResult { T::extract(ob).map(Cell::new) } } -impl<'a, T> FromPyObject<'a> for &'a PyCell +impl<'py, T> FromPyObject<'py> for &'py PyCell where T: PyClass, { - fn extract(obj: &'a PyAny) -> PyResult { + fn extract(obj: &'py PyAny) -> PyResult { obj.downcast().map_err(Into::into) } } -impl<'a, T> FromPyObject<'a> for T +impl FromPyObject<'_> for T where T: PyClass + Clone, { - fn extract(obj: &'a PyAny) -> PyResult { + fn extract(obj: &PyAny) -> PyResult { let cell: &PyCell = obj.downcast()?; Ok(unsafe { cell.try_borrow_unguarded()?.clone() }) } } -impl<'a, T> FromPyObject<'a> for PyRef<'a, T> +impl<'py, T> FromPyObject<'py> for PyRef<'py, T> where T: PyClass, { - fn extract(obj: &'a PyAny) -> PyResult { + fn extract(obj: &'py PyAny) -> PyResult { let cell: &PyCell = obj.downcast()?; cell.try_borrow().map_err(Into::into) } } -impl<'a, T> FromPyObject<'a> for PyRefMut<'a, T> +impl<'py, T> FromPyObject<'py> for PyRefMut<'py, T> where T: PyClass, { - fn extract(obj: &'a PyAny) -> PyResult { + fn extract(obj: &'py PyAny) -> PyResult { let cell: &PyCell = obj.downcast()?; cell.try_borrow_mut().map_err(Into::into) } } -impl<'a, T> FromPyObject<'a> for Option +impl<'py, T> FromPyObject<'py> for Option where - T: FromPyObject<'a>, + T: FromPyObject<'py>, { - fn extract(obj: &'a PyAny) -> PyResult { + fn extract(obj: &'py PyAny) -> PyResult { if obj.as_ptr() == unsafe { ffi::Py_None() } { Ok(None) } else { diff --git a/src/conversions/chrono.rs b/src/conversions/chrono.rs index 4a00d8a3..6e529918 100644 --- a/src/conversions/chrono.rs +++ b/src/conversions/chrono.rs @@ -275,7 +275,7 @@ impl IntoPy for DateTime { } } -impl FromPyObject<'a>> FromPyObject<'_> for DateTime { +impl FromPyObject<'py>> FromPyObject<'_> for DateTime { fn extract_bound(dt: &Bound<'_, PyAny>) -> PyResult> { #[cfg(not(Py_LIMITED_API))] let dt = dt.downcast::()?; diff --git a/src/conversions/either.rs b/src/conversions/either.rs index 897369e5..c763cdf9 100644 --- a/src/conversions/either.rs +++ b/src/conversions/either.rs @@ -82,13 +82,13 @@ where } #[cfg_attr(docsrs, doc(cfg(feature = "either")))] -impl<'source, L, R> FromPyObject<'source> for Either +impl<'py, L, R> FromPyObject<'py> for Either where - L: FromPyObject<'source>, - R: FromPyObject<'source>, + L: FromPyObject<'py>, + R: FromPyObject<'py>, { #[inline] - fn extract_bound(obj: &Bound<'source, PyAny>) -> PyResult { + fn extract_bound(obj: &Bound<'py, PyAny>) -> PyResult { if let Ok(l) = obj.extract::() { Ok(Either::Left(l)) } else if let Ok(r) = obj.extract::() { diff --git a/src/conversions/hashbrown.rs b/src/conversions/hashbrown.rs index f8260037..d2cbe4ad 100644 --- a/src/conversions/hashbrown.rs +++ b/src/conversions/hashbrown.rs @@ -51,13 +51,13 @@ where } } -impl<'source, K, V, S> FromPyObject<'source> for hashbrown::HashMap +impl<'py, K, V, S> FromPyObject<'py> for hashbrown::HashMap where - K: FromPyObject<'source> + cmp::Eq + hash::Hash, - V: FromPyObject<'source>, + K: FromPyObject<'py> + cmp::Eq + hash::Hash, + V: FromPyObject<'py>, S: hash::BuildHasher + Default, { - fn extract_bound(ob: &Bound<'source, PyAny>) -> Result { + fn extract_bound(ob: &Bound<'py, PyAny>) -> Result { let dict = ob.downcast::()?; let mut ret = hashbrown::HashMap::with_capacity_and_hasher(dict.len(), S::default()); for (k, v) in dict.iter() { @@ -90,12 +90,12 @@ where } } -impl<'source, K, S> FromPyObject<'source> for hashbrown::HashSet +impl<'py, K, S> FromPyObject<'py> for hashbrown::HashSet where - K: FromPyObject<'source> + cmp::Eq + hash::Hash, + K: FromPyObject<'py> + cmp::Eq + hash::Hash, S: hash::BuildHasher + Default, { - fn extract_bound(ob: &Bound<'source, PyAny>) -> PyResult { + fn extract_bound(ob: &Bound<'py, PyAny>) -> PyResult { match ob.downcast::() { Ok(set) => set.iter().map(|any| any.extract()).collect(), Err(err) => { diff --git a/src/conversions/indexmap.rs b/src/conversions/indexmap.rs index 1bd638da..53f7f936 100644 --- a/src/conversions/indexmap.rs +++ b/src/conversions/indexmap.rs @@ -118,13 +118,13 @@ where } } -impl<'source, K, V, S> FromPyObject<'source> for indexmap::IndexMap +impl<'py, K, V, S> FromPyObject<'py> for indexmap::IndexMap where - K: FromPyObject<'source> + cmp::Eq + hash::Hash, - V: FromPyObject<'source>, + K: FromPyObject<'py> + cmp::Eq + hash::Hash, + V: FromPyObject<'py>, S: hash::BuildHasher + Default, { - fn extract_bound(ob: &Bound<'source, PyAny>) -> Result { + fn extract_bound(ob: &Bound<'py, PyAny>) -> Result { let dict = ob.downcast::()?; let mut ret = indexmap::IndexMap::with_capacity_and_hasher(dict.len(), S::default()); for (k, v) in dict.iter() { diff --git a/src/conversions/num_bigint.rs b/src/conversions/num_bigint.rs index 41845814..1d536623 100644 --- a/src/conversions/num_bigint.rs +++ b/src/conversions/num_bigint.rs @@ -111,8 +111,8 @@ bigint_conversion!(BigUint, 0, BigUint::to_bytes_le); bigint_conversion!(BigInt, 1, BigInt::to_signed_bytes_le); #[cfg_attr(docsrs, doc(cfg(feature = "num-bigint")))] -impl<'source> FromPyObject<'source> for BigInt { - fn extract_bound(ob: &Bound<'source, PyAny>) -> PyResult { +impl<'py> FromPyObject<'py> for BigInt { + fn extract_bound(ob: &Bound<'py, PyAny>) -> PyResult { let py = ob.py(); // fast path - checking for subclass of `int` just checks a bit in the type object let num_owned: Py; @@ -159,8 +159,8 @@ impl<'source> FromPyObject<'source> for BigInt { } #[cfg_attr(docsrs, doc(cfg(feature = "num-bigint")))] -impl<'source> FromPyObject<'source> for BigUint { - fn extract_bound(ob: &Bound<'source, PyAny>) -> PyResult { +impl<'py> FromPyObject<'py> for BigUint { + fn extract_bound(ob: &Bound<'py, PyAny>) -> PyResult { let py = ob.py(); // fast path - checking for subclass of `int` just checks a bit in the type object let num_owned: Py; diff --git a/src/conversions/smallvec.rs b/src/conversions/smallvec.rs index d9ed8419..ade64a51 100644 --- a/src/conversions/smallvec.rs +++ b/src/conversions/smallvec.rs @@ -54,12 +54,12 @@ where } } -impl<'a, A> FromPyObject<'a> for SmallVec +impl<'py, A> FromPyObject<'py> for SmallVec where A: Array, - A::Item: FromPyObject<'a>, + A::Item: FromPyObject<'py>, { - fn extract_bound(obj: &Bound<'a, PyAny>) -> PyResult { + fn extract_bound(obj: &Bound<'py, PyAny>) -> PyResult { if obj.is_instance_of::() { return Err(PyTypeError::new_err("Can't extract `str` to `SmallVec`")); } @@ -72,10 +72,10 @@ where } } -fn extract_sequence<'s, A>(obj: &Bound<'s, PyAny>) -> PyResult> +fn extract_sequence<'py, A>(obj: &Bound<'py, PyAny>) -> PyResult> where A: Array, - A::Item: FromPyObject<'s>, + A::Item: FromPyObject<'py>, { // Types that pass `PySequence_Check` usually implement enough of the sequence protocol // to support this function and if not, we will only fail extraction safely. diff --git a/src/conversions/std/array.rs b/src/conversions/std/array.rs index e5e1744e..0ea16713 100644 --- a/src/conversions/std/array.rs +++ b/src/conversions/std/array.rs @@ -45,18 +45,18 @@ where } } -impl<'a, T, const N: usize> FromPyObject<'a> for [T; N] +impl<'py, T, const N: usize> FromPyObject<'py> for [T; N] where - T: FromPyObject<'a>, + T: FromPyObject<'py>, { - fn extract_bound(obj: &Bound<'a, PyAny>) -> PyResult { + fn extract_bound(obj: &Bound<'py, PyAny>) -> PyResult { create_array_from_obj(obj) } } -fn create_array_from_obj<'s, T, const N: usize>(obj: &Bound<'s, PyAny>) -> PyResult<[T; N]> +fn create_array_from_obj<'py, T, const N: usize>(obj: &Bound<'py, PyAny>) -> PyResult<[T; N]> where - T: FromPyObject<'s>, + T: FromPyObject<'py>, { // Types that pass `PySequence_Check` usually implement enough of the sequence protocol // to support this function and if not, we will only fail extraction safely. diff --git a/src/conversions/std/map.rs b/src/conversions/std/map.rs index 1b47c870..1c3f669e 100644 --- a/src/conversions/std/map.rs +++ b/src/conversions/std/map.rs @@ -67,13 +67,13 @@ where } } -impl<'source, K, V, S> FromPyObject<'source> for collections::HashMap +impl<'py, K, V, S> FromPyObject<'py> for collections::HashMap where - K: FromPyObject<'source> + cmp::Eq + hash::Hash, - V: FromPyObject<'source>, + K: FromPyObject<'py> + cmp::Eq + hash::Hash, + V: FromPyObject<'py>, S: hash::BuildHasher + Default, { - fn extract_bound(ob: &Bound<'source, PyAny>) -> Result { + fn extract_bound(ob: &Bound<'py, PyAny>) -> Result { let dict = ob.downcast::()?; let mut ret = collections::HashMap::with_capacity_and_hasher(dict.len(), S::default()); for (k, v) in dict.iter() { @@ -88,12 +88,12 @@ where } } -impl<'source, K, V> FromPyObject<'source> for collections::BTreeMap +impl<'py, K, V> FromPyObject<'py> for collections::BTreeMap where - K: FromPyObject<'source> + cmp::Ord, - V: FromPyObject<'source>, + K: FromPyObject<'py> + cmp::Ord, + V: FromPyObject<'py>, { - fn extract_bound(ob: &Bound<'source, PyAny>) -> Result { + fn extract_bound(ob: &Bound<'py, PyAny>) -> Result { let dict = ob.downcast::()?; let mut ret = collections::BTreeMap::new(); for (k, v) in dict.iter() { diff --git a/src/conversions/std/num.rs b/src/conversions/std/num.rs index 1ced6cbb..82f63016 100644 --- a/src/conversions/std/num.rs +++ b/src/conversions/std/num.rs @@ -127,7 +127,7 @@ macro_rules! int_fits_c_long { } } - impl<'source> FromPyObject<'source> for $rust_type { + impl<'py> FromPyObject<'py> for $rust_type { fn extract_bound(obj: &Bound<'_, PyAny>) -> PyResult { let val: c_long = extract_int!(obj, -1, ffi::PyLong_AsLong)?; <$rust_type>::try_from(val) diff --git a/src/conversions/std/set.rs b/src/conversions/std/set.rs index 7d1b91c5..c955801a 100644 --- a/src/conversions/std/set.rs +++ b/src/conversions/std/set.rs @@ -51,12 +51,12 @@ where } } -impl<'source, K, S> FromPyObject<'source> for collections::HashSet +impl<'py, K, S> FromPyObject<'py> for collections::HashSet where - K: FromPyObject<'source> + cmp::Eq + hash::Hash, + K: FromPyObject<'py> + cmp::Eq + hash::Hash, S: hash::BuildHasher + Default, { - fn extract_bound(ob: &Bound<'source, PyAny>) -> PyResult { + fn extract_bound(ob: &Bound<'py, PyAny>) -> PyResult { match ob.downcast::() { Ok(set) => set.iter().map(|any| any.extract()).collect(), Err(err) => { @@ -91,11 +91,11 @@ where } } -impl<'source, K> FromPyObject<'source> for collections::BTreeSet +impl<'py, K> FromPyObject<'py> for collections::BTreeSet where - K: FromPyObject<'source> + cmp::Ord, + K: FromPyObject<'py> + cmp::Ord, { - fn extract_bound(ob: &Bound<'source, PyAny>) -> PyResult { + fn extract_bound(ob: &Bound<'py, PyAny>) -> PyResult { match ob.downcast::() { Ok(set) => set.iter().map(|any| any.extract()).collect(), Err(err) => { diff --git a/src/conversions/std/slice.rs b/src/conversions/std/slice.rs index f77cd661..62809327 100644 --- a/src/conversions/std/slice.rs +++ b/src/conversions/std/slice.rs @@ -13,8 +13,8 @@ impl<'a> IntoPy for &'a [u8] { } } -impl<'a> FromPyObject<'a> for &'a [u8] { - fn extract(obj: &'a PyAny) -> PyResult { +impl<'py> FromPyObject<'py> for &'py [u8] { + fn extract(obj: &'py PyAny) -> PyResult { Ok(obj.downcast::()?.as_bytes()) } diff --git a/src/conversions/std/string.rs b/src/conversions/std/string.rs index 2ac08855..ac29d804 100644 --- a/src/conversions/std/string.rs +++ b/src/conversions/std/string.rs @@ -114,8 +114,8 @@ impl<'a> IntoPy for &'a String { /// Allows extracting strings from Python objects. /// Accepts Python `str` and `unicode` objects. -impl<'source> FromPyObject<'source> for &'source str { - fn extract(ob: &'source PyAny) -> PyResult { +impl<'py> FromPyObject<'py> for &'py str { + fn extract(ob: &'py PyAny) -> PyResult { ob.downcast::()?.to_str() } diff --git a/src/instance.rs b/src/instance.rs index 793d0e32..92fc0185 100644 --- a/src/instance.rs +++ b/src/instance.rs @@ -996,9 +996,9 @@ impl Py { /// Extracts some type from the Python object. /// /// This is a wrapper function around `FromPyObject::extract()`. - pub fn extract<'p, D>(&'p self, py: Python<'p>) -> PyResult + pub fn extract<'py, D>(&'py self, py: Python<'py>) -> PyResult where - D: FromPyObject<'p>, + D: FromPyObject<'py>, { FromPyObject::extract(unsafe { py.from_borrowed_ptr(self.as_ptr()) }) } diff --git a/src/types/any.rs b/src/types/any.rs index 61c3c672..d79a9227 100644 --- a/src/types/any.rs +++ b/src/types/any.rs @@ -801,9 +801,9 @@ impl PyAny { /// /// This is a wrapper function around [`FromPyObject::extract()`]. #[inline] - pub fn extract<'a, D>(&'a self) -> PyResult + pub fn extract<'py, D>(&'py self) -> PyResult where - D: FromPyObject<'a>, + D: FromPyObject<'py>, { FromPyObject::extract(self) } diff --git a/src/types/bytes.rs b/src/types/bytes.rs index 296925dc..e0631602 100644 --- a/src/types/bytes.rs +++ b/src/types/bytes.rs @@ -203,8 +203,8 @@ impl> Index for Bound<'_, PyBytes> { /// If the source object is a `bytes` object, the `Cow` will be borrowed and /// pointing into the source object, and no copying or heap allocations will happen. /// If it is a `bytearray`, its contents will be copied to an owned `Cow`. -impl<'source> FromPyObject<'source> for Cow<'source, [u8]> { - fn extract(ob: &'source PyAny) -> PyResult { +impl<'py> FromPyObject<'py> for Cow<'py, [u8]> { + fn extract(ob: &'py PyAny) -> PyResult { if let Ok(bytes) = ob.downcast::() { return Ok(Cow::Borrowed(bytes.as_bytes())); } diff --git a/src/types/float.rs b/src/types/float.rs index fb4ca9f4..766c597b 100644 --- a/src/types/float.rs +++ b/src/types/float.rs @@ -95,10 +95,10 @@ impl IntoPy for f64 { } } -impl<'source> FromPyObject<'source> for f64 { +impl<'py> FromPyObject<'py> for f64 { // PyFloat_AsDouble returns -1.0 upon failure #![allow(clippy::float_cmp)] - fn extract_bound(obj: &Bound<'source, PyAny>) -> PyResult { + fn extract_bound(obj: &Bound<'py, PyAny>) -> PyResult { // On non-limited API, .value() uses PyFloat_AS_DOUBLE which // allows us to have an optimized fast path for the case when // we have exactly a `float` object (it's not worth going through @@ -142,8 +142,8 @@ impl IntoPy for f32 { } } -impl<'source> FromPyObject<'source> for f32 { - fn extract_bound(obj: &Bound<'source, PyAny>) -> PyResult { +impl<'py> FromPyObject<'py> for f32 { + fn extract_bound(obj: &Bound<'py, PyAny>) -> PyResult { Ok(obj.extract::()? as f32) } diff --git a/src/types/sequence.rs b/src/types/sequence.rs index 28a6fd48..19f6846b 100644 --- a/src/types/sequence.rs +++ b/src/types/sequence.rs @@ -479,11 +479,11 @@ fn sequence_slice(seq: &PySequence, start: usize, end: usize) -> &PySequence { index_impls!(PySequence, "sequence", sequence_len, sequence_slice); -impl<'a, T> FromPyObject<'a> for Vec +impl<'py, T> FromPyObject<'py> for Vec where - T: FromPyObject<'a>, + T: FromPyObject<'py>, { - fn extract_bound(obj: &Bound<'a, PyAny>) -> PyResult { + fn extract_bound(obj: &Bound<'py, PyAny>) -> PyResult { if obj.is_instance_of::() { return Err(PyTypeError::new_err("Can't extract `str` to `Vec`")); } @@ -496,9 +496,9 @@ where } } -fn extract_sequence<'s, T>(obj: &Bound<'s, PyAny>) -> PyResult> +fn extract_sequence<'py, T>(obj: &Bound<'py, PyAny>) -> PyResult> where - T: FromPyObject<'s>, + T: FromPyObject<'py>, { // Types that pass `PySequence_Check` usually implement enough of the sequence protocol // to support this function and if not, we will only fail extraction safely. diff --git a/src/types/tuple.rs b/src/types/tuple.rs index 26bd698c..a8737c25 100644 --- a/src/types/tuple.rs +++ b/src/types/tuple.rs @@ -666,8 +666,8 @@ fn type_output() -> TypeInfo { } } - impl<'s, $($T: FromPyObject<'s>),+> FromPyObject<'s> for ($($T,)+) { - fn extract_bound(obj: &Bound<'s, PyAny>) -> PyResult + impl<'py, $($T: FromPyObject<'py>),+> FromPyObject<'py> for ($($T,)+) { + fn extract_bound(obj: &Bound<'py, PyAny>) -> PyResult { let t = obj.downcast::()?; if t.len() == $length {