[Tutor] Program review
kent37 at tds.net
Mon Jan 7 23:54:02 CET 2008
Ricardo Aráoz wrote:
> Kent Johnson wrote:
>> from contextlib import nested
>> with nested(open_file(self.direcciones), open_file(self.excluidas))
>> as (fIncl, fExcl):
> Nice! How would you add exception reporting to this?
I don't have a good answer to that, that's why I didn't propose it
originally. You could use
with nested(open_file(self.direcciones), open_file(self.excluidas))
as (fIncl, fExcl):
# handle exceptions from do_something_with()
pass # We already logged exceptions from the open_file calls
You could omit the inner try/except if you are sure that
do_something_with() won't raise an exception, but it's pretty hard to be
sure of that unless it wraps its contents already.
Looking at PEP 343, it's pretty clear ;-) that exceptions raised by
open_file() will just be passed out of the with statement. Look at the
pseudo-code in the section "Specification: The 'with' Statement". The
exception is raised in the very first line - mgr = (EXPR) - when
open_file() is called.
More information about the Tutor