[issue5251] contextlib.nested inconsistent with, well, nested with statements due exceptions raised in __enter__

Nick Coghlan report at bugs.python.org
Fri Nov 5 15:27:55 CET 2010

Nick Coghlan <ncoghlan at gmail.com> added the comment:

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__()
  if hasattr(cm, "__entered__"):
    VAL = cm.__entered__(_v)
    VAL = _v
  # do stuff

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.

components: +Interpreter Core -Documentation
resolution: out of date -> postponed
status: closed -> open
versions: +Python 3.3 -Python 2.7, Python 3.1, Python 3.2

Python tracker <report at bugs.python.org>

More information about the Python-bugs-list mailing list