MCPcopy Create free account
hub / github.com/PyO3/pyo3 / IntoPyObjectExt

Interface IntoPyObjectExt

src/conversion.rs:222–254  ·  view source on GitHub ↗

Convenience methods for common usages of [`IntoPyObject`]. Every type that implements [`IntoPyObject`] also implements this trait. These methods: - Drop type information from the output, returning a `PyAny` object. - Always convert the `Error` type to `PyErr`, which may incur a performance penalty but it more convenient in contexts where the `?` operator would produce a `PyErr` anyway.

Source from the content-addressed store, hash-verified

220/// - Always convert the `Error` type to `PyErr`, which may incur a performance penalty but it
221/// more convenient in contexts where the `?` operator would produce a `PyErr` anyway.
222pub trait IntoPyObjectExt<'py>: IntoPyObject<'py> + into_pyobject_ext::Sealed {
223 /// Converts `self` into an owned Python object, dropping type information.
224 #[inline]
225 fn into_bound_py_any(self, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> {
226 match self.into_pyobject(py) {
227 Ok(obj) => Ok(obj.into_any().into_bound()),
228 Err(err) => Err(err.into()),
229 }
230 }
231
232 /// Converts `self` into an owned Python object, dropping type information and unbinding it
233 /// from the `'py` lifetime.
234 #[inline]
235 fn into_py_any(self, py: Python<'py>) -> PyResult<Py<PyAny>> {
236 match self.into_pyobject(py) {
237 Ok(obj) => Ok(obj.into_any().unbind()),
238 Err(err) => Err(err.into()),
239 }
240 }
241
242 /// Converts `self` into a Python object.
243 ///
244 /// This is equivalent to calling [`into_pyobject`][IntoPyObject::into_pyobject] followed
245 /// with `.map_err(Into::into)` to convert the error type to [`PyErr`]. This is helpful
246 /// for generic code which wants to make use of the `?` operator.
247 #[inline]
248 fn into_pyobject_or_pyerr(self, py: Python<'py>) -> PyResult<Self::Output> {
249 match self.into_pyobject(py) {
250 Ok(obj) => Ok(obj),
251 Err(err) => Err(err.into()),
252 }
253 }
254}
255
256impl<'py, T> IntoPyObjectExt<'py> for T where T: IntoPyObject<'py> {}
257

Callers 23

newMethod · 0.80
newMethod · 0.80
set_itemMethod · 0.80
initMethod · 0.80
into_pyobjectMethod · 0.80
into_pyobjectMethod · 0.80
test_roundtripFunction · 0.80
bench_pyclassFunction · 0.80
set_newFunction · 0.80
test_py_is_truthyFunction · 0.80
argumentsMethod · 0.80

Implementers 1

conversion.rssrc/conversion.rs

Calls

no outgoing calls

Tested by

no test coverage detected