[Tutor] Program review

Ricardo Aráoz ricaraoz at gmail.com
Tue Jan 8 01:22:43 CET 2008


Kent Johnson wrote:
> 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
> 
> try:
>    with nested(open_file(self.direcciones), open_file(self.excluidas))
> as (fIncl, fExcl):
>      try:
>        do_something_with(fIncl, fExcl)
>      except:
>        # handle exceptions from do_something_with()
> except:
>    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.
> 

Yes, I can see that. I don't like it, too much hair splitting to get
something that is easily programmed the traditional way. I don't want to
be thinking about if the exception will or will not be passed out of the
with statement, and I CERTAINLY don't want the person who will be
modifying my code to have to think about those things. I'll stay with
the old way, easier, clearer and almost the same length, I fail to see
what I would win by coding this way.




More information about the Tutor mailing list