pyo3::conversion

Trait AsPyPointer

Source
pub unsafe trait AsPyPointer {
    // Required method
    fn as_ptr(&self) -> *mut PyObject;
}
Expand description

Returns a borrowed pointer to a Python object.

The returned pointer will be valid for as long as self is. It may be null depending on the implementation.

§Examples

use pyo3::prelude::*;
use pyo3::types::PyString;
use pyo3::ffi;

Python::with_gil(|py| {
    let s: Py<PyString> = "foo".into_py(py);
    let ptr = s.as_ptr();

    let is_really_a_pystring = unsafe { ffi::PyUnicode_CheckExact(ptr) };
    assert_eq!(is_really_a_pystring, 1);
});

§Safety

For callers, it is your responsibility to make sure that the underlying Python object is not dropped too early. For example, the following code will cause undefined behavior:

Python::with_gil(|py| {
    let ptr: *mut ffi::PyObject = 0xabad1dea_u32.into_py(py).as_ptr();

    let isnt_a_pystring = unsafe {
        // `ptr` is dangling, this is UB
        ffi::PyUnicode_CheckExact(ptr)
    };
});

This happens because the pointer returned by as_ptr does not carry any lifetime information and the Python object is dropped immediately after the 0xabad1dea_u32.into_py(py).as_ptr() expression is evaluated. To fix the problem, bind Python object to a local variable like earlier to keep the Python object alive until the end of its scope.

Implementors must ensure this returns a valid pointer to a Python object, which borrows a reference count from &self.

Required Methods§

Source

fn as_ptr(&self) -> *mut PyObject

Returns the underlying FFI pointer as a borrowed pointer.

Implementations on Foreign Types§

Source§

impl<T> AsPyPointer for Option<T>
where T: AsPyPointer,

Convert None into a null pointer.

Implementors§

Source§

impl AsPyPointer for CancelledError

Source§

impl AsPyPointer for IncompleteReadError

Source§

impl AsPyPointer for InvalidStateError

Source§

impl AsPyPointer for LimitOverrunError

Source§

impl AsPyPointer for QueueEmpty

Source§

impl AsPyPointer for QueueFull

Source§

impl AsPyPointer for TimeoutError

Source§

impl AsPyPointer for gaierror

Source§

impl AsPyPointer for herror

Source§

impl AsPyPointer for timeout

Source§

impl AsPyPointer for PyArithmeticError

Source§

impl AsPyPointer for PyAssertionError

Source§

impl AsPyPointer for PyAttributeError

Source§

impl AsPyPointer for PyBaseException

Source§

impl AsPyPointer for PyBlockingIOError

Source§

impl AsPyPointer for PyBrokenPipeError

Source§

impl AsPyPointer for PyBufferError

Source§

impl AsPyPointer for PyBytesWarning

Source§

impl AsPyPointer for PyChildProcessError

Source§

impl AsPyPointer for PyConnectionAbortedError

Source§

impl AsPyPointer for PyConnectionError

Source§

impl AsPyPointer for PyConnectionRefusedError

Source§

impl AsPyPointer for PyConnectionResetError

Source§

impl AsPyPointer for PyDeprecationWarning

Source§

impl AsPyPointer for PyEOFError

Source§

impl AsPyPointer for PyEncodingWarning

Source§

impl AsPyPointer for PyEnvironmentError

Source§

impl AsPyPointer for PyException

Source§

impl AsPyPointer for PyFileExistsError

Source§

impl AsPyPointer for PyFileNotFoundError

Source§

impl AsPyPointer for PyFloatingPointError

Source§

impl AsPyPointer for PyFutureWarning

Source§

impl AsPyPointer for PyGeneratorExit

Source§

impl AsPyPointer for PyIOError

Source§

impl AsPyPointer for PyImportError

Source§

impl AsPyPointer for PyImportWarning

Source§

impl AsPyPointer for PyIndexError

Source§

impl AsPyPointer for PyInterruptedError

Source§

impl AsPyPointer for PyIsADirectoryError

Source§

impl AsPyPointer for PyKeyError

Source§

impl AsPyPointer for PyKeyboardInterrupt

Source§

impl AsPyPointer for PyLookupError

Source§

impl AsPyPointer for PyMemoryError

Source§

impl AsPyPointer for PyModuleNotFoundError

Source§

impl AsPyPointer for PyNameError

Source§

impl AsPyPointer for PyNotADirectoryError

Source§

impl AsPyPointer for PyNotImplementedError

Source§

impl AsPyPointer for PyOSError

Source§

impl AsPyPointer for PyOverflowError

Source§

impl AsPyPointer for PyPendingDeprecationWarning

Source§

impl AsPyPointer for PyPermissionError

Source§

impl AsPyPointer for PyProcessLookupError

Source§

impl AsPyPointer for PyRecursionError

Source§

impl AsPyPointer for PyReferenceError

Source§

impl AsPyPointer for PyResourceWarning

Source§

impl AsPyPointer for PyRuntimeError

Source§

impl AsPyPointer for PyRuntimeWarning

Source§

impl AsPyPointer for PyStopAsyncIteration

Source§

impl AsPyPointer for PyStopIteration

Source§

impl AsPyPointer for PySyntaxError

Source§

impl AsPyPointer for PySyntaxWarning

Source§

impl AsPyPointer for PySystemError

Source§

impl AsPyPointer for PySystemExit

Source§

impl AsPyPointer for PyTimeoutError

Source§

impl AsPyPointer for PyTypeError

Source§

impl AsPyPointer for PyUnboundLocalError

Source§

impl AsPyPointer for PyUnicodeDecodeError

Source§

impl AsPyPointer for PyUnicodeEncodeError

Source§

impl AsPyPointer for PyUnicodeError

Source§

impl AsPyPointer for PyUnicodeTranslateError

Source§

impl AsPyPointer for PyUnicodeWarning

Source§

impl AsPyPointer for PyUserWarning

Source§

impl AsPyPointer for PyValueError

Source§

impl AsPyPointer for PyWarning

Source§

impl AsPyPointer for PyZeroDivisionError

Source§

impl AsPyPointer for PanicException

Source§

impl AsPyPointer for PyAny

Source§

impl AsPyPointer for PyBool

Source§

impl AsPyPointer for PyByteArray

Source§

impl AsPyPointer for PyBytes

Source§

impl AsPyPointer for PyCFunction

Source§

impl AsPyPointer for PyCapsule

Source§

impl AsPyPointer for PyCode

Source§

impl AsPyPointer for PyComplex

Source§

impl AsPyPointer for PyDate

Source§

impl AsPyPointer for PyDateTime

Source§

impl AsPyPointer for PyDelta

Source§

impl AsPyPointer for PyDict

Source§

impl AsPyPointer for PyDictItems

Source§

impl AsPyPointer for PyDictKeys

Source§

impl AsPyPointer for PyDictValues

Source§

impl AsPyPointer for PyEllipsis

Source§

impl AsPyPointer for PyFloat

Source§

impl AsPyPointer for PyFrame

Source§

impl AsPyPointer for PyFrozenSet

Source§

impl AsPyPointer for PyFunction

Source§

impl AsPyPointer for PyIterator

Source§

impl AsPyPointer for PyList

Source§

impl AsPyPointer for PyLong

Source§

impl AsPyPointer for PyMapping

Source§

impl AsPyPointer for PyMemoryView

Source§

impl AsPyPointer for PyModule

Source§

impl AsPyPointer for PyNone

Source§

impl AsPyPointer for PyNotImplemented

Source§

impl AsPyPointer for PySequence

Source§

impl AsPyPointer for PySet

Source§

impl AsPyPointer for PySlice

Source§

impl AsPyPointer for PyString

Source§

impl AsPyPointer for PySuper

Source§

impl AsPyPointer for PyTime

Source§

impl AsPyPointer for PyTraceback

Source§

impl AsPyPointer for PyTuple

Source§

impl AsPyPointer for PyType

Source§

impl AsPyPointer for PyTzInfo

Source§

impl AsPyPointer for PyWeakref

Source§

impl AsPyPointer for PyWeakrefProxy

Source§

impl AsPyPointer for PyWeakrefReference

Source§

impl<'a, T: PyClass> AsPyPointer for PyRef<'a, T>

Source§

impl<'a, T: PyClass<Frozen = False>> AsPyPointer for PyRefMut<'a, T>

Source§

impl<T> AsPyPointer for Bound<'_, T>

Source§

impl<T> AsPyPointer for Py<T>