diff --git a/src/instance.rs b/src/instance.rs index e23054e6..8406e86a 100644 --- a/src/instance.rs +++ b/src/instance.rs @@ -832,17 +832,18 @@ where impl Py { /// Attaches this `Py` to the given Python context, allowing access to further Python APIs. - pub(crate) fn attach<'py>(&self, _py: Python<'py>) -> &Bound<'py, T> { + pub fn bind<'py>(&self, _py: Python<'py>) -> &Bound<'py, T> { // Safety: `Bound` has the same layout as `Py` unsafe { &*(self as *const Py).cast() } } - /// Same as `attach` but takes ownership of `self`. - pub(crate) fn attach_into(self, py: Python<'_>) -> Bound<'_, T> { + /// Same as `bind` but takes ownership of `self`. + pub fn into_bound(self, py: Python<'_>) -> Bound<'_, T> { Bound(py, ManuallyDrop::new(self)) } - pub(crate) fn attach_borrow<'a, 'py>(&'a self, py: Python<'py>) -> Borrowed<'a, 'py, T> { + /// Same as `bind` but produces a `Borrowed` instead of a `Bound`. + pub fn bind_borrowed<'a, 'py>(&'a self, py: Python<'py>) -> Borrowed<'a, 'py, T> { Borrowed(self.0, PhantomData, py) } @@ -957,7 +958,7 @@ impl Py { where N: IntoPy>, { - self.attach(py).as_any().getattr(attr_name).map(Into::into) + self.bind(py).as_any().getattr(attr_name).map(Into::into) } /// Sets an attribute value. @@ -987,9 +988,9 @@ impl Py { N: IntoPy>, V: IntoPy>, { - self.attach(py) + self.bind(py) .as_any() - .setattr(attr_name, value.into_py(py).attach_into(py)) + .setattr(attr_name, value.into_py(py).into_bound(py)) } /// Calls the object. @@ -1001,21 +1002,21 @@ impl Py { args: impl IntoPy>, kwargs: Option<&PyDict>, ) -> PyResult { - self.attach(py).as_any().call(args, kwargs).map(Into::into) + self.bind(py).as_any().call(args, kwargs).map(Into::into) } /// Calls the object with only positional arguments. /// /// This is equivalent to the Python expression `self(*args)`. pub fn call1(&self, py: Python<'_>, args: impl IntoPy>) -> PyResult { - self.attach(py).as_any().call1(args).map(Into::into) + self.bind(py).as_any().call1(args).map(Into::into) } /// Calls the object without arguments. /// /// This is equivalent to the Python expression `self()`. pub fn call0(&self, py: Python<'_>) -> PyResult { - self.attach(py).as_any().call0().map(Into::into) + self.bind(py).as_any().call0().map(Into::into) } /// Calls a method on the object. @@ -1035,7 +1036,7 @@ impl Py { N: IntoPy>, A: IntoPy>, { - self.attach(py) + self.bind(py) .as_any() .call_method(name, args, kwargs) .map(Into::into) @@ -1052,7 +1053,7 @@ impl Py { N: IntoPy>, A: IntoPy>, { - self.attach(py) + self.bind(py) .as_any() .call_method1(name, args) .map(Into::into) @@ -1068,7 +1069,7 @@ impl Py { where N: IntoPy>, { - self.attach(py).as_any().call_method0(name).map(Into::into) + self.bind(py).as_any().call_method0(name).map(Into::into) } /// Create a `Py` instance by taking ownership of the given FFI pointer. @@ -1624,7 +1625,7 @@ a = A() #[test] fn test_debug_fmt() { Python::with_gil(|py| { - let obj = "hello world".to_object(py).attach_into(py); + let obj = "hello world".to_object(py).into_bound(py); assert_eq!(format!("{:?}", obj), "'hello world'"); }); } @@ -1632,7 +1633,7 @@ a = A() #[test] fn test_display_fmt() { Python::with_gil(|py| { - let obj = "hello world".to_object(py).attach_into(py); + let obj = "hello world".to_object(py).into_bound(py); assert_eq!(format!("{}", obj), "hello world"); }); } diff --git a/src/types/any.rs b/src/types/any.rs index 4d82fad6..29babac8 100644 --- a/src/types/any.rs +++ b/src/types/any.rs @@ -1728,7 +1728,7 @@ impl<'py> PyAnyMethods<'py> for Bound<'py, PyAny> { } let py = self.py(); - inner(self, attr_name.into_py(self.py()).attach_into(py)) + inner(self, attr_name.into_py(self.py()).into_bound(py)) } fn setattr(&self, attr_name: N, value: V) -> PyResult<()> @@ -1749,8 +1749,8 @@ impl<'py> PyAnyMethods<'py> for Bound<'py, PyAny> { let py = self.py(); inner( self, - attr_name.into_py(py).attach_into(py), - value.to_object(py).attach_into(py), + attr_name.into_py(py).into_bound(py), + value.to_object(py).into_bound(py), ) } @@ -1765,7 +1765,7 @@ impl<'py> PyAnyMethods<'py> for Bound<'py, PyAny> { } let py = self.py(); - inner(self, attr_name.into_py(py).attach_into(py)) + inner(self, attr_name.into_py(py).into_bound(py)) } fn compare(&self, other: O) -> PyResult @@ -1795,7 +1795,7 @@ impl<'py> PyAnyMethods<'py> for Bound<'py, PyAny> { } let py = self.py(); - inner(self, other.to_object(py).attach_into(py)) + inner(self, other.to_object(py).into_bound(py)) } fn rich_compare(&self, other: O, compare_op: CompareOp) -> PyResult> @@ -1814,7 +1814,7 @@ impl<'py> PyAnyMethods<'py> for Bound<'py, PyAny> { } let py = self.py(); - inner(self, other.to_object(py).attach_into(py), compare_op) + inner(self, other.to_object(py).into_bound(py), compare_op) } fn lt(&self, other: O) -> PyResult @@ -1890,7 +1890,7 @@ impl<'py> PyAnyMethods<'py> for Bound<'py, PyAny> { } let py = self.py(); - inner(self, args.into_py(py).attach_into(py), kwargs) + inner(self, args.into_py(py).into_bound(py), kwargs) } fn call0(&self) -> PyResult> { @@ -1937,7 +1937,7 @@ impl<'py> PyAnyMethods<'py> for Bound<'py, PyAny> { // Optimized path on python 3.9+ unsafe { - let name = name.into_py(py).attach_into(py); + let name = name.into_py(py).into_bound(py); ffi::PyObject_CallMethodNoArgs(self.as_ptr(), name.as_ptr()).assume_owned_or_err(py) } } else { @@ -1987,7 +1987,7 @@ impl<'py> PyAnyMethods<'py> for Bound<'py, PyAny> { } let py = self.py(); - inner(self, key.to_object(py).attach_into(py)) + inner(self, key.to_object(py).into_bound(py)) } fn set_item(&self, key: K, value: V) -> PyResult<()> @@ -2008,8 +2008,8 @@ impl<'py> PyAnyMethods<'py> for Bound<'py, PyAny> { let py = self.py(); inner( self, - key.to_object(py).attach_into(py), - value.to_object(py).attach_into(py), + key.to_object(py).into_bound(py), + value.to_object(py).into_bound(py), ) } @@ -2024,7 +2024,7 @@ impl<'py> PyAnyMethods<'py> for Bound<'py, PyAny> { } let py = self.py(); - inner(self, key.to_object(py).attach_into(py)) + inner(self, key.to_object(py).into_bound(py)) } fn iter(&self) -> PyResult> { @@ -2184,7 +2184,7 @@ impl<'py> PyAnyMethods<'py> for Bound<'py, PyAny> { } let py = self.py(); - inner(self, value.to_object(py).attach_into(py)) + inner(self, value.to_object(py).into_bound(py)) } #[cfg(not(PyPy))] diff --git a/src/types/bytes.rs b/src/types/bytes.rs index 3fd7e383..4db098c6 100644 --- a/src/types/bytes.rs +++ b/src/types/bytes.rs @@ -129,7 +129,7 @@ impl Py { /// immutable, the result may be used for as long as the reference to /// `self` is held, including when the GIL is released. pub fn as_bytes<'a>(&'a self, py: Python<'_>) -> &'a [u8] { - self.attach_borrow(py).as_bytes() + self.bind_borrowed(py).as_bytes() } } diff --git a/src/types/dict.rs b/src/types/dict.rs index 61fc21f8..24e0832b 100644 --- a/src/types/dict.rs +++ b/src/types/dict.rs @@ -391,7 +391,7 @@ impl<'py> PyDictMethods<'py> for Bound<'py, PyDict> { } let py = self.py(); - inner(self, key.to_object(py).attach_into(py)) + inner(self, key.to_object(py).into_bound(py)) } fn get_item(&self, key: K) -> PyResult>> @@ -414,7 +414,7 @@ impl<'py> PyDictMethods<'py> for Bound<'py, PyDict> { } let py = self.py(); - inner(self, key.to_object(py).attach_into(py)) + inner(self, key.to_object(py).into_bound(py)) } fn set_item(&self, key: K, value: V) -> PyResult<()> @@ -435,8 +435,8 @@ impl<'py> PyDictMethods<'py> for Bound<'py, PyDict> { let py = self.py(); inner( self, - key.to_object(py).attach_into(py), - value.to_object(py).attach_into(py), + key.to_object(py).into_bound(py), + value.to_object(py).into_bound(py), ) } @@ -451,7 +451,7 @@ impl<'py> PyDictMethods<'py> for Bound<'py, PyDict> { } let py = self.py(); - inner(self, key.to_object(py).attach_into(py)) + inner(self, key.to_object(py).into_bound(py)) } fn keys(&self) -> Bound<'py, PyList> { diff --git a/src/types/list.rs b/src/types/list.rs index c042feac..a3b19d35 100644 --- a/src/types/list.rs +++ b/src/types/list.rs @@ -437,7 +437,7 @@ impl<'py> PyListMethods<'py> for Bound<'py, PyList> { } let py = self.py(); - inner(self, index, item.to_object(py).attach_into(py)) + inner(self, index, item.to_object(py).into_bound(py)) } /// Deletes the `index`th element of self. @@ -483,7 +483,7 @@ impl<'py> PyListMethods<'py> for Bound<'py, PyList> { } let py = self.py(); - inner(self, item.to_object(py).attach_into(py)) + inner(self, item.to_object(py).into_bound(py)) } /// Inserts an item at the specified index. @@ -500,7 +500,7 @@ impl<'py> PyListMethods<'py> for Bound<'py, PyList> { } let py = self.py(); - inner(self, index, item.to_object(py).attach_into(py)) + inner(self, index, item.to_object(py).into_bound(py)) } /// Determines if self contains `value`. diff --git a/src/types/sequence.rs b/src/types/sequence.rs index 0042780c..714288bd 100644 --- a/src/types/sequence.rs +++ b/src/types/sequence.rs @@ -375,7 +375,7 @@ impl<'py> PySequenceMethods<'py> for Bound<'py, PySequence> { } let py = self.py(); - inner(self, i, item.to_object(py).attach_into(py)) + inner(self, i, item.to_object(py).into_bound(py)) } #[inline] @@ -417,7 +417,7 @@ impl<'py> PySequenceMethods<'py> for Bound<'py, PySequence> { } let py = self.py(); - inner(self, value.to_object(py).attach_into(py)) + inner(self, value.to_object(py).into_bound(py)) } #[inline] @@ -435,7 +435,7 @@ impl<'py> PySequenceMethods<'py> for Bound<'py, PySequence> { } let py = self.py(); - inner(self, value.to_object(py).attach_into(py)) + inner(self, value.to_object(py).into_bound(py)) } #[inline] @@ -450,7 +450,7 @@ impl<'py> PySequenceMethods<'py> for Bound<'py, PySequence> { } let py = self.py(); - inner(self, value.to_object(self.py()).attach_into(py)) + inner(self, value.to_object(self.py()).into_bound(py)) } #[inline] diff --git a/src/types/string.rs b/src/types/string.rs index cc05293a..03d41963 100644 --- a/src/types/string.rs +++ b/src/types/string.rs @@ -407,7 +407,7 @@ impl Py { /// the GIL lifetime. #[cfg(any(Py_3_10, not(Py_LIMITED_API)))] pub fn to_str<'a>(&'a self, py: Python<'_>) -> PyResult<&'a str> { - self.attach_borrow(py).to_str() + self.bind_borrowed(py).to_str() } /// Converts the `PyString` into a Rust string, avoiding copying when possible. @@ -418,7 +418,7 @@ impl Py { /// Because `str` objects are immutable, the returned slice is independent of /// the GIL lifetime. pub fn to_cow<'a>(&'a self, py: Python<'_>) -> PyResult> { - self.attach_borrow(py).to_cow() + self.bind_borrowed(py).to_cow() } /// Converts the `PyString` into a Rust string. @@ -429,7 +429,7 @@ impl Py { /// Because `str` objects are immutable, the returned slice is independent of /// the GIL lifetime. pub fn to_string_lossy<'a>(&'a self, py: Python<'_>) -> Cow<'a, str> { - self.attach_borrow(py).to_string_lossy() + self.bind_borrowed(py).to_string_lossy() } }