Skip to content

Commit

Permalink
add as_ptr and into_ptr inherent methods
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhewitt committed Aug 11, 2023
1 parent 1df7270 commit 64adab1
Show file tree
Hide file tree
Showing 56 changed files with 199 additions and 94 deletions.
1 change: 0 additions & 1 deletion guide/src/class.md
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,6 @@ that inherit native types. Even in such cases, you can unsafely get a base class
# #[cfg(not(Py_LIMITED_API))] {
# use pyo3::prelude::*;
use pyo3::types::PyDict;
use pyo3::AsPyPointer;
use std::collections::HashMap;

#[pyclass(extends=PyDict)]
Expand Down
1 change: 0 additions & 1 deletion guide/src/class/numeric.md
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,6 @@ Let's create that helper function. The signature has to be `fn(&PyAny) -> PyResu
use std::os::raw::c_ulong;
use pyo3::prelude::*;
use pyo3::ffi;
use pyo3::conversion::AsPyPointer;
fn wrap(obj: &PyAny) -> Result<i32, PyErr> {
let py: Python<'_> = obj.py();
Expand Down
1 change: 1 addition & 0 deletions newsfragments/3359.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add `as_ptr` and `into_ptr` inherent methods for `Py`, `PyAny`, `PyRef`, and `PyRefMut`.
2 changes: 1 addition & 1 deletion pyo3-macros-backend/src/quotes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ pub(crate) fn ok_wrap(obj: TokenStream) -> TokenStream {

pub(crate) fn map_result_into_ptr(result: TokenStream) -> TokenStream {
quote! {
#result.map(_pyo3::IntoPyPointer::into_ptr)
#result.map(_pyo3::PyObject::into_ptr)
}
}
4 changes: 1 addition & 3 deletions src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@
// DEALINGS IN THE SOFTWARE.

//! `PyBuffer` implementation
use crate::{
err, exceptions::PyBufferError, ffi, AsPyPointer, FromPyObject, PyAny, PyResult, Python,
};
use crate::{err, exceptions::PyBufferError, ffi, FromPyObject, PyAny, PyResult, Python};
use std::marker::PhantomData;
use std::os::raw;
use std::pin::Pin;
Expand Down
1 change: 0 additions & 1 deletion src/callback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
use crate::err::{PyErr, PyResult};
use crate::exceptions::PyOverflowError;
use crate::ffi::{self, Py_hash_t};
use crate::IntoPyPointer;
use crate::{IntoPy, PyObject, Python};
use std::isize;
use std::os::raw::c_int;
Expand Down
5 changes: 2 additions & 3 deletions src/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ use std::ptr::NonNull;
///
/// ```rust
/// use pyo3::prelude::*;
/// use pyo3::AsPyPointer;
/// use pyo3::types::PyString;
/// use pyo3::ffi;
///
Expand All @@ -40,7 +39,6 @@ use std::ptr::NonNull;
///
/// ```rust,no_run
/// # use pyo3::prelude::*;
/// # use pyo3::AsPyPointer;
/// # use pyo3::ffi;
/// #
/// Python::with_gil(|py| {
Expand Down Expand Up @@ -632,7 +630,7 @@ mod test_no_clone {}
#[cfg(test)]
mod tests {
use crate::types::{IntoPyDict, PyAny, PyDict, PyList};
use crate::{AsPyPointer, PyObject, Python, ToPyObject};
use crate::{PyObject, Python, ToPyObject};

use super::PyTryFrom;

Expand Down Expand Up @@ -676,6 +674,7 @@ mod tests {
#[test]
fn test_option_as_ptr() {
Python::with_gil(|py| {
use crate::AsPyPointer;
let mut option: Option<PyObject> = None;
assert_eq!(option.as_ptr(), std::ptr::null_mut());

Expand Down
2 changes: 1 addition & 1 deletion src/conversions/chrono.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ use crate::types::{
timezone_utc, PyDate, PyDateAccess, PyDateTime, PyDelta, PyDeltaAccess, PyTime, PyTimeAccess,
PyTzInfo, PyTzInfoAccess, PyUnicode,
};
use crate::{AsPyPointer, FromPyObject, IntoPy, PyAny, PyObject, PyResult, Python, ToPyObject};
use crate::{FromPyObject, IntoPy, PyAny, PyObject, PyResult, Python, ToPyObject};
use chrono::offset::{FixedOffset, Utc};
use chrono::{
DateTime, Datelike, Duration, NaiveDate, NaiveDateTime, NaiveTime, Offset, TimeZone, Timelike,
Expand Down
3 changes: 1 addition & 2 deletions src/conversions/num_bigint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@
//! ```

use crate::{
ffi, types::*, AsPyPointer, FromPyObject, IntoPy, Py, PyAny, PyObject, PyResult, Python,
ToPyObject,
ffi, types::*, FromPyObject, IntoPy, Py, PyAny, PyObject, PyResult, Python, ToPyObject,
};

use num_bigint::{BigInt, BigUint};
Expand Down
3 changes: 1 addition & 2 deletions src/conversions/num_complex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,7 @@
//! assert result == [complex(1,-1), complex(-2,0)]
//! ```
use crate::{
ffi, types::PyComplex, AsPyPointer, FromPyObject, PyAny, PyErr, PyObject, PyResult, Python,
ToPyObject,
ffi, types::PyComplex, FromPyObject, PyAny, PyErr, PyObject, PyResult, Python, ToPyObject,
};
use num_complex::Complex;
use std::os::raw::c_double;
Expand Down
1 change: 0 additions & 1 deletion src/conversions/std/array.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::conversion::{AsPyPointer, IntoPyPointer};
use crate::types::PySequence;
use crate::{exceptions, PyErr};
use crate::{
Expand Down
3 changes: 1 addition & 2 deletions src/conversions/std/num.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#[cfg(feature = "experimental-inspect")]
use crate::inspect::types::TypeInfo;
use crate::{
exceptions, ffi, AsPyPointer, FromPyObject, IntoPy, PyAny, PyErr, PyObject, PyResult, Python,
ToPyObject,
exceptions, ffi, FromPyObject, IntoPy, PyAny, PyErr, PyObject, PyResult, Python, ToPyObject,
};
use std::convert::TryFrom;
use std::num::{
Expand Down
4 changes: 1 addition & 3 deletions src/conversions/std/osstr.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use crate::types::PyString;
use crate::{
ffi, AsPyPointer, FromPyObject, IntoPy, PyAny, PyObject, PyResult, Python, ToPyObject,
};
use crate::{ffi, FromPyObject, IntoPy, PyAny, PyObject, PyResult, Python, ToPyObject};
use std::borrow::Cow;
use std::ffi::{OsStr, OsString};
#[cfg(not(windows))]
Expand Down
3 changes: 1 addition & 2 deletions src/conversions/std/path.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::{
ffi, AsPyPointer, FromPyObject, FromPyPointer, IntoPy, PyAny, PyObject, PyResult, Python,
ToPyObject,
ffi, FromPyObject, FromPyPointer, IntoPy, PyAny, PyObject, PyResult, Python, ToPyObject,
};
use std::borrow::Cow;
use std::ffi::OsString;
Expand Down
14 changes: 11 additions & 3 deletions src/err/err_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{
exceptions::{PyBaseException, PyTypeError},
ffi,
types::{PyTraceback, PyType},
AsPyPointer, IntoPy, IntoPyPointer, Py, PyAny, PyObject, PyTypeInfo, Python,
IntoPy, Py, PyAny, PyObject, PyTypeInfo, Python,
};

#[derive(Clone)]
Expand Down Expand Up @@ -118,12 +118,20 @@ impl PyErrState {
ptype,
pvalue,
ptraceback,
} => (ptype.into_ptr(), pvalue.into_ptr(), ptraceback.into_ptr()),
} => (
ptype.into_ptr(),
pvalue.map_or(std::ptr::null_mut(), Py::into_ptr),
ptraceback.map_or(std::ptr::null_mut(), Py::into_ptr),
),
PyErrState::Normalized(PyErrStateNormalized {
ptype,
pvalue,
ptraceback,
}) => (ptype.into_ptr(), pvalue.into_ptr(), ptraceback.into_ptr()),
}) => (
ptype.into_ptr(),
pvalue.into_ptr(),
ptraceback.map_or(std::ptr::null_mut(), Py::into_ptr),
),
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/err/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
exceptions::{self, PyBaseException},
ffi,
};
use crate::{AsPyPointer, IntoPy, IntoPyPointer, Py, PyAny, PyObject, Python, ToPyObject};
use crate::{IntoPy, Py, PyAny, PyObject, Python, ToPyObject};
use std::borrow::Cow;
use std::cell::UnsafeCell;
use std::ffi::CString;
Expand Down Expand Up @@ -443,7 +443,7 @@ impl PyErr {
self.get_type(py).as_ptr(),
self.value(py).as_ptr(),
self.traceback(py)
.map_or(std::ptr::null_mut(), PyTraceback::as_ptr),
.map_or(std::ptr::null_mut(), |traceback| traceback.as_ptr()),
)
}
}
Expand Down Expand Up @@ -642,7 +642,7 @@ impl PyErr {
// PyException_SetCause _steals_ a reference to cause, so must use .into_ptr()
ffi::PyException_SetCause(
value.as_ptr(),
cause.map_or(std::ptr::null_mut(), IntoPyPointer::into_ptr),
cause.map_or(std::ptr::null_mut(), Py::into_ptr),
);
}
}
Expand Down
3 changes: 0 additions & 3 deletions src/exceptions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ macro_rules! impl_exception_boilerplate {
impl ::std::error::Error for $name {
fn source(&self) -> ::std::option::Option<&(dyn ::std::error::Error + 'static)> {
unsafe {
use $crate::AsPyPointer;
let cause: &$crate::exceptions::PyBaseException = self
.py()
.from_owned_ptr_or_opt($crate::ffi::PyException_GetCause(self.as_ptr()))?;
Expand Down Expand Up @@ -101,7 +100,6 @@ macro_rules! import_exception {
impl $name {
fn type_object_raw(py: $crate::Python<'_>) -> *mut $crate::ffi::PyTypeObject {
use $crate::sync::GILOnceCell;
use $crate::AsPyPointer;
static TYPE_OBJECT: GILOnceCell<$crate::Py<$crate::types::PyType>> =
GILOnceCell::new();

Expand Down Expand Up @@ -240,7 +238,6 @@ macro_rules! create_exception_type_object {
impl $name {
fn type_object_raw(py: $crate::Python<'_>) -> *mut $crate::ffi::PyTypeObject {
use $crate::sync::GILOnceCell;
use $crate::AsPyPointer;
static TYPE_OBJECT: GILOnceCell<$crate::Py<$crate::types::PyType>> =
GILOnceCell::new();

Expand Down
4 changes: 2 additions & 2 deletions src/ffi/tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::ffi::*;
use crate::{AsPyPointer, Python};
use crate::Python;

#[cfg(not(Py_LIMITED_API))]
use crate::{
Expand Down Expand Up @@ -256,7 +256,7 @@ fn test_get_tzinfo() {

crate::Python::with_gil(|py| {
use crate::types::{PyDateTime, PyTime};
use crate::{AsPyPointer, PyAny};
use crate::PyAny;

let utc = timezone_utc(py);

Expand Down
2 changes: 1 addition & 1 deletion src/gil.rs
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ fn decrement_gil_count() {
#[cfg(test)]
mod tests {
use super::{gil_is_acquired, GILPool, GIL_COUNT, OWNED_OBJECTS, POOL};
use crate::{ffi, gil, AsPyPointer, IntoPyPointer, PyObject, Python, ToPyObject};
use crate::{ffi, gil, PyObject, Python, ToPyObject};
#[cfg(not(target_arch = "wasm32"))]
use parking_lot::{const_mutex, Condvar, Mutex};
use std::ptr::NonNull;
Expand Down
2 changes: 1 addition & 1 deletion src/impl_/extract_argument.rs
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,7 @@ fn push_parameter_list(msg: &mut String, parameter_names: &[&str]) {
mod tests {
use crate::{
types::{IntoPyDict, PyTuple},
AsPyPointer, PyAny, Python, ToPyObject,
PyAny, Python, ToPyObject,
};

use super::{push_parameter_list, FunctionDescription, NoVarargs, NoVarkeywords};
Expand Down
2 changes: 1 addition & 1 deletion src/impl_/pyclass/lazy_type_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
pyclass::{create_type_object, PyClassTypeObject},
sync::{GILOnceCell, GILProtected},
types::PyType,
AsPyPointer, IntoPyPointer, PyClass, PyErr, PyMethodDefType, PyObject, PyResult, Python,
PyClass, PyErr, PyMethodDefType, PyObject, PyResult, Python,
};

use super::PyClassItemsIter;
Expand Down
2 changes: 1 addition & 1 deletion src/impl_/trampoline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::{

use crate::{
callback::PyCallbackOutput, ffi, impl_::panic::PanicTrap, methods::IPowModulo,
panic::PanicException, types::PyModule, GILPool, IntoPyPointer, Py, PyResult, Python,
panic::PanicException, types::PyModule, GILPool, Py, PyResult, Python,
};

#[inline]
Expand Down
30 changes: 29 additions & 1 deletion src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,34 @@ where
}
}

impl<T> Py<T> {
/// Returns the raw FFI pointer represented by self.
///
/// # Safety
///
/// Callers are responsible for ensuring that the pointer does not outlive self.
///
/// The reference is borrowed; callers should not decrease the reference count
/// when they are finished with the pointer.
#[inline]
pub fn as_ptr(&self) -> *mut ffi::PyObject {
self.0.as_ptr()
}

/// Returns an owned raw FFI pointer represented by self.
///
/// # Safety
///
/// The reference is owned; when finished the caller should either transfer ownership
/// of the pointer or decrease the reference count (e.g. with [`pyo3::ffi::Py_DecRef`](crate::ffi::Py_DecRef)).
#[inline]
pub fn into_ptr(self) -> *mut ffi::PyObject {
let ptr = self.0.as_ptr();
std::mem::forget(self);
ptr
}
}

impl<T> Py<T>
where
T: PyClass,
Expand Down Expand Up @@ -897,7 +925,7 @@ impl<T> IntoPy<PyObject> for Py<T> {
}
}

impl<T> AsPyPointer for Py<T> {
impl<T> crate::AsPyPointer for Py<T> {
/// Gets the underlying FFI pointer, returns a borrowed pointer.
#[inline]
fn as_ptr(&self) -> *mut ffi::PyObject {
Expand Down
9 changes: 3 additions & 6 deletions src/marker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,7 @@ use crate::gil::{GILGuard, GILPool, SuspendGIL};
use crate::impl_::not_send::NotSend;
use crate::types::{PyAny, PyDict, PyModule, PyString, PyType};
use crate::version::PythonVersionInfo;
use crate::{
ffi, AsPyPointer, FromPyPointer, IntoPy, IntoPyPointer, Py, PyNativeType, PyObject, PyTryFrom,
PyTypeInfo,
};
use crate::{ffi, FromPyPointer, IntoPy, Py, PyNativeType, PyObject, PyTryFrom, PyTypeInfo};
use std::ffi::{CStr, CString};
use std::marker::PhantomData;
use std::os::raw::c_int;
Expand Down Expand Up @@ -628,9 +625,9 @@ impl<'py> Python<'py> {
}

let globals = globals
.map(AsPyPointer::as_ptr)
.map(|dict| dict.as_ptr())
.unwrap_or_else(|| ffi::PyModule_GetDict(mptr));
let locals = locals.map(AsPyPointer::as_ptr).unwrap_or(globals);
let locals = locals.map(|dict| dict.as_ptr()).unwrap_or(globals);

let code_obj = ffi::Py_CompileString(code.as_ptr(), "<string>\0".as_ptr() as _, start);
if code_obj.is_null() {
Expand Down
Loading

0 comments on commit 64adab1

Please sign in to comment.