pub struct PyObjectRef { /* private fields */ }
Expand description
The PyObjectRef
is one of the most used types. It is a reference to a
python object. A single python object can have multiple references, and
this reference counting is accounted for by this type. Use the .clone()
method to create a new reference and increment the amount of references
to the python object by 1.
Implementations§
Source§impl PyObjectRef
impl PyObjectRef
Sourcepub fn try_complex(
&self,
vm: &VirtualMachine,
) -> PyResult<Option<(Complex64, bool)>>
pub fn try_complex( &self, vm: &VirtualMachine, ) -> PyResult<Option<(Complex64, bool)>>
Tries converting a python object into a complex, returns an option of whether the complex and whether the object was a complex originally or coereced into one
Source§impl PyObjectRef
impl PyObjectRef
Sourcepub fn try_to_bool(self, vm: &VirtualMachine) -> PyResult<bool>
pub fn try_to_bool(self, vm: &VirtualMachine) -> PyResult<bool>
Convert Python bool into Rust bool.
Source§impl PyObjectRef
impl PyObjectRef
pub fn try_into_value<T>(self, vm: &VirtualMachine) -> PyResult<T>where
T: TryFromObject,
Source§impl PyObjectRef
impl PyObjectRef
pub fn into_raw(self) -> *const PyObject
Sourcepub unsafe fn from_raw(ptr: *const PyObject) -> Self
pub unsafe fn from_raw(ptr: *const PyObject) -> Self
§Safety
The raw pointer must have been previously returned from a call to
PyObjectRef::into_raw
. The user is responsible for ensuring that the inner data is not
dropped more than once due to mishandling the reference count by calling this function
too many times.
Sourcepub fn downcast<T: PyObjectPayload>(self) -> Result<PyRef<T>, Self>
pub fn downcast<T: PyObjectPayload>(self) -> Result<PyRef<T>, Self>
Attempt to downcast this reference to a subclass.
If the downcast fails, the original ref is returned in as Err
so
another downcast can be attempted without unnecessary cloning.
pub fn downcast_ref<T: PyObjectPayload>(&self) -> Option<&Py<T>>
Sourcepub unsafe fn downcast_unchecked<T: PyObjectPayload>(self) -> PyRef<T>
pub unsafe fn downcast_unchecked<T: PyObjectPayload>(self) -> PyRef<T>
Sourcepub unsafe fn downcast_unchecked_ref<T: PyObjectPayload>(&self) -> &Py<T>
pub unsafe fn downcast_unchecked_ref<T: PyObjectPayload>(&self) -> &Py<T>
§Safety
T must be the exact payload type
Sourcepub fn downcast_exact<T: PyObjectPayload + PyPayload>(
self,
vm: &VirtualMachine,
) -> Result<PyRefExact<T>, Self>
pub fn downcast_exact<T: PyObjectPayload + PyPayload>( self, vm: &VirtualMachine, ) -> Result<PyRefExact<T>, Self>
Attempt to downcast this reference to the specific class that is associated T
.
If the downcast fails, the original ref is returned in as Err
so
another downcast can be attempted without unnecessary cloning.
Source§impl PyObjectRef
impl PyObjectRef
pub fn rich_compare( self, other: Self, opid: PyComparisonOp, vm: &VirtualMachine, ) -> PyResult
pub fn bytes(self, vm: &VirtualMachine) -> PyResult
pub fn is_true(self, vm: &VirtualMachine) -> PyResult<bool>
pub fn not(self, vm: &VirtualMachine) -> PyResult<bool>
pub fn length_hint( self, defaultvalue: usize, vm: &VirtualMachine, ) -> PyResult<usize>
pub fn dir(self, vm: &VirtualMachine) -> PyResult<PyList>
Methods from Deref<Target = PyObject>§
pub fn try_to_value<'a, T>(&'a self, vm: &VirtualMachine) -> PyResult<T>where
T: 'a + TryFromBorrowedObject<'a>,
pub fn try_to_ref<'a, T>(&'a self, vm: &VirtualMachine) -> PyResult<&'a Py<T>>where
T: 'a + PyPayload,
pub fn try_value_with<T, F, R>(&self, f: F, vm: &VirtualMachine) -> PyResult<R>
pub fn try_bytes_like<R>( &self, vm: &VirtualMachine, f: impl FnOnce(&[u8]) -> R, ) -> PyResult<R>
pub fn try_rw_bytes_like<R>( &self, vm: &VirtualMachine, f: impl FnOnce(&mut [u8]) -> R, ) -> PyResult<R>
pub fn as_interned_str( &self, vm: &VirtualMachine, ) -> Option<&'static PyStrInterned>
pub fn downgrade( &self, callback: Option<PyObjectRef>, vm: &VirtualMachine, ) -> PyResult<PyRef<PyWeak>>
pub fn get_weak_references(&self) -> Option<Vec<PyRef<PyWeak>>>
pub fn payload_is<T: PyObjectPayload>(&self) -> bool
Sourcepub unsafe fn payload_unchecked<T: PyObjectPayload>(&self) -> &T
pub unsafe fn payload_unchecked<T: PyObjectPayload>(&self) -> &T
pub fn payload<T: PyObjectPayload>(&self) -> Option<&T>
pub fn class(&self) -> &Py<PyType>
pub fn set_class(&self, typ: PyTypeRef, vm: &VirtualMachine)
pub fn payload_if_exact<T: PyObjectPayload + PyPayload>( &self, vm: &VirtualMachine, ) -> Option<&T>
pub fn dict(&self) -> Option<PyDictRef>
Sourcepub fn set_dict(&self, dict: PyDictRef) -> Result<(), PyDictRef>
pub fn set_dict(&self, dict: PyDictRef) -> Result<(), PyDictRef>
Set the dict field. Returns Err(dict)
if this object does not have a dict field
in the first place.
pub fn payload_if_subclass<T: PyPayload>( &self, vm: &VirtualMachine, ) -> Option<&T>
pub fn downcast_ref<T: PyObjectPayload>(&self) -> Option<&Py<T>>
pub fn downcast_ref_if_exact<T: PyObjectPayload + PyPayload>( &self, vm: &VirtualMachine, ) -> Option<&Py<T>>
Sourcepub unsafe fn downcast_unchecked_ref<T: PyObjectPayload>(&self) -> &Py<T>
pub unsafe fn downcast_unchecked_ref<T: PyObjectPayload>(&self) -> &Py<T>
§Safety
T must be the exact payload type
pub fn strong_count(&self) -> usize
pub fn weak_count(&self) -> Option<usize>
pub fn as_raw(&self) -> *const PyObject
pub fn to_callable(&self) -> Option<PyCallable<'_>>
pub fn is_callable(&self) -> bool
Sourcepub fn call(&self, args: impl IntoFuncArgs, vm: &VirtualMachine) -> PyResult
pub fn call(&self, args: impl IntoFuncArgs, vm: &VirtualMachine) -> PyResult
PyObject_CallArg series
Sourcepub fn call_with_args(&self, args: FuncArgs, vm: &VirtualMachine) -> PyResult
pub fn call_with_args(&self, args: FuncArgs, vm: &VirtualMachine) -> PyResult
PyObject_Call
pub fn to_mapping(&self) -> PyMapping<'_>
pub fn to_number(&self) -> PyNumber<'_>
pub fn try_index_opt(&self, vm: &VirtualMachine) -> Option<PyResult<PyIntRef>>
pub fn try_index(&self, vm: &VirtualMachine) -> PyResult<PyIntRef>
pub fn try_int(&self, vm: &VirtualMachine) -> PyResult<PyIntRef>
pub fn try_float_opt( &self, vm: &VirtualMachine, ) -> Option<PyResult<PyRef<PyFloat>>>
pub fn try_float(&self, vm: &VirtualMachine) -> PyResult<PyRef<PyFloat>>
Sourcepub fn get_iter(&self, vm: &VirtualMachine) -> PyResult<PyIter>
pub fn get_iter(&self, vm: &VirtualMachine) -> PyResult<PyIter>
Takes an object and returns an iterator for it. This is typically a new iterator but if the argument is an iterator, this returns itself.
pub fn get_aiter(&self, vm: &VirtualMachine) -> PyResult
pub fn has_attr<'a>( &self, attr_name: impl AsPyStr<'a>, vm: &VirtualMachine, ) -> PyResult<bool>
pub fn get_attr<'a>( &self, attr_name: impl AsPyStr<'a>, vm: &VirtualMachine, ) -> PyResult
pub fn call_set_attr( &self, vm: &VirtualMachine, attr_name: &Py<PyStr>, attr_value: PySetterValue, ) -> PyResult<()>
pub fn set_attr<'a>( &self, attr_name: impl AsPyStr<'a>, attr_value: impl Into<PyObjectRef>, vm: &VirtualMachine, ) -> PyResult<()>
pub fn generic_setattr( &self, attr_name: &Py<PyStr>, value: PySetterValue, vm: &VirtualMachine, ) -> PyResult<()>
pub fn generic_getattr(&self, name: &Py<PyStr>, vm: &VirtualMachine) -> PyResult
Sourcepub fn generic_getattr_opt(
&self,
name_str: &Py<PyStr>,
dict: Option<PyDictRef>,
vm: &VirtualMachine,
) -> PyResult<Option<PyObjectRef>>
pub fn generic_getattr_opt( &self, name_str: &Py<PyStr>, dict: Option<PyDictRef>, vm: &VirtualMachine, ) -> PyResult<Option<PyObjectRef>>
CPython _PyObject_GenericGetAttrWithDict
pub fn del_attr<'a>( &self, attr_name: impl AsPyStr<'a>, vm: &VirtualMachine, ) -> PyResult<()>
pub fn rich_compare_bool( &self, other: &Self, opid: PyComparisonOp, vm: &VirtualMachine, ) -> PyResult<bool>
pub fn repr(&self, vm: &VirtualMachine) -> PyResult<PyStrRef>
pub fn ascii(&self, vm: &VirtualMachine) -> PyResult<AsciiString>
pub fn str(&self, vm: &VirtualMachine) -> PyResult<PyStrRef>
Sourcepub fn is_subclass(&self, cls: &PyObject, vm: &VirtualMachine) -> PyResult<bool>
pub fn is_subclass(&self, cls: &PyObject, vm: &VirtualMachine) -> PyResult<bool>
Determines if self
is a subclass of cls
, either directly, indirectly or virtually
via the subclasscheck magic method.
Sourcepub fn is_instance(&self, cls: &PyObject, vm: &VirtualMachine) -> PyResult<bool>
pub fn is_instance(&self, cls: &PyObject, vm: &VirtualMachine) -> PyResult<bool>
Determines if self
is an instance of cls
, either directly, indirectly or virtually via
the instancecheck magic method.
pub fn hash(&self, vm: &VirtualMachine) -> PyResult<PyHash>
pub fn obj_type(&self) -> PyObjectRef
pub fn type_check(&self, typ: &Py<PyType>) -> bool
pub fn length_opt(&self, vm: &VirtualMachine) -> Option<PyResult<usize>>
pub fn length(&self, vm: &VirtualMachine) -> PyResult<usize>
pub fn get_item<K: DictKey + ?Sized>( &self, needle: &K, vm: &VirtualMachine, ) -> PyResult
pub fn set_item<K: DictKey + ?Sized>( &self, needle: &K, value: PyObjectRef, vm: &VirtualMachine, ) -> PyResult<()>
pub fn del_item<K: DictKey + ?Sized>( &self, needle: &K, vm: &VirtualMachine, ) -> PyResult<()>
pub fn to_sequence(&self) -> PySequence<'_>
Trait Implementations§
Source§impl AsRef<PyObject> for PyObjectRef
impl AsRef<PyObject> for PyObjectRef
Source§impl Borrow<PyObject> for PyObjectRef
impl Borrow<PyObject> for PyObjectRef
Source§impl Clone for PyObjectRef
impl Clone for PyObjectRef
Source§impl Debug for PyObjectRef
impl Debug for PyObjectRef
Source§impl Deref for PyObjectRef
impl Deref for PyObjectRef
Source§impl Drop for PyObjectRef
impl Drop for PyObjectRef
Source§impl Dumpable for PyObjectRef
impl Dumpable for PyObjectRef
Source§impl From<ArgCallable> for PyObjectRef
impl From<ArgCallable> for PyObjectRef
Source§fn from(value: ArgCallable) -> PyObjectRef
fn from(value: ArgCallable) -> PyObjectRef
Source§impl From<ArgMapping> for PyObjectRef
impl From<ArgMapping> for PyObjectRef
Source§fn from(value: ArgMapping) -> PyObjectRef
fn from(value: ArgMapping) -> PyObjectRef
Source§impl<A: Into<PyObjectRef>, B: Into<PyObjectRef>> From<Either<A, B>> for PyObjectRef
impl<A: Into<PyObjectRef>, B: Into<PyObjectRef>> From<Either<A, B>> for PyObjectRef
Source§impl From<PyIter> for PyObjectRef
impl From<PyIter> for PyObjectRef
Source§fn from(value: PyIter<PyObjectRef>) -> PyObjectRef
fn from(value: PyIter<PyObjectRef>) -> PyObjectRef
Source§impl From<PyObjectRef> for PyAtomicRef<PyObject>
impl From<PyObjectRef> for PyAtomicRef<PyObject>
Source§fn from(obj: PyObjectRef) -> Self
fn from(obj: PyObjectRef) -> Self
Source§impl From<PyObjectRef> for PyClassMethod
impl From<PyObjectRef> for PyClassMethod
Source§fn from(callable: PyObjectRef) -> Self
fn from(callable: PyObjectRef) -> Self
Source§impl From<PyObjectRef> for PyStaticMethod
impl From<PyObjectRef> for PyStaticMethod
Source§fn from(callable: PyObjectRef) -> Self
fn from(callable: PyObjectRef) -> Self
Source§impl<T> From<PyRef<T>> for PyObjectRefwhere
T: PyObjectPayload,
impl<T> From<PyRef<T>> for PyObjectRefwhere
T: PyObjectPayload,
Source§impl FromIterator<PyObjectRef> for PyList
impl FromIterator<PyObjectRef> for PyList
Source§fn from_iter<T: IntoIterator<Item = PyObjectRef>>(iter: T) -> Self
fn from_iter<T: IntoIterator<Item = PyObjectRef>>(iter: T) -> Self
Source§impl ToPyObject for PyObjectRef
impl ToPyObject for PyObjectRef
fn to_pyobject(self, _vm: &VirtualMachine) -> PyObjectRef
Source§impl Traverse for PyObjectRef
impl Traverse for PyObjectRef
Source§fn traverse(&self, traverse_fn: &mut TraverseFn<'_>)
fn traverse(&self, traverse_fn: &mut TraverseFn<'_>)
traverse()
with caution! Following those guideline so traverse doesn’t cause memory error!: Read moreSource§impl TryFromObject for PyObjectRef
impl TryFromObject for PyObjectRef
Source§fn try_from_object(_vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self>
fn try_from_object(_vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self>
Auto Trait Implementations§
impl Freeze for PyObjectRef
impl !RefUnwindSafe for PyObjectRef
impl !Send for PyObjectRef
impl !Sync for PyObjectRef
impl Unpin for PyObjectRef
impl !UnwindSafe for PyObjectRef
Blanket Implementations§
Source§impl<T> AsObject for T
impl<T> AsObject for T
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> FromArgOptional for Twhere
T: TryFromObject,
impl<T> FromArgOptional for Twhere
T: TryFromObject,
Source§impl<T> FromArgs for Twhere
T: TryFromObject,
impl<T> FromArgs for Twhere
T: TryFromObject,
Source§fn arity() -> RangeInclusive<usize>
fn arity() -> RangeInclusive<usize>
Source§fn from_args(
vm: &VirtualMachine,
args: &mut FuncArgs,
) -> Result<T, ArgumentError>
fn from_args( vm: &VirtualMachine, args: &mut FuncArgs, ) -> Result<T, ArgumentError>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more