prefer inner / _private naming

This commit is contained in:
David Hewitt 2023-06-27 08:33:16 +01:00
parent 233fdd555e
commit bf2f441567
12 changed files with 58 additions and 56 deletions

View File

@ -119,7 +119,7 @@ pub fn process_functions_in_module(
let name = &func.sig.ident;
let statements: Vec<syn::Stmt> = syn::parse_quote! {
#wrapped_function
#module_name.add_function(#krate::impl_::pyfunction::wrap_pyfunction_impl(&#name::DEF, #module_name)?)?;
#module_name.add_function(#krate::impl_::pyfunction::_wrap_pyfunction(&#name::DEF, #module_name)?)?;
};
stmts.extend(statements);
}

View File

@ -420,7 +420,7 @@ fn impl_traverse_slot(cls: &syn::Type, spec: &FnSpec<'_>) -> syn::Result<MethodA
visit: _pyo3::ffi::visitproc,
arg: *mut ::std::os::raw::c_void,
) -> ::std::os::raw::c_int {
_pyo3::impl_::pymethods::call_traverse_impl::<#cls>(slf, #cls::#rust_fn_ident, visit, arg)
_pyo3::impl_::pymethods::_call_traverse::<#cls>(slf, #cls::#rust_fn_ident, visit, arg)
}
};
let slot_def = quote! {

View File

@ -469,7 +469,7 @@ impl<T: Element> PyBuffer<T> {
/// you can use `<T as buffer::Element>::is_compatible_format(buf.format())`.
/// Alternatively, `match buffer::ElementType::from_format(buf.format())`.
pub fn copy_to_slice(&self, py: Python<'_>, target: &mut [T]) -> PyResult<()> {
self.copy_to_slice_impl(py, target, b'C')
self._copy_to_slice(py, target, b'C')
}
/// Copies the buffer elements to the specified slice.
@ -482,10 +482,10 @@ impl<T: Element> PyBuffer<T> {
/// you can use `<T as buffer::Element>::is_compatible_format(buf.format())`.
/// Alternatively, `match buffer::ElementType::from_format(buf.format())`.
pub fn copy_to_fortran_slice(&self, py: Python<'_>, target: &mut [T]) -> PyResult<()> {
self.copy_to_slice_impl(py, target, b'F')
self._copy_to_slice(py, target, b'F')
}
fn copy_to_slice_impl(&self, py: Python<'_>, target: &mut [T], fort: u8) -> PyResult<()> {
fn _copy_to_slice(&self, py: Python<'_>, target: &mut [T], fort: u8) -> PyResult<()> {
if mem::size_of_val(target) != self.len_bytes() {
return Err(PyBufferError::new_err(format!(
"slice to copy to (of length {}) does not match buffer length of {}",
@ -516,7 +516,7 @@ impl<T: Element> PyBuffer<T> {
///
/// Fails if the buffer format is not compatible with type `T`.
pub fn to_vec(&self, py: Python<'_>) -> PyResult<Vec<T>> {
self.to_vec_impl(py, b'C')
self._to_vec(py, b'C')
}
/// Copies the buffer elements to a newly allocated vector.
@ -524,10 +524,10 @@ impl<T: Element> PyBuffer<T> {
///
/// Fails if the buffer format is not compatible with type `T`.
pub fn to_fortran_vec(&self, py: Python<'_>) -> PyResult<Vec<T>> {
self.to_vec_impl(py, b'F')
self._to_vec(py, b'F')
}
fn to_vec_impl(&self, py: Python<'_>, fort: u8) -> PyResult<Vec<T>> {
fn _to_vec(&self, py: Python<'_>, fort: u8) -> PyResult<Vec<T>> {
let item_count = self.item_count();
let mut vec: Vec<T> = Vec::with_capacity(item_count);
unsafe {
@ -564,7 +564,7 @@ impl<T: Element> PyBuffer<T> {
/// use `<T as buffer::Element>::is_compatible_format(buf.format())`.
/// Alternatively, `match buffer::ElementType::from_format(buf.format())`.
pub fn copy_from_slice(&self, py: Python<'_>, source: &[T]) -> PyResult<()> {
self.copy_from_slice_impl(py, source, b'C')
self._copy_from_slice(py, source, b'C')
}
/// Copies the specified slice into the buffer.
@ -578,10 +578,10 @@ impl<T: Element> PyBuffer<T> {
/// use `<T as buffer::Element>::is_compatible_format(buf.format())`.
/// Alternatively, `match buffer::ElementType::from_format(buf.format())`.
pub fn copy_from_fortran_slice(&self, py: Python<'_>, source: &[T]) -> PyResult<()> {
self.copy_from_slice_impl(py, source, b'F')
self._copy_from_slice(py, source, b'F')
}
fn copy_from_slice_impl(&self, py: Python<'_>, source: &[T], fort: u8) -> PyResult<()> {
fn _copy_from_slice(&self, py: Python<'_>, source: &[T], fort: u8) -> PyResult<()> {
if self.readonly() {
return Err(PyBufferError::new_err("cannot write to read-only buffer"));
} else if mem::size_of_val(source) != self.len_bytes() {

View File

@ -2,7 +2,7 @@ use crate::{derive_utils::PyFunctionArguments, types::PyCFunction, PyResult};
pub use crate::impl_::pymethods::PyMethodDef;
pub fn wrap_pyfunction_impl<'a>(
pub fn _wrap_pyfunction<'a>(
method_def: &PyMethodDef,
py_or_module: impl Into<PyFunctionArguments<'a>>,
) -> PyResult<&'a PyCFunction> {

View File

@ -247,7 +247,7 @@ impl PySetterDef {
/// Calls an implementation of __traverse__ for tp_traverse
#[doc(hidden)]
pub unsafe fn call_traverse_impl<T>(
pub unsafe fn _call_traverse<T>(
slf: *mut ffi::PyObject,
impl_: fn(&T, PyVisit<'_>) -> Result<(), PyTraverseError>,
visit: ffi::visitproc,

View File

@ -18,7 +18,7 @@ use crate::{
pub unsafe fn module_init(
f: for<'py> unsafe fn(Python<'py>) -> PyResult<Py<PyModule>>,
) -> *mut ffi::PyObject {
trampoline_inner(|py| f(py).map(|module| module.into_ptr()))
trampoline(|py| f(py).map(|module| module.into_ptr()))
}
#[inline]
@ -28,7 +28,7 @@ pub unsafe fn noargs(
f: for<'py> unsafe fn(Python<'py>, *mut ffi::PyObject) -> PyResult<*mut ffi::PyObject>,
) -> *mut ffi::PyObject {
debug_assert!(args.is_null());
trampoline_inner(|py| f(py, slf))
trampoline(|py| f(py, slf))
}
macro_rules! trampoline {
@ -38,7 +38,7 @@ macro_rules! trampoline {
$($arg_names: $arg_types,)*
f: for<'py> unsafe fn (Python<'py>, $($arg_types),*) -> PyResult<$ret>,
) -> $ret {
trampoline_inner(|py| f(py, $($arg_names,)*))
trampoline(|py| f(py, $($arg_names,)*))
}
}
}
@ -131,7 +131,7 @@ pub unsafe fn releasebufferproc(
buf: *mut ffi::Py_buffer,
f: for<'py> unsafe fn(Python<'py>, *mut ffi::PyObject, *mut ffi::Py_buffer) -> PyResult<()>,
) {
trampoline_inner_unraisable(|py| f(py, slf, buf), slf)
trampoline_unraisable(|py| f(py, slf, buf), slf)
}
#[inline]
@ -143,7 +143,7 @@ pub(crate) unsafe fn dealloc(
// so pass null_mut() to the context.
//
// (Note that we don't allow the implementation `f` to fail.)
trampoline_inner_unraisable(
trampoline_unraisable(
|py| {
f(py, slf);
Ok(())
@ -168,7 +168,7 @@ trampoline!(
/// Panics during execution are trapped so that they don't propagate through any
/// outer FFI boundary.
#[inline]
pub(crate) fn trampoline_inner<F, R>(body: F) -> R
pub(crate) fn trampoline<F, R>(body: F) -> R
where
F: for<'py> FnOnce(Python<'py>) -> PyResult<R> + UnwindSafe,
R: PyCallbackOutput,
@ -214,7 +214,7 @@ where
///
/// ctx must be either a valid ffi::PyObject or NULL
#[inline]
unsafe fn trampoline_inner_unraisable<F>(body: F, ctx: *mut ffi::PyObject)
unsafe fn trampoline_unraisable<F>(body: F, ctx: *mut ffi::PyObject)
where
F: for<'py> FnOnce(Python<'py>) -> PyResult<()> + UnwindSafe,
{

View File

@ -125,12 +125,12 @@ macro_rules! wrap_pyfunction {
($function:path) => {
&|py_or_module| {
use $function as wrapped_pyfunction;
$crate::impl_::pyfunction::wrap_pyfunction_impl(&wrapped_pyfunction::DEF, py_or_module)
$crate::impl_::pyfunction::_wrap_pyfunction(&wrapped_pyfunction::DEF, py_or_module)
}
};
($function:path, $py_or_module:expr) => {{
use $function as wrapped_pyfunction;
$crate::impl_::pyfunction::wrap_pyfunction_impl(&wrapped_pyfunction::DEF, $py_or_module)
$crate::impl_::pyfunction::_wrap_pyfunction(&wrapped_pyfunction::DEF, $py_or_module)
}};
}

View File

@ -7,7 +7,7 @@ use crate::{
},
impl_::{
pymethods::{get_doc, get_name, Getter, Setter},
trampoline::trampoline_inner,
trampoline::trampoline,
},
types::PyType,
Py, PyClass, PyGetterDef, PyMethodDefType, PyResult, PySetterDef, PyTypeInfo, Python,
@ -413,7 +413,7 @@ unsafe extern "C" fn no_constructor_defined(
_args: *mut ffi::PyObject,
_kwds: *mut ffi::PyObject,
) -> *mut ffi::PyObject {
trampoline_inner(|_| {
trampoline(|_| {
Err(crate::exceptions::PyTypeError::new_err(
"No constructor defined",
))
@ -513,7 +513,7 @@ impl GetSetDefType {
) -> *mut ffi::PyObject {
// Safety: PyO3 sets the closure when constructing the ffi getter so this cast should always be valid
let getter: Getter = std::mem::transmute(closure);
trampoline_inner(|py| getter(py, slf))
trampoline(|py| getter(py, slf))
}
(Some(getter), None, closure as Getter as _)
}
@ -525,7 +525,7 @@ impl GetSetDefType {
) -> c_int {
// Safety: PyO3 sets the closure when constructing the ffi setter so this cast should always be valid
let setter: Setter = std::mem::transmute(closure);
trampoline_inner(|py| setter(py, slf, value))
trampoline(|py| setter(py, slf, value))
}
(None, Some(setter), closure as Setter as _)
}
@ -535,7 +535,7 @@ impl GetSetDefType {
closure: *mut c_void,
) -> *mut ffi::PyObject {
let getset: &GetterAndSetter = &*(closure as *const GetterAndSetter);
trampoline_inner(|py| (getset.getter)(py, slf))
trampoline(|py| (getset.getter)(py, slf))
}
unsafe extern "C" fn getset_setter(
@ -544,7 +544,7 @@ impl GetSetDefType {
closure: *mut c_void,
) -> c_int {
let getset: &GetterAndSetter = &*(closure as *const GetterAndSetter);
trampoline_inner(|py| (getset.setter)(py, slf, value))
trampoline(|py| (getset.setter)(py, slf, value))
}
(
Some(getset_getter),

View File

@ -142,17 +142,20 @@ impl PyDict {
where
K: ToPyObject,
{
self.get_item_impl(key.to_object(self.py()))
}
fn get_item_impl(&self, key: PyObject) -> Option<&PyAny> {
let py = self.py();
unsafe {
let ptr = ffi::PyDict_GetItem(self.as_ptr(), key.as_ptr());
fn inner(dict: &PyDict, key: PyObject) -> Option<&PyAny> {
let py = dict.py();
// PyDict_GetItem returns a borrowed ptr, must make it owned for safety (see #890).
// PyObject::from_borrowed_ptr_or_opt will take ownership in this way.
PyObject::from_borrowed_ptr_or_opt(py, ptr).map(|pyobject| pyobject.into_ref(py))
unsafe {
PyObject::from_borrowed_ptr_or_opt(
py,
ffi::PyDict_GetItem(dict.as_ptr(), key.as_ptr()),
)
}
.map(|pyobject| pyobject.into_ref(py))
}
inner(self, key.to_object(self.py()))
}
/// Gets an item from the dictionary,
@ -164,20 +167,22 @@ impl PyDict {
where
K: ToPyObject,
{
self.get_item_with_error_impl(key.to_object(self.py()))
}
fn get_item_with_error_impl(&self, key: PyObject) -> PyResult<Option<&PyAny>> {
let py = self.py();
unsafe {
let ptr = ffi::PyDict_GetItemWithError(self.as_ptr(), key.as_ptr());
fn inner(dict: &PyDict, key: PyObject) -> PyResult<Option<&PyAny>> {
let py = dict.py();
// PyDict_GetItemWithError returns a borrowed ptr, must make it owned for safety (see #890).
// PyObject::from_borrowed_ptr_or_opt will take ownership in this way.
PyObject::from_borrowed_ptr_or_opt(py, ptr)
.map(|pyobject| Ok(pyobject.into_ref(py)))
.or_else(|| PyErr::take(py).map(Err))
.transpose()
unsafe {
PyObject::from_borrowed_ptr_or_opt(
py,
ffi::PyDict_GetItemWithError(dict.as_ptr(), key.as_ptr()),
)
}
.map(|pyobject| Ok(pyobject.into_ref(py)))
.or_else(|| PyErr::take(py).map(Err))
.transpose()
}
inner(self, key.to_object(self.py()))
}
/// Sets an item value.

View File

@ -197,7 +197,7 @@ pub(crate) fn new_from_iter<T: ToPyObject>(
py: Python<'_>,
elements: impl IntoIterator<Item = T>,
) -> PyResult<Py<PyFrozenSet>> {
fn new_from_iter_inner(
fn inner(
py: Python<'_>,
elements: &mut dyn Iterator<Item = PyObject>,
) -> PyResult<Py<PyFrozenSet>> {
@ -217,7 +217,7 @@ pub(crate) fn new_from_iter<T: ToPyObject>(
}
let mut iter = elements.into_iter().map(|e| e.to_object(py));
new_from_iter_inner(py, &mut iter)
inner(py, &mut iter)
}
#[cfg(test)]

View File

@ -248,10 +248,7 @@ pub(crate) fn new_from_iter<T: ToPyObject>(
py: Python<'_>,
elements: impl IntoIterator<Item = T>,
) -> PyResult<Py<PySet>> {
fn new_from_iter_inner(
py: Python<'_>,
elements: &mut dyn Iterator<Item = PyObject>,
) -> PyResult<Py<PySet>> {
fn inner(py: Python<'_>, elements: &mut dyn Iterator<Item = PyObject>) -> PyResult<Py<PySet>> {
let set: Py<PySet> = unsafe {
// We create the `Py` pointer because its Drop cleans up the set if user code panics.
Py::from_owned_ptr_or_err(py, ffi::PySet_New(std::ptr::null_mut()))?
@ -268,7 +265,7 @@ pub(crate) fn new_from_iter<T: ToPyObject>(
}
let mut iter = elements.into_iter().map(|e| e.to_object(py));
new_from_iter_inner(py, &mut iter)
inner(py, &mut iter)
}
#[cfg(test)]

View File

@ -19,5 +19,5 @@ error[E0308]: mismatched types
note: function defined here
--> src/impl_/pymethods.rs
|
| pub unsafe fn call_traverse_impl<T>(
| ^^^^^^^^^^^^^^^^^^
| pub unsafe fn _call_traverse<T>(
| ^^^^^^^^^^^^^^