Trait pyo3::conversion::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 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<'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>