Trait cpython::FromPyObject [−][src]
pub trait FromPyObject<'s>: Sized {
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
Implementations on Foreign Types
If the python value is None, returns Option::None
.
Otherwise, converts the python value to T
and returns Some(T)
.
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.
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.
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).
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).
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
.
Converts a Python tuple
to a Rust tuple.
Note: only accepts Python tuple
(or derived classes);
other types are not accepted.
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)
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.
impl<'s, A: FromPyObject<'s>, B: FromPyObject<'s>, C: FromPyObject<'s>, D: FromPyObject<'s>> FromPyObject<'s> for (A, B, C, D)
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.
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)
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.
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)
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.
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)
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.
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)
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.
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)
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.