This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author kristjan.jonsson
Recipients eric.snow, kristjan.jonsson, ncoghlan, r.david.murray
Date 2013-08-08.10:39:42
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1375958382.54.0.851050075272.issue18677@psf.upfronthosting.co.za>
In-reply-to
Content
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?
History
Date User Action Args
2013-08-08 10:39:42kristjan.jonssonsetrecipients: + kristjan.jonsson, ncoghlan, r.david.murray, eric.snow
2013-08-08 10:39:42kristjan.jonssonsetmessageid: <1375958382.54.0.851050075272.issue18677@psf.upfronthosting.co.za>
2013-08-08 10:39:42kristjan.jonssonlinkissue18677 messages
2013-08-08 10:39:42kristjan.jonssoncreate