PyO3 / pyo3

Rust bindings for the Python interpreter
https://pyo3.rs
Apache License 2.0
12.46k stars 769 forks source link

Missing `PySendResult` return type for `pyo3::ffi::PyIter_Send` #4727

Open francis-clairicia opened 5 days ago

francis-clairicia commented 5 days ago

Bug Description

I wanted to use PyIter_Send function for a generator and I can't have the function result:

https://github.com/PyO3/pyo3/blob/v0.23.1/pyo3-ffi/src/abstract_.rs#L123

Steps to Reproduce

An example which should work:

use pyo3::prelude::*;
use pyo3::ffi as pyo3_ffi;

let generator_module = PyModule::from_code(
    py,
    pyo3_ffi::c_str!(
        r#"
        def gen():
            val = yield 1
            yield val
    "#
    ),
    pyo3_ffi::c_str!("generator_module.py"),
    pyo3_ffi::c_str!("generator_module"),
)?;
let generator = generator_module.getattr("gen")?.call0()?;

unsafe {
    let presult: *mut pyo3_ffi::PyObject = std::ptr::null_mut();

    match pyo3_ffi::PyIter_Send(generator.as_ptr(), py.None().as_ptr(), &mut presult) {
        pyo3_ffi::PySendResult::PYGEN_RETURN => todo!(),
        pyo3_ffi::PySendResult::PYGEN_NEXT => todo!(),
        pyo3_ffi::PySendResult::PYGEN_ERROR => todo!(),
    }
}

Backtrace

No response

Your operating system and version

Manjaro Linux 24.1

Your Python version (python --version)

Python 3.11.10

Your Rust version (rustc --version)

rustc 1.82.0 (f6e511eec 2024-10-15)

Your PyO3 version

0.23.1

How did you install python? Did you use a virtualenv?

pyenv

Additional Info

No response

ngoldbaum commented 4 days ago

It looks like that header was never properly updated to capture the new API added to abstract.h in 3.10. PRs to improve the bindings are very welcome :)

It looks like PySendResult is just an integer enum so that should also be straightforward to wrap.