[−][src]Struct cpython::PyObject
Represents a reference to a Python object.
Python objects are reference counted.
Calling clone_ref()
on a PyObject
will return a new reference to the same object
(thus incrementing the reference count).
The Drop
implementation will automatically decrement the reference count.
You can also call release_ref()
to explicitly decrement the reference count.
This is slightly faster than relying on automatic drop, because release_ref
does not need to check whether the GIL needs to be acquired.
PyObject
can be used with all Python objects, since all python types
derive from object
. This crate also contains other, more specific types
that serve as references to Python objects (e.g. PyTuple
for Python tuples, etc.).
You can convert from any Python object to PyObject
by calling as_object()
or into_object()
from the PythonObject trait.
In the other direction, you can call cast_as()
or cast_into()
on PyObject
to convert to more specific object types.
Most of the interesting methods are provided by the ObjectProtocol trait.
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_borrowed_ptr(_py: Python<'_>, ptr: *mut PyObject) -> PyObject
[src]
Creates a PyObject instance for the given FFI pointer. Calls Py_INCREF() on the ptr. Undefined behavior if the pointer is NULL or invalid.
pub unsafe fn from_owned_ptr_opt(
py: Python<'_>,
ptr: *mut PyObject
) -> Option<PyObject>
[src]
py: Python<'_>,
ptr: *mut PyObject
) -> Option<PyObject>
Creates a PyObject instance for the given FFI pointer. This moves ownership over the pointer into the PyObject. Returns None for null pointers; undefined behavior if the pointer is invalid.
pub unsafe fn from_borrowed_ptr_opt(
py: Python<'_>,
ptr: *mut PyObject
) -> Option<PyObject>
[src]
py: Python<'_>,
ptr: *mut PyObject
) -> Option<PyObject>
Returns None for null pointers; undefined behavior if the pointer is invalid.
pub fn as_ptr(&self) -> *mut PyObject
[src]
Gets the underlying FFI pointer. Returns a borrowed pointer.
#[must_use]pub fn steal_ptr(self) -> *mut PyObject
[src]
Gets the underlying FFI pointer.
Consumes self
without calling Py_DECREF()
, thus returning an owned pointer.
pub unsafe fn borrow_from_ptr<'a>(ptr: &'a *mut PyObject) -> &'a PyObject
[src]
Transmutes an FFI pointer to &PyObject
.
Undefined behavior if the pointer is NULL or invalid.
pub unsafe fn borrow_from_owned_ptr_slice<'a>(
ptr: &'a [*mut PyObject]
) -> &'a [PyObject]
[src]
ptr: &'a [*mut PyObject]
) -> &'a [PyObject]
Transmutes a slice of owned FFI pointers to &[PyObject]
.
Undefined behavior if any pointer in the slice is NULL or invalid.
pub fn get_refcnt(&self, _py: Python<'_>) -> usize
[src]
Gets the reference count of this Python object.
pub fn get_type(&self, py: Python<'_>) -> PyType
[src]
Gets the Python type object for this object's type.
pub unsafe fn unchecked_cast_into<T>(self) -> T where
T: PythonObject,
[src]
T: PythonObject,
Casts the PyObject to a concrete Python object type.
Causes undefined behavior if the object is not of the expected type.
This is a wrapper function around PythonObject::unchecked_downcast_from()
.
pub fn cast_into<'p, T>(
self,
py: Python<'p>
) -> Result<T, PythonObjectDowncastError<'p>> where
T: PythonObjectWithCheckedDowncast,
[src]
self,
py: Python<'p>
) -> Result<T, PythonObjectDowncastError<'p>> where
T: PythonObjectWithCheckedDowncast,
Casts the PyObject to a concrete Python object type.
Fails with PythonObjectDowncastError
if the object is not of the expected type.
This is a wrapper function around PythonObjectWithCheckedDowncast::downcast_from()
.
pub unsafe fn unchecked_cast_as<'s, T>(&'s self) -> &'s T where
T: PythonObject,
[src]
T: PythonObject,
Casts the PyObject to a concrete Python object type.
Causes undefined behavior if the object is not of the expected type.
This is a wrapper function around PythonObject::unchecked_downcast_borrow_from()
.
pub fn cast_as<'s, 'p, T>(
&'s self,
py: Python<'p>
) -> Result<&'s T, PythonObjectDowncastError<'p>> where
T: PythonObjectWithCheckedDowncast,
[src]
&'s self,
py: Python<'p>
) -> Result<&'s T, PythonObjectDowncastError<'p>> where
T: PythonObjectWithCheckedDowncast,
Casts the PyObject to a concrete Python object type.
Fails with PythonObjectDowncastError
if the object is not of the expected type.
This is a wrapper function around PythonObjectWithCheckedDowncast::downcast_borrow_from()
.
pub fn extract<'a, T>(&'a self, py: Python<'_>) -> PyResult<T> where
T: FromPyObject<'a>,
[src]
T: FromPyObject<'a>,
Extracts some type from the Python object.
This is a wrapper function around FromPyObject::from_py_object()
.
Trait Implementations
impl BaseObject for PyObject
[src]
pub fn size() -> usize
[src]
type InitType = ()
pub unsafe fn alloc(
py: Python<'_>,
ty: &PyType,
_init_val: ()
) -> PyResult<PyObject>
[src]
py: Python<'_>,
ty: &PyType,
_init_val: ()
) -> PyResult<PyObject>
pub unsafe fn dealloc(_py: Python<'_>, obj: *mut PyObject)
[src]
impl Debug for PyObject
[src]
impl Display for PyObject
[src]
impl Drop for PyObject
[src]
Dropping a PyObject
decrements the reference count on the object by 1.
impl Eq for PyObject
[src]
PyObject implements the ==
operator using reference equality:
obj1 == obj2
in rust is equivalent to obj1 is obj2
in Python.
impl<'s> FromPyObject<'s> for PyObject
[src]
impl<'s> FromPyObject<'s> for &'s PyObject
[src]
impl ObjectProtocol for PyObject
[src]
pub fn hasattr<N>(&self, py: Python<'_>, attr_name: N) -> PyResult<bool> where
N: ToPyObject,
[src]
N: ToPyObject,
pub fn getattr<N>(&self, py: Python<'_>, attr_name: N) -> PyResult<PyObject> where
N: ToPyObject,
[src]
N: ToPyObject,
pub fn setattr<N, V>(
&self,
py: Python<'_>,
attr_name: N,
value: V
) -> PyResult<()> where
N: ToPyObject,
V: ToPyObject,
[src]
&self,
py: Python<'_>,
attr_name: N,
value: V
) -> PyResult<()> where
N: ToPyObject,
V: ToPyObject,
pub fn delattr<N>(&self, py: Python<'_>, attr_name: N) -> PyResult<()> where
N: ToPyObject,
[src]
N: ToPyObject,
pub fn compare<O>(&self, py: Python<'_>, other: O) -> PyResult<Ordering> where
O: ToPyObject,
[src]
O: ToPyObject,
pub fn rich_compare<O>(
&self,
py: Python<'_>,
other: O,
compare_op: CompareOp
) -> PyResult<PyObject> where
O: ToPyObject,
[src]
&self,
py: Python<'_>,
other: O,
compare_op: CompareOp
) -> PyResult<PyObject> where
O: ToPyObject,
pub fn repr(&self, py: Python<'_>) -> PyResult<PyString>
[src]
pub fn str(&self, py: Python<'_>) -> PyResult<PyString>
[src]
pub fn is_callable(&self, _py: Python<'_>) -> bool
[src]
pub fn call<A>(
&self,
py: Python<'_>,
args: A,
kwargs: Option<&PyDict>
) -> PyResult<PyObject> where
A: ToPyObject<ObjectType = PyTuple>,
[src]
&self,
py: Python<'_>,
args: A,
kwargs: Option<&PyDict>
) -> PyResult<PyObject> where
A: ToPyObject<ObjectType = PyTuple>,
pub fn call_method<A>(
&self,
py: Python<'_>,
name: &str,
args: A,
kwargs: Option<&PyDict>
) -> PyResult<PyObject> where
A: ToPyObject<ObjectType = PyTuple>,
[src]
&self,
py: Python<'_>,
name: &str,
args: A,
kwargs: Option<&PyDict>
) -> PyResult<PyObject> where
A: ToPyObject<ObjectType = PyTuple>,
pub fn hash(&self, py: Python<'_>) -> PyResult<Py_hash_t>
[src]
pub fn is_true(&self, py: Python<'_>) -> PyResult<bool>
[src]
pub fn len(&self, py: Python<'_>) -> PyResult<usize>
[src]
pub fn get_item<K>(&self, py: Python<'_>, key: K) -> PyResult<PyObject> where
K: ToPyObject,
[src]
K: ToPyObject,
pub fn set_item<K, V>(&self, py: Python<'_>, key: K, value: V) -> PyResult<()> where
K: ToPyObject,
V: ToPyObject,
[src]
K: ToPyObject,
V: ToPyObject,
pub fn del_item<K>(&self, py: Python<'_>, key: K) -> PyResult<()> where
K: ToPyObject,
[src]
K: ToPyObject,
pub fn iter<'p>(&self, py: Python<'p>) -> PyResult<PyIterator<'p>>
[src]
impl PartialEq<PyObject> for PyObject
[src]
PyObject implements the ==
operator using reference equality:
obj1 == obj2
in rust is equivalent to obj1 is obj2
in Python.
pub fn eq(&self, o: &PyObject) -> bool
[src]
#[must_use]pub fn ne(&self, other: &Rhs) -> bool
1.0.0[src]
impl PythonObject for PyObject
[src]
pub fn as_object(&self) -> &PyObject
[src]
pub fn into_object(self) -> PyObject
[src]
pub unsafe fn unchecked_downcast_from(o: PyObject) -> PyObject
[src]
pub unsafe fn unchecked_downcast_borrow_from(o: &PyObject) -> &PyObject
[src]
impl PythonObjectWithCheckedDowncast for PyObject
[src]
pub fn downcast_from<'p>(
_py: Python<'p>,
obj: PyObject
) -> Result<PyObject, PythonObjectDowncastError<'p>>
[src]
_py: Python<'p>,
obj: PyObject
) -> Result<PyObject, PythonObjectDowncastError<'p>>
pub fn downcast_borrow_from<'a, 'p>(
_py: Python<'p>,
obj: &'a PyObject
) -> Result<&'a PyObject, PythonObjectDowncastError<'p>>
[src]
_py: Python<'p>,
obj: &'a PyObject
) -> Result<&'a PyObject, PythonObjectDowncastError<'p>>
impl PythonObjectWithTypeObject for PyObject
[src]
pub fn type_object(py: Python<'_>) -> PyType
[src]
impl Send for PyObject
[src]
impl Sync for PyObject
[src]
impl ToPyObject for PyObject
[src]
Identity conversion: allows using existing PyObject
instances where
T: ToPyObject
is expected.
type ObjectType = PyObject
pub fn to_py_object(&self, py: Python<'_>) -> PyObject
[src]
pub fn into_py_object(self, _py: Python<'_>) -> PyObject
[src]
pub fn with_borrowed_ptr<F, R>(&self, _py: Python<'_>, f: F) -> R where
F: FnOnce(*mut PyObject) -> R,
[src]
F: FnOnce(*mut PyObject) -> R,
Auto Trait Implementations
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToString for T where
T: Display + ?Sized,
[src]
T: Display + ?Sized,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,