Yield-based contextmanager for classes

I find myself explaining this trick to people all the time, so I agree it would be nice if there was a standard version I could point them to.

In my case, it’s b/c Trio has some important context managers that people want to wrap (cancel scopes + nurseries). So new users constantly try to write their own __enter__/__exit__ methods that manually call Trio’s __enter__/__exit__ methods + do other things, and I think literally every person who’s ever tried this has gotten it wrong (mostly around exception handling details). At this point we don’t even try to debug; we just tell users to always use @contextmanager.

One thing that makes me a bit uncomfortable is the handling of the magic contextmanager state. There’s the question of what to call the magic object attribute, but even more so, it’s weird to have a method that implicitly sets object state – what if someone calls __enter__ twice and one of the context manager objects overwrites the other? But I think these can be solved [1], and I’d be in favor of adding this to contextlib. @ncoghlan and @yselivanov, IIRC you’re the contextlib maintainers; what do you think?

[1] e.g. make the second call to __enter__ raise an error instead of overwriting internal state.