Kristján Valur Jónsson added the comment:

Thanks, Eric.
I read that bit and I can't say that I disagree.
And I'm not necessarily advocating that "skipping the body" become a standard 
feature of context managers.  But it is a necessary functionality if you want 
to be able to dynamically nest one or more context managers, something I think 
Python should be able to do, for completeness, if not only for aesthetic beauty.

Having said that, optionally skipping the body is a far cry from the more 
esoteric constructs achievable with pep 340.

And python _already_ silently skips the body of managed code, if you nest two 
managers:

@contextmanager errordude:
    1 // 0
    yield
@contextmanager handler:
    try:
        yield
    except ZeroDivisionError:
        pass

with handler, errordude:
    do_stuff()

These context managers will skip the execution of f.  It will be Python's 
internal decision to do so, of course.  But the "with" statement already has 
the potential to have the body silently skipped.

What I'm adding here, the ContextManagerExit, is the ability for the context 
manager itself to make the decision, so that the two context managers above can 
be coalesced into one:

    with nested(handler, errordude):
        do_stuff()

The fact that do_stuff can be silently skipped in the first case, where we 
explicitly have two nested calls, invalidates IMHO the argument that context 
managers should not affect control flow.  why shouldn't it also be skippable in 
the case of a single context manager?

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue18677>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to