Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ffi: add compat functions for no-argument calls #4461

Merged
merged 1 commit into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading