Message 120497 - Python tracker (original) (raw)

Reopening as a reminder to myself that I have a new PEP I want to write in this area. The idea is essentially a lighter weight alternative to PEP 377 that adds an optional entered method to the context management protocol along the following lines:

_v = cm.enter() try: if hasattr(cm, "entered"): VAL = cm.entered(_v) else: VAL = _v

do stuff

finally: cm.exit(*exception_status)

Providing a second, optional method that is executed inside the body will let CMs do things they can't do now (like skip the body of the with statement) without significantly affecting the behaviour of normal CMs. Notably, GeneratorContextManager will be able to use this to more gracefully handle the case where the generator doesn't yield a value.

I plan to flesh this out into a python-ideas post (and likely a subsequent PEP) at some point in the next few months.