Skip to content

Commit

Permalink
ffi: add compat functions for no-argument calls (#4461)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhewitt committed Aug 21, 2024
1 parent 8413890 commit 5cc23d9
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 26 deletions.
1 change: 1 addition & 0 deletions newsfragments/4461.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add FFI definitions `compat::PyObject_CallNoArgs` and `compat::PyObject_CallMethodNoArgs`.
2 changes: 2 additions & 0 deletions pyo3-ffi/src/compat/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ macro_rules! compat_function {

mod py_3_10;
mod py_3_13;
mod py_3_9;

pub use self::py_3_10::*;
pub use self::py_3_13::*;
pub use self::py_3_9::*;
21 changes: 21 additions & 0 deletions pyo3-ffi/src/compat/py_3_9.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
compat_function!(
originally_defined_for(all(
not(PyPy),
not(GraalPy),
any(Py_3_10, all(not(Py_LIMITED_API), Py_3_9)) // Added to python in 3.9 but to limited API in 3.10
));

#[inline]
pub unsafe fn PyObject_CallNoArgs(obj: *mut crate::PyObject) -> *mut crate::PyObject {
crate::PyObject_CallObject(obj, std::ptr::null_mut())
}
);

compat_function!(
originally_defined_for(all(Py_3_9, not(any(Py_LIMITED_API, PyPy, GraalPy))));

#[inline]
pub unsafe fn PyObject_CallMethodNoArgs(obj: *mut crate::PyObject, name: *mut crate::PyObject) -> *mut crate::PyObject {
crate::PyObject_CallMethodObjArgs(obj, name, std::ptr::null_mut::<crate::PyObject>())
}
);
32 changes: 6 additions & 26 deletions src/types/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1180,20 +1180,7 @@ impl<'py> PyAnyMethods<'py> for Bound<'py, PyAny> {
}

fn call0(&self) -> PyResult<Bound<'py, PyAny>> {
cfg_if::cfg_if! {
if #[cfg(all(
not(PyPy),
not(GraalPy),
any(Py_3_10, all(not(Py_LIMITED_API), Py_3_9)) // PyObject_CallNoArgs was added to python in 3.9 but to limited API in 3.10
))] {
// Optimized path on python 3.9+
unsafe {
ffi::PyObject_CallNoArgs(self.as_ptr()).assume_owned_or_err(self.py())
}
} else {
self.call((), None)
}
}
unsafe { ffi::compat::PyObject_CallNoArgs(self.as_ptr()).assume_owned_or_err(self.py()) }
}

fn call1(&self, args: impl IntoPy<Py<PyTuple>>) -> PyResult<Bound<'py, PyAny>> {
Expand All @@ -1218,18 +1205,11 @@ impl<'py> PyAnyMethods<'py> for Bound<'py, PyAny> {
where
N: IntoPy<Py<PyString>>,
{
cfg_if::cfg_if! {
if #[cfg(all(Py_3_9, not(any(Py_LIMITED_API, PyPy, GraalPy))))] {
let py = self.py();

// Optimized path on python 3.9+
unsafe {
let name = name.into_py(py).into_bound(py);
ffi::PyObject_CallMethodNoArgs(self.as_ptr(), name.as_ptr()).assume_owned_or_err(py)
}
} else {
self.call_method(name, (), None)
}
let py = self.py();
let name = name.into_py(py).into_bound(py);
unsafe {
ffi::compat::PyObject_CallMethodNoArgs(self.as_ptr(), name.as_ptr())
.assume_owned_or_err(py)
}
}

Expand Down

0 comments on commit 5cc23d9

Please sign in to comment.