pub trait FromPyObject<'source>: Sized {
    // Required method
    fn extract(ob: &'source PyAny) -> Result<Self, PyErr>;
}
Expand description

Extract a type from a Python object.

Normal usage is through the extract methods on Py and PyAny, which forward to this trait.

Examples

use pyo3::prelude::*;
use pyo3::types::PyString;

Python::with_gil(|py| {
    let obj: Py<PyString> = PyString::new(py, "blah").into();

    // Straight from an owned reference
    let s: &str = obj.extract(py)?;

    // Or from a borrowed reference
    let obj: &PyString = obj.as_ref(py);
    let s: &str = obj.extract()?;
})

Note: depending on the implementation, the lifetime of the extracted result may 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). 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, both the obj and prepared variables must outlive the resulting string slice.

The trait’s conversion method takes a &PyAny argument but is called FromPyObject for historical reasons.

Required Methods§

source

fn extract(ob: &'source PyAny) -> Result<Self, PyErr>

Extracts Self from the source PyObject.

Implementations on Foreign Types§

source§

impl<'s, T0, T1, T2, T3> FromPyObject<'s> for (T0, T1, T2, T3)where T0: FromPyObject<'s>, T1: FromPyObject<'s>, T2: FromPyObject<'s>, T3: FromPyObject<'s>,

source§

impl<'s, T0, T1> FromPyObject<'s> for (T0, T1)where T0: FromPyObject<'s>, T1: FromPyObject<'s>,

source§

impl<'source> FromPyObject<'source> for u32

source§

fn extract(obj: &'source PyAny) -> Result<u32, PyErr>

source§

impl<'source> FromPyObject<'source> for u8

source§

fn extract(obj: &'source PyAny) -> Result<u8, PyErr>

source§

impl<'s, T0, T1, T2, T3, T4, T5, T6> FromPyObject<'s> for (T0, T1, T2, T3, T4, T5, T6)where T0: FromPyObject<'s>, T1: FromPyObject<'s>, T2: FromPyObject<'s>, T3: FromPyObject<'s>, T4: FromPyObject<'s>, T5: FromPyObject<'s>, T6: FromPyObject<'s>,

source§

impl<'source> FromPyObject<'source> for bool

Converts a Python bool to a Rust bool.

Fails with TypeError if the input is not a Python bool.

source§

fn extract(obj: &'source PyAny) -> Result<bool, PyErr>

source§

impl<'source> FromPyObject<'source> for i8

source§

fn extract(obj: &'source PyAny) -> Result<i8, PyErr>

source§

impl<'source> FromPyObject<'source> for u16

source§

fn extract(obj: &'source PyAny) -> Result<u16, PyErr>

source§

impl<'a, T, const N: usize> FromPyObject<'a> for [T; N]where T: FromPyObject<'a>,

source§

impl<'s, T0> FromPyObject<'s> for (T0,)where T0: FromPyObject<'s>,

source§

impl<'source> FromPyObject<'source> for i16

source§

fn extract(obj: &'source PyAny) -> Result<i16, PyErr>

source§

impl FromPyObject<'_> for OsString

source§

impl<'source> FromPyObject<'source> for i64

source§

fn extract(obj: &'source PyAny) -> Result<i64, PyErr>

source§

impl<'source> FromPyObject<'source> for u128

source§

fn extract(ob: &'source PyAny) -> Result<u128, PyErr>

source§

impl<'s, T0, T1, T2> FromPyObject<'s> for (T0, T1, T2)where T0: FromPyObject<'s>, T1: FromPyObject<'s>, T2: FromPyObject<'s>,

source§

impl FromPyObject<'_> for char

source§

impl<'source> FromPyObject<'source> for u64

source§

fn extract(ob: &'source PyAny) -> Result<u64, PyErr>

source§

impl<'s, T0, T1, T2, T3, T4, T5, T6, T7, T8> FromPyObject<'s> for (T0, T1, T2, T3, T4, T5, T6, T7, T8)where T0: FromPyObject<'s>, T1: FromPyObject<'s>, T2: FromPyObject<'s>, T3: FromPyObject<'s>, T4: FromPyObject<'s>, T5: FromPyObject<'s>, T6: FromPyObject<'s>, T7: FromPyObject<'s>, T8: FromPyObject<'s>,

source§

impl<'a> FromPyObject<'a> for &'a [u8]

source§

fn extract(obj: &'a PyAny) -> Result<&'a [u8], PyErr>

source§

impl<'source> FromPyObject<'source> for i32

source§

fn extract(obj: &'source PyAny) -> Result<i32, PyErr>

source§

impl<'source> FromPyObject<'source> for &'source str

Allows extracting strings from Python objects. Accepts Python str and unicode objects.

source§

fn extract(ob: &'source PyAny) -> Result<&'source str, PyErr>

source§

impl<'s, T0, T1, T2, T3, T4, T5, T6, T7> FromPyObject<'s> for (T0, T1, T2, T3, T4, T5, T6, T7)where T0: FromPyObject<'s>, T1: FromPyObject<'s>, T2: FromPyObject<'s>, T3: FromPyObject<'s>, T4: FromPyObject<'s>, T5: FromPyObject<'s>, T6: FromPyObject<'s>, T7: FromPyObject<'s>,

source§

impl<'source> FromPyObject<'source> for f64

source§

fn extract(obj: &'source PyAny) -> Result<f64, PyErr>

source§

impl<'s, T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11> FromPyObject<'s> for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11)where T0: FromPyObject<'s>, T1: FromPyObject<'s>, T2: FromPyObject<'s>, T3: FromPyObject<'s>, T4: FromPyObject<'s>, T5: FromPyObject<'s>, T6: FromPyObject<'s>, T7: FromPyObject<'s>, T8: FromPyObject<'s>, T9: FromPyObject<'s>, T10: FromPyObject<'s>, T11: FromPyObject<'s>,

source§

impl<'source> FromPyObject<'source> for f32

source§

fn extract(obj: &'source PyAny) -> Result<f32, PyErr>

source§

impl<'s, T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> FromPyObject<'s> for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10)where T0: FromPyObject<'s>, T1: FromPyObject<'s>, T2: FromPyObject<'s>, T3: FromPyObject<'s>, T4: FromPyObject<'s>, T5: FromPyObject<'s>, T6: FromPyObject<'s>, T7: FromPyObject<'s>, T8: FromPyObject<'s>, T9: FromPyObject<'s>, T10: FromPyObject<'s>,

source§

impl<'s, T0, T1, T2, T3, T4, T5, T6, T7, T8, T9> FromPyObject<'s> for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9)where T0: FromPyObject<'s>, T1: FromPyObject<'s>, T2: FromPyObject<'s>, T3: FromPyObject<'s>, T4: FromPyObject<'s>, T5: FromPyObject<'s>, T6: FromPyObject<'s>, T7: FromPyObject<'s>, T8: FromPyObject<'s>, T9: FromPyObject<'s>,

source§

impl<'source> FromPyObject<'source> for isize

source§

fn extract(obj: &'source PyAny) -> Result<isize, PyErr>

source§

impl<'s, T0, T1, T2, T3, T4> FromPyObject<'s> for (T0, T1, T2, T3, T4)where T0: FromPyObject<'s>, T1: FromPyObject<'s>, T2: FromPyObject<'s>, T3: FromPyObject<'s>, T4: FromPyObject<'s>,

source§

impl<'a, T> FromPyObject<'a> for Vec<T, Global>where T: FromPyObject<'a>,

source§

fn extract(obj: &'a PyAny) -> Result<Vec<T, Global>, PyErr>

source§

impl<'s, T0, T1, T2, T3, T4, T5> FromPyObject<'s> for (T0, T1, T2, T3, T4, T5)where T0: FromPyObject<'s>, T1: FromPyObject<'s>, T2: FromPyObject<'s>, T3: FromPyObject<'s>, T4: FromPyObject<'s>, T5: FromPyObject<'s>,

source§

impl FromPyObject<'_> for PathBuf

source§

impl<'source> FromPyObject<'source> for usize

source§

fn extract(obj: &'source PyAny) -> Result<usize, PyErr>

source§

impl<'source> FromPyObject<'source> for i128

source§

fn extract(ob: &'source PyAny) -> Result<i128, PyErr>

source§

impl<'source> FromPyObject<'source> for Cow<'source, [u8]>

Special-purpose trait impl to efficiently handle both bytes and bytearray

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.

source§

fn extract(ob: &'source PyAny) -> Result<Cow<'source, [u8]>, PyErr>

source§

impl FromPyObject<'_> for String

Allows extracting strings from Python objects. Accepts Python str and unicode objects.

Implementors§

source§

impl<'a, T> FromPyObject<'a> for &'a PyCell<T>where T: PyClass,

source§

impl<'a, T> FromPyObject<'a> for Option<T>where T: FromPyObject<'a>,

source§

impl<'a, T> FromPyObject<'a> for Cell<T>where T: FromPyObject<'a>,

source§

impl<'a, T> FromPyObject<'a> for Py<T>where T: PyTypeInfo, &'a <T as PyTypeInfo>::AsRefTarget: FromPyObject<'a>, <T as PyTypeInfo>::AsRefTarget: 'a + AsPyPointer,

source§

impl<'a, T> FromPyObject<'a> for PyRef<'a, T>where T: PyClass,

source§

impl<'a, T> FromPyObject<'a> for PyRefMut<'a, T>where T: PyClass<Frozen = False>,

source§

impl<'a, T> FromPyObject<'a> for Twhere T: PyClass + Clone,

source§

impl<'py> FromPyObject<'py> for &'py CancelledError

source§

impl<'py> FromPyObject<'py> for &'py IncompleteReadError

source§

impl<'py> FromPyObject<'py> for &'py InvalidStateError

source§

impl<'py> FromPyObject<'py> for &'py LimitOverrunError

source§

impl<'py> FromPyObject<'py> for &'py QueueEmpty

source§

impl<'py> FromPyObject<'py> for &'py QueueFull

source§

impl<'py> FromPyObject<'py> for &'py TimeoutError

source§

impl<'py> FromPyObject<'py> for &'py gaierror

source§

impl<'py> FromPyObject<'py> for &'py herror

source§

impl<'py> FromPyObject<'py> for &'py timeout

source§

impl<'py> FromPyObject<'py> for &'py PyArithmeticError

source§

impl<'py> FromPyObject<'py> for &'py PyAssertionError

source§

impl<'py> FromPyObject<'py> for &'py PyAttributeError

source§

impl<'py> FromPyObject<'py> for &'py PyBaseException

source§

impl<'py> FromPyObject<'py> for &'py PyBlockingIOError

source§

impl<'py> FromPyObject<'py> for &'py PyBrokenPipeError

source§

impl<'py> FromPyObject<'py> for &'py PyBufferError

source§

impl<'py> FromPyObject<'py> for &'py PyBytesWarning

source§

impl<'py> FromPyObject<'py> for &'py PyChildProcessError

source§

impl<'py> FromPyObject<'py> for &'py PyConnectionAbortedError

source§

impl<'py> FromPyObject<'py> for &'py PyConnectionError

source§

impl<'py> FromPyObject<'py> for &'py PyConnectionRefusedError

source§

impl<'py> FromPyObject<'py> for &'py PyConnectionResetError

source§

impl<'py> FromPyObject<'py> for &'py PyDeprecationWarning

source§

impl<'py> FromPyObject<'py> for &'py PyEOFError

source§

impl<'py> FromPyObject<'py> for &'py PyEncodingWarning

source§

impl<'py> FromPyObject<'py> for &'py PyEnvironmentError

source§

impl<'py> FromPyObject<'py> for &'py PyException

source§

impl<'py> FromPyObject<'py> for &'py PyFileExistsError

source§

impl<'py> FromPyObject<'py> for &'py PyFileNotFoundError

source§

impl<'py> FromPyObject<'py> for &'py PyFloatingPointError

source§

impl<'py> FromPyObject<'py> for &'py PyFutureWarning

source§

impl<'py> FromPyObject<'py> for &'py PyGeneratorExit

source§

impl<'py> FromPyObject<'py> for &'py PyIOError

source§

impl<'py> FromPyObject<'py> for &'py PyImportError

source§

impl<'py> FromPyObject<'py> for &'py PyImportWarning

source§

impl<'py> FromPyObject<'py> for &'py PyIndexError

source§

impl<'py> FromPyObject<'py> for &'py PyInterruptedError

source§

impl<'py> FromPyObject<'py> for &'py PyIsADirectoryError

source§

impl<'py> FromPyObject<'py> for &'py PyKeyError

source§

impl<'py> FromPyObject<'py> for &'py PyKeyboardInterrupt

source§

impl<'py> FromPyObject<'py> for &'py PyLookupError

source§

impl<'py> FromPyObject<'py> for &'py PyMemoryError

source§

impl<'py> FromPyObject<'py> for &'py PyModuleNotFoundError

source§

impl<'py> FromPyObject<'py> for &'py PyNameError

source§

impl<'py> FromPyObject<'py> for &'py PyNotADirectoryError

source§

impl<'py> FromPyObject<'py> for &'py PyNotImplementedError

source§

impl<'py> FromPyObject<'py> for &'py PyOSError

source§

impl<'py> FromPyObject<'py> for &'py PyOverflowError

source§

impl<'py> FromPyObject<'py> for &'py PyPendingDeprecationWarning

source§

impl<'py> FromPyObject<'py> for &'py PyPermissionError

source§

impl<'py> FromPyObject<'py> for &'py PyProcessLookupError

source§

impl<'py> FromPyObject<'py> for &'py PyRecursionError

source§

impl<'py> FromPyObject<'py> for &'py PyReferenceError

source§

impl<'py> FromPyObject<'py> for &'py PyResourceWarning

source§

impl<'py> FromPyObject<'py> for &'py PyRuntimeError

source§

impl<'py> FromPyObject<'py> for &'py PyRuntimeWarning

source§

impl<'py> FromPyObject<'py> for &'py PyStopAsyncIteration

source§

impl<'py> FromPyObject<'py> for &'py PyStopIteration

source§

impl<'py> FromPyObject<'py> for &'py PySyntaxError

source§

impl<'py> FromPyObject<'py> for &'py PySyntaxWarning

source§

impl<'py> FromPyObject<'py> for &'py PySystemError

source§

impl<'py> FromPyObject<'py> for &'py PySystemExit

source§

impl<'py> FromPyObject<'py> for &'py PyTimeoutError

source§

impl<'py> FromPyObject<'py> for &'py PyTypeError

source§

impl<'py> FromPyObject<'py> for &'py PyUnboundLocalError

source§

impl<'py> FromPyObject<'py> for &'py PyUnicodeDecodeError

source§

impl<'py> FromPyObject<'py> for &'py PyUnicodeEncodeError

source§

impl<'py> FromPyObject<'py> for &'py PyUnicodeError

source§

impl<'py> FromPyObject<'py> for &'py PyUnicodeTranslateError

source§

impl<'py> FromPyObject<'py> for &'py PyUnicodeWarning

source§

impl<'py> FromPyObject<'py> for &'py PyUserWarning

source§

impl<'py> FromPyObject<'py> for &'py PyValueError

source§

impl<'py> FromPyObject<'py> for &'py PyWarning

source§

impl<'py> FromPyObject<'py> for &'py PyZeroDivisionError

source§

impl<'py> FromPyObject<'py> for &'py PanicException

source§

impl<'py> FromPyObject<'py> for &'py PyBool

source§

impl<'py> FromPyObject<'py> for &'py PyByteArray

source§

impl<'py> FromPyObject<'py> for &'py PyBytes

source§

impl<'py> FromPyObject<'py> for &'py PyCapsule

source§

impl<'py> FromPyObject<'py> for &'py PyCode

source§

impl<'py> FromPyObject<'py> for &'py PyComplex

source§

impl<'py> FromPyObject<'py> for &'py PyDate

source§

impl<'py> FromPyObject<'py> for &'py PyDateTime

source§

impl<'py> FromPyObject<'py> for &'py PyDelta

source§

impl<'py> FromPyObject<'py> for &'py PyTime

source§

impl<'py> FromPyObject<'py> for &'py PyTzInfo

source§

impl<'py> FromPyObject<'py> for &'py PyDict

source§

impl<'py> FromPyObject<'py> for &'py PyDictItems

source§

impl<'py> FromPyObject<'py> for &'py PyDictKeys

source§

impl<'py> FromPyObject<'py> for &'py PyDictValues

source§

impl<'py> FromPyObject<'py> for &'py PyFloat

source§

impl<'py> FromPyObject<'py> for &'py PyFrame

source§

impl<'py> FromPyObject<'py> for &'py PyFrozenSet

source§

impl<'py> FromPyObject<'py> for &'py PyCFunction

source§

impl<'py> FromPyObject<'py> for &'py PyFunction

source§

impl<'py> FromPyObject<'py> for &'py PyIterator

source§

impl<'py> FromPyObject<'py> for &'py PyList

source§

impl<'py> FromPyObject<'py> for &'py PyMapping

source§

impl<'py> FromPyObject<'py> for &'py PyLong

source§

impl<'py> FromPyObject<'py> for &'py PySuper

source§

impl<'py> FromPyObject<'py> for &'py PySequence

source§

impl<'py> FromPyObject<'py> for &'py PySet

source§

impl<'py> FromPyObject<'py> for &'py PySlice

source§

impl<'py> FromPyObject<'py> for &'py PyString

source§

impl<'py> FromPyObject<'py> for &'py PyTraceback

source§

impl<'py> FromPyObject<'py> for &'py PyTuple

source§

impl<'py> FromPyObject<'py> for &'py PyType

source§

impl<'py> FromPyObject<'py> for &'py PyAny

source§

impl<'py> FromPyObject<'py> for &'py PyModule

source§

impl<'source> FromPyObject<'source> for NonZeroI8

source§

impl<'source> FromPyObject<'source> for NonZeroI16

source§

impl<'source> FromPyObject<'source> for NonZeroI32

source§

impl<'source> FromPyObject<'source> for NonZeroI64

source§

impl<'source> FromPyObject<'source> for NonZeroI128

source§

impl<'source> FromPyObject<'source> for NonZeroIsize

source§

impl<'source> FromPyObject<'source> for NonZeroU8

source§

impl<'source> FromPyObject<'source> for NonZeroU16

source§

impl<'source> FromPyObject<'source> for NonZeroU32

source§

impl<'source> FromPyObject<'source> for NonZeroU64

source§

impl<'source> FromPyObject<'source> for NonZeroU128

source§

impl<'source> FromPyObject<'source> for NonZeroUsize

source§

impl<'source, K> FromPyObject<'source> for BTreeSet<K, Global>where K: FromPyObject<'source> + Ord,

source§

impl<'source, K, S> FromPyObject<'source> for HashSet<K, S>where K: FromPyObject<'source> + Eq + Hash, S: BuildHasher + Default,

source§

impl<'source, K, V> FromPyObject<'source> for BTreeMap<K, V, Global>where K: FromPyObject<'source> + Ord, V: FromPyObject<'source>,

source§

impl<'source, K, V, S> FromPyObject<'source> for HashMap<K, V, S>where K: FromPyObject<'source> + Eq + Hash, V: FromPyObject<'source>, S: BuildHasher + Default,

source§

impl<'source, T> FromPyObject<'source> for PyBuffer<T>where T: Element,