Trait cpython::FromPyObject

source ·
pub trait FromPyObject<'s>: Sized {
    // Required method
    fn extract(py: Python<'_>, obj: &'s PyObject) -> PyResult<Self>;
}
Expand description

FromPyObject is implemented by various types that can be extracted from a Python object.

Normal usage is through the PyObject::extract helper method:

let value = obj.extract::<TargetType>(py)?;

Each target type for this conversion supports a different Python objects as input. Calls with an unsupported input object will result in an exception (usually a TypeError).

This trait is also used by the py_fn! and py_class! and py_argparse! macros in order to translate from Python objects to the expected Rust parameter types. For example, the parameter x in def method(self, x: i32) will use impl FromPyObject for i32 to convert the input Python object into a Rust i32. When these macros are used with reference parameters (x: &str), the trait RefFromPyObject is used instead.

Required Methods§

source

fn extract(py: Python<'_>, obj: &'s PyObject) -> PyResult<Self>

Extracts Self from the source PyObject.

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl<'s> FromPyObject<'s> for Cow<'s, str>

Allows extracting strings from Python objects. Accepts Python str and unicode objects. In Python 2.7, str is expected to be UTF-8 encoded.

Returns a UnicodeDecodeError if the input is not valid unicode (containing unpaired surrogates, or a Python 2.7 byte string that is not valid UTF-8).

source§

fn extract(py: Python<'_>, obj: &'s PyObject) -> PyResult<Self>

source§

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

For Python bytes, returns a reference to the existing immutable string data. If the Python object is a single-dimensional buffer of format c or B (C: char or unsigned char), returns an owned copy of the data in the buffer. Otherwise, uses the sequence protocol and converts each individual element via impl FromPyObject for u8.

source§

fn extract(py: Python<'_>, obj: &'s PyObject) -> PyResult<Self>

source§

impl<'s> FromPyObject<'s> for bool

source§

fn extract(py: Python<'_>, obj: &'s PyObject) -> PyResult<Self>

Converts a Python bool to a rust bool.

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

source§

impl<'s> FromPyObject<'s> for f32

source§

fn extract(py: Python<'_>, obj: &'s PyObject) -> PyResult<Self>

Converts Python float to Rust f32.

This conversion loses precision as the 64-bit float from Python gets converted to a 32-bit float. Out-of-range numbers may also overflow to infinity.

source§

impl<'s> FromPyObject<'s> for f64

source§

fn extract(py: Python<'_>, obj: &'s PyObject) -> PyResult<Self>

Converts Python float to Rust f64.

source§

impl<'s> FromPyObject<'s> for i8

source§

fn extract(py: Python<'_>, obj: &'s PyObject) -> PyResult<Self>

Converts Python integers to Rust integers.

Returns OverflowError if the input integer does not fit the Rust type; or TypeError if the input is not an integer.

source§

impl<'s> FromPyObject<'s> for i16

source§

fn extract(py: Python<'_>, obj: &'s PyObject) -> PyResult<Self>

Converts Python integers to Rust integers.

Returns OverflowError if the input integer does not fit the Rust type; or TypeError if the input is not an integer.

source§

impl<'s> FromPyObject<'s> for i32

source§

fn extract(py: Python<'_>, obj: &'s PyObject) -> PyResult<Self>

Converts Python integers to Rust integers.

Returns OverflowError if the input integer does not fit the Rust type; or TypeError if the input is not an integer.

source§

impl<'s> FromPyObject<'s> for i64

Converts Python integers to Rust integers.

Returns OverflowError if the input integer does not fit the Rust type; or TypeError if the input is not an integer.

source§

fn extract(py: Python<'_>, obj: &'s PyObject) -> PyResult<i64>

source§

impl<'s> FromPyObject<'s> for isize

source§

fn extract(py: Python<'_>, obj: &'s PyObject) -> PyResult<Self>

Converts Python integers to Rust integers.

Returns OverflowError if the input integer does not fit the Rust type; or TypeError if the input is not an integer.

source§

impl<'s> FromPyObject<'s> for u8

source§

fn extract(py: Python<'_>, obj: &'s PyObject) -> PyResult<Self>

Converts Python integers to Rust integers.

Returns OverflowError if the input integer does not fit the Rust type; or TypeError if the input is not an integer.

source§

impl<'s> FromPyObject<'s> for u16

source§

fn extract(py: Python<'_>, obj: &'s PyObject) -> PyResult<Self>

Converts Python integers to Rust integers.

Returns OverflowError if the input integer does not fit the Rust type; or TypeError if the input is not an integer.

source§

impl<'s> FromPyObject<'s> for u32

source§

fn extract(py: Python<'_>, obj: &'s PyObject) -> PyResult<Self>

Converts Python integers to Rust integers.

Returns OverflowError if the input integer does not fit the Rust type; or TypeError if the input is not an integer.

source§

impl<'s> FromPyObject<'s> for u64

Converts Python integers to Rust integers.

Returns OverflowError if the input integer does not fit the Rust type; or TypeError if the input is not an integer.

source§

fn extract(py: Python<'_>, obj: &'s PyObject) -> PyResult<u64>

source§

impl<'s> FromPyObject<'s> for usize

source§

fn extract(py: Python<'_>, obj: &'s PyObject) -> PyResult<Self>

Converts Python integers to Rust integers.

Returns OverflowError if the input integer does not fit the Rust type; or TypeError if the input is not an integer.

source§

impl<'s> FromPyObject<'s> for String

Allows extracting strings from Python objects. Accepts Python str and unicode objects. In Python 2.7, str is expected to be UTF-8 encoded.

Returns a UnicodeDecodeError if the input is not valid unicode (containing unpaired surrogates, or a Python 2.7 byte string that is not valid UTF-8).

source§

fn extract(py: Python<'_>, obj: &'s PyObject) -> PyResult<Self>

source§

impl<'s, A: FromPyObject<'s>> FromPyObject<'s> for (A,)

Converts a Python tuple to a Rust tuple.

Note: only accepts Python tuple (or derived classes); other types are not accepted.

source§

fn extract(py: Python<'_>, obj: &'s PyObject) -> PyResult<Self>

source§

impl<'s, A: FromPyObject<'s>, B: FromPyObject<'s>> FromPyObject<'s> for (A, B)

Converts a Python tuple to a Rust tuple.

Note: only accepts Python tuple (or derived classes); other types are not accepted.

source§

fn extract(py: Python<'_>, obj: &'s PyObject) -> PyResult<Self>

source§

impl<'s, A: FromPyObject<'s>, B: FromPyObject<'s>, C: FromPyObject<'s>> FromPyObject<'s> for (A, B, C)

Converts a Python tuple to a Rust tuple.

Note: only accepts Python tuple (or derived classes); other types are not accepted.

source§

fn extract(py: Python<'_>, obj: &'s PyObject) -> PyResult<Self>

source§

impl<'s, A: FromPyObject<'s>, B: FromPyObject<'s>, C: FromPyObject<'s>, D: FromPyObject<'s>> FromPyObject<'s> for (A, B, C, D)

Converts a Python tuple to a Rust tuple.

Note: only accepts Python tuple (or derived classes); other types are not accepted.

source§

fn extract(py: Python<'_>, obj: &'s PyObject) -> PyResult<Self>

source§

impl<'s, A: FromPyObject<'s>, B: FromPyObject<'s>, C: FromPyObject<'s>, D: FromPyObject<'s>, E: FromPyObject<'s>> FromPyObject<'s> for (A, B, C, D, E)

Converts a Python tuple to a Rust tuple.

Note: only accepts Python tuple (or derived classes); other types are not accepted.

source§

fn extract(py: Python<'_>, obj: &'s PyObject) -> PyResult<Self>

source§

impl<'s, A: FromPyObject<'s>, B: FromPyObject<'s>, C: FromPyObject<'s>, D: FromPyObject<'s>, E: FromPyObject<'s>, F: FromPyObject<'s>> FromPyObject<'s> for (A, B, C, D, E, F)

Converts a Python tuple to a Rust tuple.

Note: only accepts Python tuple (or derived classes); other types are not accepted.

source§

fn extract(py: Python<'_>, obj: &'s PyObject) -> PyResult<Self>

source§

impl<'s, A: FromPyObject<'s>, B: FromPyObject<'s>, C: FromPyObject<'s>, D: FromPyObject<'s>, E: FromPyObject<'s>, F: FromPyObject<'s>, G: FromPyObject<'s>> FromPyObject<'s> for (A, B, C, D, E, F, G)

Converts a Python tuple to a Rust tuple.

Note: only accepts Python tuple (or derived classes); other types are not accepted.

source§

fn extract(py: Python<'_>, obj: &'s PyObject) -> PyResult<Self>

source§

impl<'s, A: FromPyObject<'s>, B: FromPyObject<'s>, C: FromPyObject<'s>, D: FromPyObject<'s>, E: FromPyObject<'s>, F: FromPyObject<'s>, G: FromPyObject<'s>, H: FromPyObject<'s>> FromPyObject<'s> for (A, B, C, D, E, F, G, H)

Converts a Python tuple to a Rust tuple.

Note: only accepts Python tuple (or derived classes); other types are not accepted.

source§

fn extract(py: Python<'_>, obj: &'s PyObject) -> PyResult<Self>

source§

impl<'s, A: FromPyObject<'s>, B: FromPyObject<'s>, C: FromPyObject<'s>, D: FromPyObject<'s>, E: FromPyObject<'s>, F: FromPyObject<'s>, G: FromPyObject<'s>, H: FromPyObject<'s>, I: FromPyObject<'s>> FromPyObject<'s> for (A, B, C, D, E, F, G, H, I)

Converts a Python tuple to a Rust tuple.

Note: only accepts Python tuple (or derived classes); other types are not accepted.

source§

fn extract(py: Python<'_>, obj: &'s PyObject) -> PyResult<Self>

source§

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

If the python value is None, returns Option::None. Otherwise, converts the python value to T and returns Some(T).

source§

fn extract(py: Python<'_>, obj: &'s PyObject) -> PyResult<Self>

source§

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

Uses the sequence protocol and converts each individual element via impl FromPyObject for T.

Note: when using --features nightly, a specialization of this impl may use the buffer protocol to perform a more efficient bulk copy.

source§

fn extract(py: Python<'_>, obj: &'s PyObject) -> PyResult<Self>

Implementors§

source§

impl FromPyObject<'_> for PyNone

source§

impl<'s> FromPyObject<'s> for &'s AssertionError

source§

impl<'s> FromPyObject<'s> for &'s AttributeError

source§

impl<'s> FromPyObject<'s> for &'s BaseException

source§

impl<'s> FromPyObject<'s> for &'s BlockingIOError

source§

impl<'s> FromPyObject<'s> for &'s BrokenPipeError

source§

impl<'s> FromPyObject<'s> for &'s BufferError

source§

impl<'s> FromPyObject<'s> for &'s ChildProcessError

source§

impl<'s> FromPyObject<'s> for &'s ConnectionAbortedError

source§

impl<'s> FromPyObject<'s> for &'s ConnectionError

source§

impl<'s> FromPyObject<'s> for &'s ConnectionRefusedError

source§

impl<'s> FromPyObject<'s> for &'s ConnectionResetError

source§

impl<'s> FromPyObject<'s> for &'s EOFError

source§

impl<'s> FromPyObject<'s> for &'s EnvironmentError

source§

impl<'s> FromPyObject<'s> for &'s Exception

source§

impl<'s> FromPyObject<'s> for &'s FileExistsError

source§

impl<'s> FromPyObject<'s> for &'s FileNotFoundError

source§

impl<'s> FromPyObject<'s> for &'s FloatingPointError

source§

impl<'s> FromPyObject<'s> for &'s IOError

source§

impl<'s> FromPyObject<'s> for &'s ImportError

source§

impl<'s> FromPyObject<'s> for &'s IndexError

source§

impl<'s> FromPyObject<'s> for &'s InterruptedError

source§

impl<'s> FromPyObject<'s> for &'s IsADirectoryError

source§

impl<'s> FromPyObject<'s> for &'s KeyError

source§

impl<'s> FromPyObject<'s> for &'s KeyboardInterrupt

source§

impl<'s> FromPyObject<'s> for &'s LookupError

source§

impl<'s> FromPyObject<'s> for &'s MemoryError

source§

impl<'s> FromPyObject<'s> for &'s NameError

source§

impl<'s> FromPyObject<'s> for &'s NotADirectoryError

source§

impl<'s> FromPyObject<'s> for &'s NotImplementedError

source§

impl<'s> FromPyObject<'s> for &'s OSError

source§

impl<'s> FromPyObject<'s> for &'s OverflowError

source§

impl<'s> FromPyObject<'s> for &'s PermissionError

source§

impl<'s> FromPyObject<'s> for &'s ProcessLookupError

source§

impl<'s> FromPyObject<'s> for &'s ReferenceError

source§

impl<'s> FromPyObject<'s> for &'s RuntimeError

source§

impl<'s> FromPyObject<'s> for &'s SyntaxError

source§

impl<'s> FromPyObject<'s> for &'s SystemError

source§

impl<'s> FromPyObject<'s> for &'s SystemExit

source§

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

source§

impl<'s> FromPyObject<'s> for &'s TypeError

source§

impl<'s> FromPyObject<'s> for &'s UnicodeDecodeError

source§

impl<'s> FromPyObject<'s> for &'s UnicodeEncodeError

source§

impl<'s> FromPyObject<'s> for &'s UnicodeTranslateError

source§

impl<'s> FromPyObject<'s> for &'s ValueError

source§

impl<'s> FromPyObject<'s> for &'s ZeroDivisionError

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

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

source§

impl<'s> FromPyObject<'s> for &'s PyObject

source§

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

source§

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

source§

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

source§

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

source§

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

source§

impl<'s> FromPyObject<'s> for AssertionError

source§

impl<'s> FromPyObject<'s> for AttributeError

source§

impl<'s> FromPyObject<'s> for BaseException

source§

impl<'s> FromPyObject<'s> for BlockingIOError

source§

impl<'s> FromPyObject<'s> for BrokenPipeError

source§

impl<'s> FromPyObject<'s> for BufferError

source§

impl<'s> FromPyObject<'s> for ChildProcessError

source§

impl<'s> FromPyObject<'s> for ConnectionAbortedError

source§

impl<'s> FromPyObject<'s> for ConnectionError

source§

impl<'s> FromPyObject<'s> for ConnectionRefusedError

source§

impl<'s> FromPyObject<'s> for ConnectionResetError

source§

impl<'s> FromPyObject<'s> for EOFError

source§

impl<'s> FromPyObject<'s> for EnvironmentError

source§

impl<'s> FromPyObject<'s> for Exception

source§

impl<'s> FromPyObject<'s> for FileExistsError

source§

impl<'s> FromPyObject<'s> for FileNotFoundError

source§

impl<'s> FromPyObject<'s> for FloatingPointError

source§

impl<'s> FromPyObject<'s> for IOError

source§

impl<'s> FromPyObject<'s> for ImportError

source§

impl<'s> FromPyObject<'s> for IndexError

source§

impl<'s> FromPyObject<'s> for InterruptedError

source§

impl<'s> FromPyObject<'s> for IsADirectoryError

source§

impl<'s> FromPyObject<'s> for KeyError

source§

impl<'s> FromPyObject<'s> for KeyboardInterrupt

source§

impl<'s> FromPyObject<'s> for LookupError

source§

impl<'s> FromPyObject<'s> for MemoryError

source§

impl<'s> FromPyObject<'s> for NameError

source§

impl<'s> FromPyObject<'s> for NotADirectoryError

source§

impl<'s> FromPyObject<'s> for NotImplementedError

source§

impl<'s> FromPyObject<'s> for OSError

source§

impl<'s> FromPyObject<'s> for OverflowError

source§

impl<'s> FromPyObject<'s> for PermissionError

source§

impl<'s> FromPyObject<'s> for ProcessLookupError

source§

impl<'s> FromPyObject<'s> for ReferenceError

source§

impl<'s> FromPyObject<'s> for RuntimeError

source§

impl<'s> FromPyObject<'s> for SyntaxError

source§

impl<'s> FromPyObject<'s> for SystemError

source§

impl<'s> FromPyObject<'s> for SystemExit

source§

impl<'s> FromPyObject<'s> for TimeoutError

source§

impl<'s> FromPyObject<'s> for TypeError

source§

impl<'s> FromPyObject<'s> for UnicodeDecodeError

source§

impl<'s> FromPyObject<'s> for UnicodeEncodeError

source§

impl<'s> FromPyObject<'s> for UnicodeTranslateError

source§

impl<'s> FromPyObject<'s> for ValueError

source§

impl<'s> FromPyObject<'s> for ZeroDivisionError

source§

impl<'s> FromPyObject<'s> for NoArgs

source§

impl<'s> FromPyObject<'s> for PyBool

source§

impl<'s> FromPyObject<'s> for PyBytes

source§

impl<'s> FromPyObject<'s> for PyCapsule

source§

impl<'s> FromPyObject<'s> for PyDict

source§

impl<'s> FromPyObject<'s> for PyFloat

source§

impl<'s> FromPyObject<'s> for PyLong

source§

impl<'s> FromPyObject<'s> for PyList

source§

impl<'s> FromPyObject<'s> for PyModule

source§

impl<'s> FromPyObject<'s> for PyObject

source§

impl<'s> FromPyObject<'s> for PySequence

source§

impl<'s> FromPyObject<'s> for PySet

source§

impl<'s> FromPyObject<'s> for PyTuple

source§

impl<'s> FromPyObject<'s> for PyType

source§

impl<'s> FromPyObject<'s> for PyString