Trait cpython::FromPyObject[][src]

pub trait FromPyObject<'s>: Sized {
    fn extract(py: Python<'_>, obj: &'s PyObject) -> PyResult<Self>;
}

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

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

Extracts Self from the source PyObject.

Loading content...

Implementations on Foreign Types

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

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

impl<'s> FromPyObject<'s> for bool[src]

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

Converts a Python bool to a rust bool.

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

impl<'s> FromPyObject<'s> for i8[src]

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

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.

impl<'s> FromPyObject<'s> for u8[src]

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

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.

impl<'s> FromPyObject<'s> for i16[src]

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

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.

impl<'s> FromPyObject<'s> for u16[src]

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

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.

impl<'s> FromPyObject<'s> for i32[src]

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

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.

impl<'s> FromPyObject<'s> for u32[src]

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

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.

impl<'s> FromPyObject<'s> for i64[src]

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

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.

impl<'s> FromPyObject<'s> for isize[src]

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

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.

impl<'s> FromPyObject<'s> for usize[src]

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

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.

impl<'s> FromPyObject<'s> for u64[src]

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.

impl<'s> FromPyObject<'s> for f64[src]

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

Converts Python float to Rust f64.

impl<'s> FromPyObject<'s> for f32[src]

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

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.

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

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.

impl<'s> FromPyObject<'s> for Cow<'s, str>[src]

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).

impl<'s> FromPyObject<'s> for String[src]

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).

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

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.

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

Converts a Python tuple to a Rust tuple.

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

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

Converts a Python tuple to a Rust tuple.

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

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

Converts a Python tuple to a Rust tuple.

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

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

Converts a Python tuple to a Rust tuple.

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

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)[src]

Converts a Python tuple to a Rust tuple.

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

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)[src]

Converts a Python tuple to a Rust tuple.

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

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)[src]

Converts a Python tuple to a Rust tuple.

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

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)[src]

Converts a Python tuple to a Rust tuple.

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

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)[src]

Converts a Python tuple to a Rust tuple.

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

Loading content...

Implementors

impl FromPyObject<'_> for PyNone[src]

impl<'s> FromPyObject<'s> for &'s AssertionError[src]

impl<'s> FromPyObject<'s> for &'s AttributeError[src]

impl<'s> FromPyObject<'s> for &'s BaseException[src]

impl<'s> FromPyObject<'s> for &'s BufferError[src]

impl<'s> FromPyObject<'s> for &'s EOFError[src]

impl<'s> FromPyObject<'s> for &'s EnvironmentError[src]

impl<'s> FromPyObject<'s> for &'s Exception[src]

impl<'s> FromPyObject<'s> for &'s FloatingPointError[src]

impl<'s> FromPyObject<'s> for &'s IOError[src]

impl<'s> FromPyObject<'s> for &'s ImportError[src]

impl<'s> FromPyObject<'s> for &'s IndexError[src]

impl<'s> FromPyObject<'s> for &'s KeyError[src]

impl<'s> FromPyObject<'s> for &'s KeyboardInterrupt[src]

impl<'s> FromPyObject<'s> for &'s LookupError[src]

impl<'s> FromPyObject<'s> for &'s MemoryError[src]

impl<'s> FromPyObject<'s> for &'s NameError[src]

impl<'s> FromPyObject<'s> for &'s NotImplementedError[src]

impl<'s> FromPyObject<'s> for &'s OSError[src]

impl<'s> FromPyObject<'s> for &'s OverflowError[src]

impl<'s> FromPyObject<'s> for &'s ReferenceError[src]

impl<'s> FromPyObject<'s> for &'s RuntimeError[src]

impl<'s> FromPyObject<'s> for &'s StandardError[src]

impl<'s> FromPyObject<'s> for &'s SyntaxError[src]

impl<'s> FromPyObject<'s> for &'s SystemError[src]

impl<'s> FromPyObject<'s> for &'s SystemExit[src]

impl<'s> FromPyObject<'s> for &'s TypeError[src]

impl<'s> FromPyObject<'s> for &'s UnicodeDecodeError[src]

impl<'s> FromPyObject<'s> for &'s UnicodeEncodeError[src]

impl<'s> FromPyObject<'s> for &'s UnicodeTranslateError[src]

impl<'s> FromPyObject<'s> for &'s ValueError[src]

impl<'s> FromPyObject<'s> for &'s ZeroDivisionError[src]

impl<'s> FromPyObject<'s> for &'s PyClass[src]

impl<'s> FromPyObject<'s> for &'s PyInstance[src]

impl<'s> FromPyObject<'s> for &'s PyBool[src]

impl<'s> FromPyObject<'s> for &'s PyBytes[src]

impl<'s> FromPyObject<'s> for &'s PyCapsule[src]

impl<'s> FromPyObject<'s> for &'s PyDict[src]

impl<'s> FromPyObject<'s> for &'s PyFloat[src]

impl<'s> FromPyObject<'s> for &'s PyInt[src]

impl<'s> FromPyObject<'s> for &'s PyList[src]

impl<'s> FromPyObject<'s> for &'s PyLong[src]

impl<'s> FromPyObject<'s> for &'s PyModule[src]

impl<'s> FromPyObject<'s> for &'s PyObject[src]

impl<'s> FromPyObject<'s> for &'s PySequence[src]

impl<'s> FromPyObject<'s> for &'s PySet[src]

impl<'s> FromPyObject<'s> for &'s PyString[src]

impl<'s> FromPyObject<'s> for &'s PyTuple[src]

impl<'s> FromPyObject<'s> for &'s PyType[src]

impl<'s> FromPyObject<'s> for &'s PyUnicode[src]

impl<'s> FromPyObject<'s> for AssertionError[src]

impl<'s> FromPyObject<'s> for AttributeError[src]

impl<'s> FromPyObject<'s> for BaseException[src]

impl<'s> FromPyObject<'s> for BufferError[src]

impl<'s> FromPyObject<'s> for EOFError[src]

impl<'s> FromPyObject<'s> for EnvironmentError[src]

impl<'s> FromPyObject<'s> for Exception[src]

impl<'s> FromPyObject<'s> for FloatingPointError[src]

impl<'s> FromPyObject<'s> for IOError[src]

impl<'s> FromPyObject<'s> for ImportError[src]

impl<'s> FromPyObject<'s> for IndexError[src]

impl<'s> FromPyObject<'s> for KeyError[src]

impl<'s> FromPyObject<'s> for KeyboardInterrupt[src]

impl<'s> FromPyObject<'s> for LookupError[src]

impl<'s> FromPyObject<'s> for MemoryError[src]

impl<'s> FromPyObject<'s> for NameError[src]

impl<'s> FromPyObject<'s> for NotImplementedError[src]

impl<'s> FromPyObject<'s> for OSError[src]

impl<'s> FromPyObject<'s> for OverflowError[src]

impl<'s> FromPyObject<'s> for ReferenceError[src]

impl<'s> FromPyObject<'s> for RuntimeError[src]

impl<'s> FromPyObject<'s> for StandardError[src]

impl<'s> FromPyObject<'s> for SyntaxError[src]

impl<'s> FromPyObject<'s> for SystemError[src]

impl<'s> FromPyObject<'s> for SystemExit[src]

impl<'s> FromPyObject<'s> for TypeError[src]

impl<'s> FromPyObject<'s> for UnicodeDecodeError[src]

impl<'s> FromPyObject<'s> for UnicodeEncodeError[src]

impl<'s> FromPyObject<'s> for UnicodeTranslateError[src]

impl<'s> FromPyObject<'s> for ValueError[src]

impl<'s> FromPyObject<'s> for ZeroDivisionError[src]

impl<'s> FromPyObject<'s> for PyClass[src]

impl<'s> FromPyObject<'s> for PyInstance[src]

impl<'s> FromPyObject<'s> for NoArgs[src]

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

Returns Ok(NoArgs) if the input is an empty Python tuple. Otherwise, returns an error.

impl<'s> FromPyObject<'s> for PyBool[src]

impl<'s> FromPyObject<'s> for PyBytes[src]

impl<'s> FromPyObject<'s> for PyCapsule[src]

impl<'s> FromPyObject<'s> for PyDict[src]

impl<'s> FromPyObject<'s> for PyFloat[src]

impl<'s> FromPyObject<'s> for PyInt[src]

impl<'s> FromPyObject<'s> for PyList[src]

impl<'s> FromPyObject<'s> for PyLong[src]

impl<'s> FromPyObject<'s> for PyModule[src]

impl<'s> FromPyObject<'s> for PyObject[src]

impl<'s> FromPyObject<'s> for PySequence[src]

impl<'s> FromPyObject<'s> for PySet[src]

impl<'s> FromPyObject<'s> for PyString[src]

impl<'s> FromPyObject<'s> for PyTuple[src]

impl<'s> FromPyObject<'s> for PyType[src]

impl<'s> FromPyObject<'s> for PyUnicode[src]

Loading content...