C API: Making a context manager

Chris Kaynor ckaynor at zindagigames.com
Mon Oct 31 15:18:05 EDT 2011


On Mon, Oct 31, 2011 at 12:15 PM, Brian Curtin <brian.curtin at gmail.com> wrote:
>
> You'd just add "__enter__" and "__exit__" in the PyMethodDef. If you
> have the CPython source, we do it in there in a few places. Off the
> top of my head, PC\winreg.c contains at least one class that works as
> a context manager (PyHKEY), although there are a few others scattered
> around the source.
>

That is what I figured. I was just hoping there was some helper class
similar to the contextmanager decorator that would make it easier to
use, however at the same time it makes sense that there is not.

Thanks,
Chris



More information about the Python-list mailing list