-
Notifications
You must be signed in to change notification settings - Fork 784
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
Trouble using PyContextProtocol #1205
Comments
Thanks, yes this is sadly a flaw with the |
For now, I'm happy to write a temporary solution (i.e. allow |
I think what I have in mind is non-breaking and not too hard so I can do over the weekend and we can potentially even release in a 0.12.2. |
I started to implemented this type then quickly found myself blocked on PyO3/pyo3#1205 / PyO3/pyo3#1206 due to not being able to return `Self` from `__enter__`. We'll have to wait for a future pyo3 release before we can finish the Rust port.
I started to implemented this type then quickly found myself blocked on PyO3/pyo3#1205 / PyO3/pyo3#1206 due to not being able to return `Self` from `__enter__`. We'll have to wait for a future pyo3 release before we can finish the Rust port.
Fun fact: You can return #[pymethods]
impl MyClass {
fn __enter__<'p>(slf: PyRef<'p, Self>, _py: Python<'p>) -> PyResult<PyRef<'p, Self>> {
Ok(slf)
}
} You can also use I am curious why |
No, there's no some complex reason. It's just a convention for users: i.e., we treat |
+1 - I'm not aware of the original design choice for However I have to confess I've been wondering what happens if we deprecated |
As |
Is this still the correct way to call pub fn __enter__(slf: PyRef<Self>) -> PyRef<Self> {
slf
} ? I couldn't find any examples in the docs. |
Yes, that looks right. As a tiny optimization, if you don't need to access the class contents, I think you can avoid needing to do the runtime borrow check by using pub fn __enter__(slf: Py<Self>) -> Py<Self> {
slf
} (If that doesn't work, it should be trivial for us to make it work.) |
Thanks so much, seems to work fine, samuelcolvin/watchfiles#164 updated. |
Hey, I've been bitten by the fact that I'm guessing there's not good solution for it, is there ? (since the context_manager variable seems to outlive the context in Python, so maybe PyO3 cannot actually drop). I could have made the struct be a wrapper itself, containing Anything I missed ? |
As you say, the context manager itself can't magically drop itself on |
I'm trying to make a file writing interface be its own context manager with PyO3 0.12. The trouble is that I don't know how to return self from the
__enter__
function. I don't have this issue for implementing iterators that are their own iterators.I can work around this by not using the context protocol and simply using methods inside the pymethods block.
Code that I'm trying to make work: https://github.com/jothan/cordoba/blob/cf8c6deb809d73948c8c47677c6b92b799588131/src/pymod.rs#L200
Iterator that can return self: https://github.com/jothan/cordoba/blob/cf8c6deb809d73948c8c47677c6b92b799588131/src/pymod.rs#L118
The text was updated successfully, but these errors were encountered: