pub trait IntoPyPointer {
    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.


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

Returns the underlying FFI pointer as an owned pointer.

If self has ownership of the underlying pointer, it will “steal” ownership of it.

Implementations on Foreign Types

Convert None into a null pointer.