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§
Implementations on Foreign Types§
Source§impl<T> AsPyPointer for Option<T>where
T: AsPyPointer,
impl<T> AsPyPointer for Option<T>where
T: AsPyPointer,
Convert None
into a null pointer.
Implementors§
impl AsPyPointer for CancelledError
impl AsPyPointer for IncompleteReadError
impl AsPyPointer for InvalidStateError
impl AsPyPointer for LimitOverrunError
impl AsPyPointer for QueueEmpty
impl AsPyPointer for QueueFull
impl AsPyPointer for TimeoutError
impl AsPyPointer for gaierror
impl AsPyPointer for herror
impl AsPyPointer for timeout
impl AsPyPointer for PyArithmeticError
impl AsPyPointer for PyAssertionError
impl AsPyPointer for PyAttributeError
impl AsPyPointer for PyBaseException
impl AsPyPointer for PyBlockingIOError
impl AsPyPointer for PyBrokenPipeError
impl AsPyPointer for PyBufferError
impl AsPyPointer for PyBytesWarning
impl AsPyPointer for PyChildProcessError
impl AsPyPointer for PyConnectionAbortedError
impl AsPyPointer for PyConnectionError
impl AsPyPointer for PyConnectionRefusedError
impl AsPyPointer for PyConnectionResetError
impl AsPyPointer for PyDeprecationWarning
impl AsPyPointer for PyEOFError
impl AsPyPointer for PyEncodingWarning
impl AsPyPointer for PyEnvironmentError
impl AsPyPointer for PyException
impl AsPyPointer for PyFileExistsError
impl AsPyPointer for PyFileNotFoundError
impl AsPyPointer for PyFloatingPointError
impl AsPyPointer for PyFutureWarning
impl AsPyPointer for PyGeneratorExit
impl AsPyPointer for PyIOError
impl AsPyPointer for PyImportError
impl AsPyPointer for PyImportWarning
impl AsPyPointer for PyIndexError
impl AsPyPointer for PyInterruptedError
impl AsPyPointer for PyIsADirectoryError
impl AsPyPointer for PyKeyError
impl AsPyPointer for PyKeyboardInterrupt
impl AsPyPointer for PyLookupError
impl AsPyPointer for PyMemoryError
impl AsPyPointer for PyModuleNotFoundError
impl AsPyPointer for PyNameError
impl AsPyPointer for PyNotADirectoryError
impl AsPyPointer for PyNotImplementedError
impl AsPyPointer for PyOSError
impl AsPyPointer for PyOverflowError
impl AsPyPointer for PyPendingDeprecationWarning
impl AsPyPointer for PyPermissionError
impl AsPyPointer for PyProcessLookupError
impl AsPyPointer for PyRecursionError
impl AsPyPointer for PyReferenceError
impl AsPyPointer for PyResourceWarning
impl AsPyPointer for PyRuntimeError
impl AsPyPointer for PyRuntimeWarning
impl AsPyPointer for PyStopAsyncIteration
impl AsPyPointer for PyStopIteration
impl AsPyPointer for PySyntaxError
impl AsPyPointer for PySyntaxWarning
impl AsPyPointer for PySystemError
impl AsPyPointer for PySystemExit
impl AsPyPointer for PyTimeoutError
impl AsPyPointer for PyTypeError
impl AsPyPointer for PyUnboundLocalError
impl AsPyPointer for PyUnicodeDecodeError
impl AsPyPointer for PyUnicodeEncodeError
impl AsPyPointer for PyUnicodeError
impl AsPyPointer for PyUnicodeTranslateError
impl AsPyPointer for PyUnicodeWarning
impl AsPyPointer for PyUserWarning
impl AsPyPointer for PyValueError
impl AsPyPointer for PyWarning
impl AsPyPointer for PyZeroDivisionError
impl AsPyPointer for PanicException
impl AsPyPointer for PyAny
impl AsPyPointer for PyBool
impl AsPyPointer for PyByteArray
impl AsPyPointer for PyBytes
impl AsPyPointer for PyCFunction
impl AsPyPointer for PyCapsule
impl AsPyPointer for PyCode
Py_LIMITED_API
and non-PyPy
and non-GraalPy
only.impl AsPyPointer for PyComplex
impl AsPyPointer for PyDate
Py_LIMITED_API
only.impl AsPyPointer for PyDateTime
Py_LIMITED_API
only.impl AsPyPointer for PyDelta
Py_LIMITED_API
only.impl AsPyPointer for PyDict
impl AsPyPointer for PyDictItems
impl AsPyPointer for PyDictKeys
impl AsPyPointer for PyDictValues
impl AsPyPointer for PyEllipsis
impl AsPyPointer for PyFloat
impl AsPyPointer for PyFrame
Py_LIMITED_API
and non-PyPy
and non-GraalPy
only.impl AsPyPointer for PyFrozenSet
impl AsPyPointer for PyFunction
impl AsPyPointer for PyIterator
impl AsPyPointer for PyList
impl AsPyPointer for PyLong
impl AsPyPointer for PyMapping
impl AsPyPointer for PyMemoryView
impl AsPyPointer for PyModule
impl AsPyPointer for PyNone
impl AsPyPointer for PyNotImplemented
impl AsPyPointer for PySequence
impl AsPyPointer for PySet
impl AsPyPointer for PySlice
impl AsPyPointer for PyString
impl AsPyPointer for PySuper
PyPy
nor GraalPy
.impl AsPyPointer for PyTime
Py_LIMITED_API
only.impl AsPyPointer for PyTraceback
impl AsPyPointer for PyTuple
impl AsPyPointer for PyType
impl AsPyPointer for PyTzInfo
Py_LIMITED_API
only.impl AsPyPointer for PyWeakref
impl AsPyPointer for PyWeakrefProxy
impl AsPyPointer for PyWeakrefReference
impl<'a, T: PyClass> AsPyPointer for PyRef<'a, T>
impl<'a, T: PyClass<Frozen = False>> AsPyPointer for PyRefMut<'a, T>
impl<T> AsPyPointer for Bound<'_, T>
impl<T> AsPyPointer for Py<T>
impl<T: PyClass> AsPyPointer for PyCell<T>
gil-refs
only.