Pyo3: Trouble using PyContextProtocol

Created on 22 Sep 2020  路  3Comments  路  Source: PyO3/pyo3

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

confusing-api

Most helpful comment

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.

All 3 comments

Thanks, yes this is sadly a flaw with the #[pyproto] design at the moment which I've been aware of for a while but not found the right moment to fix. Perhaps this is now a good time. I've opened #1206 and will try to take a spin at this soon.

For now, I'm happy to write a temporary solution (i.e. allow PyRefMut) for this.
@davidhewitt What do you think?

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.

Was this page helpful?
0 / 5 - 0 ratings