[Python-Dev] PEP 343 - next steps
Nick Coghlan
ncoghlan at gmail.com
Mon Jun 13 14:40:05 CEST 2005
Guido van Rossum wrote:
> We considered this at some point during the PEP 340/343/346 discussion
> (in fact I had it this way in an early version of one of the PEPs),
> and in the end decided against it. I believe the argument was that any
> failure *before* __enter__() returns can be handled by try/except
> clauses inside __init__() or __enter__().
Hmm, you're right. Also, given implementation of PEP 343, code which
genuinely has to care about this can do so manually:
with async_exceptions_blocked():
with critical_resource():
with async_exceptions_unblocked():
# Do the real work
This ensures acquisition of the critical resource is not interrupted.
Even better, the above can be wrapped up in a template and still give
the desired guarantees:
@stmt_template
def safe_acquisition(resource):
with async_exceptions_blocked():
with resource():
with async_exceptions_unblocked():
yield None
with safe_acquisition(critical_resource):
# Do the real work
Cheers,
Nick.
--
Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia
---------------------------------------------------------------
http://boredomandlaziness.blogspot.com
More information about the Python-Dev
mailing list