[][src]Struct pyo3::PyObject

#[repr(transparent)]pub struct PyObject(_);

A Python object of any type.

The Python object's lifetime is managed by Python's garbage collector, so to access the object API, a Python<'py> GIL token is required.

See the guide for an explanation of the different Python object types.

Technically, it is a safe wrapper around NonNull<ffi::PyObject>.

Implementations

impl PyObject[src]

pub unsafe fn from_owned_ptr(_py: Python, ptr: *mut PyObject) -> PyObject[src]

Creates a PyObject instance for the given FFI pointer. This moves ownership over the pointer into the PyObject. Undefined behavior if the pointer is NULL or invalid.

pub unsafe fn from_owned_ptr_or_panic(
    py: Python,
    ptr: *mut PyObject
) -> PyObject
[src]

Creates a PyObject instance for the given FFI pointer. Panics if the pointer is NULL. Undefined behavior if the pointer is invalid.

pub unsafe fn from_owned_ptr_or_err(
    py: Python,
    ptr: *mut PyObject
) -> PyResult<PyObject>
[src]

Constructs a PyObject from the result of a Python FFI call that returns a new reference (owned pointer). Returns Err(PyErr) if the pointer is NULL.

pub unsafe fn from_owned_ptr_or_opt(
    _py: Python,
    ptr: *mut PyObject
) -> Option<PyObject>
[src]

Constructs a PyObject from the result of a Python FFI call that returns a new reference (owned pointer). Returns None if the pointer is NULL.

pub unsafe fn from_borrowed_ptr(_py: Python, ptr: *mut PyObject) -> PyObject[src]

Creates a PyObject instance for the given Python FFI pointer. Calls Py_INCREF() on the ptr. Undefined behavior if the pointer is NULL or invalid.

pub unsafe fn from_borrowed_ptr_or_err(
    py: Python,
    ptr: *mut PyObject
) -> PyResult<PyObject>
[src]

Creates a PyObject instance for the given Python FFI pointer. Calls Py_INCREF() on the ptr. Returns Err(PyErr) if the pointer is NULL.

pub unsafe fn from_borrowed_ptr_or_opt(
    py: Python,
    ptr: *mut PyObject
) -> Option<PyObject>
[src]

Creates a PyObject instance for the given Python FFI pointer. Calls Py_INCREF() on the ptr. Returns None if the pointer is NULL.

pub fn get_refcnt(&self, _py: Python) -> isize[src]

Gets the reference count of the ffi::PyObject pointer.

pub fn clone_ref(&self, py: Python) -> Self[src]

Clones self by calling Py_INCREF() on the ptr.

pub fn is_none(&self, _py: Python) -> bool[src]

Returns whether the object is considered to be None.

This is equivalent to the Python expression self is None.

pub fn is_true(&self, py: Python) -> PyResult<bool>[src]

Returns whether the object is considered to be true.

This is equivalent to the Python expression bool(self).

pub fn cast_as<'p, D>(
    &'p self,
    py: Python<'p>
) -> Result<&'p D, PyDowncastError> where
    D: PyTryFrom<'p>, 
[src]

Casts the PyObject to a concrete Python object type.

This can cast only to native Python types, not types implemented in Rust.

pub fn extract<'p, D>(&'p self, py: Python<'p>) -> PyResult<D> where
    D: FromPyObject<'p>, 
[src]

Extracts some type from the Python object.

This is a wrapper function around FromPyObject::extract().

pub fn getattr<N>(&self, py: Python, attr_name: N) -> PyResult<PyObject> where
    N: ToPyObject
[src]

Retrieves an attribute value.

This is equivalent to the Python expression self.attr_name.

pub fn call(
    &self,
    py: Python,
    args: impl IntoPy<Py<PyTuple>>,
    kwargs: Option<&PyDict>
) -> PyResult<PyObject>
[src]

Calls the object.

This is equivalent to the Python expression self(*args, **kwargs).

pub fn call1(
    &self,
    py: Python,
    args: impl IntoPy<Py<PyTuple>>
) -> PyResult<PyObject>
[src]

Calls the object with only positional arguments.

This is equivalent to the Python expression self(*args).

pub fn call0(&self, py: Python) -> PyResult<PyObject>[src]

Calls the object without arguments.

This is equivalent to the Python expression self().

pub fn call_method(
    &self,
    py: Python,
    name: &str,
    args: impl IntoPy<Py<PyTuple>>,
    kwargs: Option<&PyDict>
) -> PyResult<PyObject>
[src]

Calls a method on the object.

This is equivalent to the Python expression self.name(*args, **kwargs).

pub fn call_method1(
    &self,
    py: Python,
    name: &str,
    args: impl IntoPy<Py<PyTuple>>
) -> PyResult<PyObject>
[src]

Calls a method on the object with only positional arguments.

This is equivalent to the Python expression self.name(*args).

pub fn call_method0(&self, py: Python, name: &str) -> PyResult<PyObject>[src]

Calls a method on the object with no arguments.

This is equivalent to the Python expression self.name().

Trait Implementations

impl AsPyPointer for PyObject[src]

fn as_ptr(&self) -> *mut PyObject[src]

Gets the underlying FFI pointer, returns a borrowed pointer.

impl AsPyRef for PyObject[src]

type Target = PyAny

impl Clone for PyObject[src]

impl Debug for PyObject[src]

impl Drop for PyObject[src]

Dropping a PyObject instance decrements the reference count on the object by 1.

impl<'a, T> From<&'a T> for PyObject where
    T: AsPyPointer + PyNativeType
[src]

impl<'a, T> From<&'a mut T> for PyObject where
    T: AsPyPointer + PyNativeType
[src]

impl<T> From<Py<T>> for PyObject[src]

impl<'a> FromPy<&'a [u8]> for PyObject[src]

impl<'a, T> FromPy<&'a T> for PyObject where
    T: AsPyPointer
[src]

impl FromPy<()> for PyObject[src]

impl<K> FromPy<BTreeSet<K>> for PyObject where
    K: IntoPy<PyObject> + Ord + ToPyObject
[src]

impl<K, S> FromPy<HashSet<K, S>> for PyObject where
    K: IntoPy<PyObject> + Eq + Hash,
    S: BuildHasher + Default
[src]

impl FromPy<PyErr> for PyObject[src]

impl<'p, T: PyClass> FromPy<PyRef<'p, T>> for PyObject[src]

impl<'p, T: PyClass> FromPy<PyRefMut<'p, T>> for PyObject[src]

impl FromPy<String> for PyObject[src]

impl FromPy<bool> for PyObject[src]

impl FromPy<f32> for PyObject[src]

impl FromPy<f64> for PyObject[src]

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

fn extract(ob: &'a PyAny) -> PyResult<Self>[src]

Extracts Self from the source PyObject.

impl<T> IntoPy<PyObject> for Option<T> where
    T: IntoPy<PyObject>, 
[src]

impl<'a> IntoPy<PyObject> for &'a PyErr[src]

impl<T> IntoPy<PyObject> for [T; 5] where
    T: ToPyObject
[src]

impl<T> IntoPy<PyObject> for [T; 6] where
    T: ToPyObject
[src]

impl<T> IntoPy<PyObject> for [T; 7] where
    T: ToPyObject
[src]

impl<T> IntoPy<PyObject> for [T; 8] where
    T: ToPyObject
[src]

impl<T> IntoPy<PyObject> for [T; 9] where
    T: ToPyObject
[src]

impl<T> IntoPy<PyObject> for [T; 10] where
    T: ToPyObject
[src]

impl<T> IntoPy<PyObject> for [T; 11] where
    T: ToPyObject
[src]

impl<T> IntoPy<PyObject> for [T; 12] where
    T: ToPyObject
[src]

impl<T> IntoPy<PyObject> for [T; 13] where
    T: ToPyObject
[src]

impl<T> IntoPy<PyObject> for [T; 14] where
    T: ToPyObject
[src]

impl<T> IntoPy<PyObject> for Py<T>[src]

fn into_py(self, _py: Python) -> PyObject[src]

Converts a Py instance to PyObject. Consumes self without calling Py_DECREF().

impl<T> IntoPy<PyObject> for [T; 15] where
    T: ToPyObject
[src]

impl<T> IntoPy<PyObject> for [T; 16] where
    T: ToPyObject
[src]

impl<T> IntoPy<PyObject> for [T; 17] where
    T: ToPyObject
[src]

impl<T> IntoPy<PyObject> for [T; 18] where
    T: ToPyObject
[src]

impl<T> IntoPy<PyObject> for [T; 19] where
    T: ToPyObject
[src]

impl<T> IntoPy<PyObject> for [T; 20] where
    T: ToPyObject
[src]

impl<T> IntoPy<PyObject> for [T; 21] where
    T: ToPyObject
[src]

impl<T> IntoPy<PyObject> for [T; 22] where
    T: ToPyObject
[src]

impl<T> IntoPy<PyObject> for [T; 23] where
    T: ToPyObject
[src]

impl<T> IntoPy<PyObject> for [T; 24] where
    T: ToPyObject
[src]

impl<K, V, H> IntoPy<PyObject> for HashMap<K, V, H> where
    K: Hash + Eq + IntoPy<PyObject>,
    V: IntoPy<PyObject>,
    H: BuildHasher
[src]

impl<T> IntoPy<PyObject> for [T; 25] where
    T: ToPyObject
[src]

impl<T> IntoPy<PyObject> for [T; 26] where
    T: ToPyObject
[src]

impl<T> IntoPy<PyObject> for [T; 27] where
    T: ToPyObject
[src]

impl<T> IntoPy<PyObject> for [T; 28] where
    T: ToPyObject
[src]

impl<T> IntoPy<PyObject> for [T; 29] where
    T: ToPyObject
[src]

impl<T> IntoPy<PyObject> for [T; 30] where
    T: ToPyObject
[src]

impl<T> IntoPy<PyObject> for [T; 31] where
    T: ToPyObject
[src]

impl<T> IntoPy<PyObject> for [T; 32] where
    T: ToPyObject
[src]

impl<T> IntoPy<PyObject> for Vec<T> where
    T: IntoPy<PyObject>, 
[src]

impl IntoPy<PyObject> for i8[src]

impl<K, V> IntoPy<PyObject> for BTreeMap<K, V> where
    K: Eq + IntoPy<PyObject>,
    V: IntoPy<PyObject>, 
[src]

impl IntoPy<PyObject> for u8[src]

impl IntoPy<PyObject> for i16[src]

impl IntoPy<PyObject> for u16[src]

impl IntoPy<PyObject> for i32[src]

impl IntoPy<PyObject> for u32[src]

impl IntoPy<PyObject> for i64[src]

impl IntoPy<PyObject> for isize[src]

impl IntoPy<PyObject> for usize[src]

impl IntoPy<PyObject> for u64[src]

impl IntoPy<PyObject> for i128[src]

impl<T> IntoPy<PyObject> for [T; 0] where
    T: ToPyObject
[src]

impl IntoPy<PyObject> for u128[src]

impl<'a> IntoPy<PyObject> for &'a str[src]

impl<'a> IntoPy<PyObject> for &'a String[src]

impl<A: IntoPy<PyObject>> IntoPy<PyObject> for (A,)[src]

impl<A: IntoPy<PyObject>, B: IntoPy<PyObject>> IntoPy<PyObject> for (A, B)[src]

impl<A: IntoPy<PyObject>, B: IntoPy<PyObject>, C: IntoPy<PyObject>> IntoPy<PyObject> for (A, B, C)[src]

impl<A: IntoPy<PyObject>, B: IntoPy<PyObject>, C: IntoPy<PyObject>, D: IntoPy<PyObject>> IntoPy<PyObject> for (A, B, C, D)[src]

impl<A: IntoPy<PyObject>, B: IntoPy<PyObject>, C: IntoPy<PyObject>, D: IntoPy<PyObject>, E: IntoPy<PyObject>> IntoPy<PyObject> for (A, B, C, D, E)[src]

impl<A: IntoPy<PyObject>, B: IntoPy<PyObject>, C: IntoPy<PyObject>, D: IntoPy<PyObject>, E: IntoPy<PyObject>, F: IntoPy<PyObject>> IntoPy<PyObject> for (A, B, C, D, E, F)[src]

impl<A: IntoPy<PyObject>, B: IntoPy<PyObject>, C: IntoPy<PyObject>, D: IntoPy<PyObject>, E: IntoPy<PyObject>, F: IntoPy<PyObject>, G: IntoPy<PyObject>> IntoPy<PyObject> for (A, B, C, D, E, F, G)[src]

impl<T> IntoPy<PyObject> for [T; 1] where
    T: ToPyObject
[src]

impl<A: IntoPy<PyObject>, B: IntoPy<PyObject>, C: IntoPy<PyObject>, D: IntoPy<PyObject>, E: IntoPy<PyObject>, F: IntoPy<PyObject>, G: IntoPy<PyObject>, H: IntoPy<PyObject>> IntoPy<PyObject> for (A, B, C, D, E, F, G, H)[src]

impl<A: IntoPy<PyObject>, B: IntoPy<PyObject>, C: IntoPy<PyObject>, D: IntoPy<PyObject>, E: IntoPy<PyObject>, F: IntoPy<PyObject>, G: IntoPy<PyObject>, H: IntoPy<PyObject>, I: IntoPy<PyObject>> IntoPy<PyObject> for (A, B, C, D, E, F, G, H, I)[src]

impl<T> IntoPy<PyObject> for [T; 2] where
    T: ToPyObject
[src]

impl<T> IntoPy<PyObject> for [T; 3] where
    T: ToPyObject
[src]

impl<T> IntoPy<PyObject> for [T; 4] where
    T: ToPyObject
[src]

impl IntoPyPointer for PyObject[src]

#[must_use]fn into_ptr(self) -> *mut PyObject[src]

Gets the underlying FFI pointer, returns a owned pointer.

impl PartialEq<PyObject> for PyObject[src]

fn eq(&self, o: &PyObject) -> bool[src]

Checks for pointer identity, not equivalent to Python's __eq__.

impl Send for PyObject[src]

impl Sync for PyObject[src]

impl ToPyObject for PyObject[src]

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T> FromPy<T> for T[src]

impl<'a, T> FromPyObject<'a> for T where
    T: PyClass + Clone
[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> IntoPy<U> for T where
    U: FromPy<T>, 
[src]

impl<T> ToBorrowedObject for T where
    T: ToPyObject
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.