Trait pyo3::conversion::IntoPyPointer
source · pub trait IntoPyPointer {
// Required method
fn into_ptr(self) -> *mut PyObject;
}
Expand description
Returns an owned pointer to a Python object.
The returned pointer will be valid until you decrease its reference count. It may be null depending on the implementation. It is your responsibility to decrease the reference count of the pointer to avoid leaking memory.
Examples
use pyo3::prelude::*;
use pyo3::IntoPyPointer;
use pyo3::types::PyString;
use pyo3::ffi;
Python::with_gil(|py| {
let s: Py<PyString> = "foo".into_py(py);
let ptr = s.into_ptr();
let is_really_a_pystring = unsafe { ffi::PyUnicode_CheckExact(ptr) };
assert_eq!(is_really_a_pystring, 1);
// Because we took ownership of the pointer,
// we must manually decrement it to avoid leaking memory
unsafe { ffi::Py_DECREF(ptr) };
});
Required Methods§
Implementations on Foreign Types§
source§impl<T> IntoPyPointer for Option<T>where
T: IntoPyPointer,
impl<T> IntoPyPointer for Option<T>where
T: IntoPyPointer,
Convert None
into a null pointer.