Skip to content

Commit

Permalink
Merge pull request #2144 from davidhewitt/cleanup-ffi-call
Browse files Browse the repository at this point in the history
chore: cleanup old todo
  • Loading branch information
davidhewitt authored Feb 5, 2022
2 parents abd7eaf + 3a70406 commit 02b7f99
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
24 changes: 19 additions & 5 deletions src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -580,12 +580,10 @@ impl<T> Py<T> {
/// This is equivalent to the Python expression `self()`.
pub fn call0(&self, py: Python) -> PyResult<PyObject> {
cfg_if::cfg_if! {
// TODO: Use PyObject_CallNoArgs instead after https://bugs.python.org/issue42415.
// Once the issue is resolved, we can enable this optimization for limited API.
if #[cfg(all(Py_3_9, not(Py_LIMITED_API)))] {
if #[cfg(Py_3_9)] {
// Optimized path on python 3.9+
unsafe {
PyObject::from_owned_ptr_or_err(py, ffi::_PyObject_CallNoArg(self.as_ptr()))
PyObject::from_owned_ptr_or_err(py, ffi::PyObject_CallNoArgs(self.as_ptr()))
}
} else {
self.call(py, (), None)
Expand Down Expand Up @@ -923,7 +921,23 @@ impl PyObject {
mod tests {
use super::{Py, PyObject};
use crate::types::PyDict;
use crate::Python;
use crate::{Python, ToPyObject};

#[test]
fn test_call0() {
Python::with_gil(|py| {
let obj = py.get_type::<PyDict>().to_object(py);
assert_eq!(
obj.call0(py)
.unwrap()
.as_ref(py)
.repr()
.unwrap()
.to_string_lossy(),
"{}"
);
})
}

#[test]
fn test_call_for_non_existing_method() {
Expand Down
6 changes: 2 additions & 4 deletions src/types/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,12 +333,10 @@ impl PyAny {
/// This is equivalent to the Python expression `help()`.
pub fn call0(&self) -> PyResult<&PyAny> {
cfg_if::cfg_if! {
// TODO: Use PyObject_CallNoArgs instead after https://bugs.python.org/issue42415.
// Once the issue is resolved, we can enable this optimization for limited API.
if #[cfg(all(Py_3_9, not(Py_LIMITED_API)))] {
if #[cfg(Py_3_9)] {
// Optimized path on python 3.9+
unsafe {
self.py().from_owned_ptr_or_err(ffi::_PyObject_CallNoArg(self.as_ptr()))
self.py().from_owned_ptr_or_err(ffi::PyObject_CallNoArgs(self.as_ptr()))
}
} else {
self.call((), None)
Expand Down

0 comments on commit 02b7f99

Please sign in to comment.