-
Notifications
You must be signed in to change notification settings - Fork 782
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
0.21.0-beta.0: can't extract to Vec<Cow<'_, [u8]>> without gil-refs feature #3990
Comments
I think the additional impl<'py, T> FromPyObjectBound<'_, 'py> for Vec<T>
where
T: FromPyObjectBound<'_, 'py> which is not provided by the blanket impl based on In your case, could you try extract the |
We could provide those impls but it would be a bit of a game of whack-a-mole as the same reasoning applies to e.g. |
Of course, this does not work as the new error[E0515]: cannot return value referencing function parameter `item`
--> src/lib.rs:11:62
|
11 | let _value: Vec<Cow<'_, [u8]>> = x.iter().map(|item| item.extract()).collect::<PyResult<_>>()?;
| ----^^^^^^^^^^
| |
| returns a value referencing data owned by the current function
| `item` is borrowed here happens. So I suspect this will require |
So indeed use pyo3::prelude::*;
use pyo3::pybacked::PyBackedBytes;
use pyo3::types::PyList;
#[pyclass]
struct Foo {}
#[pymethods]
impl Foo {
fn bar(&self, x: Bound<'_, PyList>) -> PyResult<()> {
let _value: Vec<PyBackedBytes> = x.extract()?;
Ok(())
}
} should be the closest thing to the previous pattern you can do without the GIL pool. |
I agree with the above, probably the documentation could do better to emphasise this case has changed. In general the additional It might be that we choose never to have |
Thanks, I wasn't aware of PyBackedBytes! It sounds useful. I can probably write code to accept @adamreichold in your example, are the PyBackedBytes objects backed by the @davidhewitt should I leave this open while the documentation is figured out? |
Sure thing, yes.
Reading this I immediately wonder if we should implement |
I'm still fairly new to Rust and I'm still figuring out when to use AsRef vs Deref vs Borrow, but that sounds good to me. |
The clone only a reference to the |
I agree that as a bound |
Agreed both implementations are reasonable here; I wasn't thinking to remove the Deref implementation. |
Bug Description
Even though I'm using the new Bound API (in a new project), it seems I have to enable the gil-refs feature for
Bound::extract::<Vec<Cow<'_, [u8]>>>
to compile. I don't know if that's expected, but it was surprising to me, since the documentation led me to believe the feature should only be needed for backwards compatibility.Steps to Reproduce
In src/lib.rs:
In Cargo.toml:
Run
cargo build
.Output:
Backtrace
No response
Your operating system and version
Ubuntu 22.04
Your Python version (
python --version
)3.10.12
Your Rust version (
rustc --version
)rustc 1.75.0 (82e1608df 2023-12-21)
Your PyO3 version
0.21.0-beta.0
How did you install python? Did you use a virtualenv?
apt
Additional Info
When enabling the gil-refs feature, the sample code compiles without warnings.
I should add that I'm using
Vec<Cow<'_, [u8]>>
because it's the natural fit for the Rust code I'm trying to bind, rather than expecting it to borrow the memory from thebytes
objects stored in the PyList. I can imagine that might not be practical without using a GILPool to keep them alive.The text was updated successfully, but these errors were encountered: