bare excepts
Robert Collins
robertc at robertcollins.net
Wed Jan 4 00:04:19 GMT 2006
On Tue, 2006-01-03 at 11:39 -0500, Aaron Bentley wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Robert Collins wrote:
> > On Tue, 2006-01-03 at 01:28 -0500, Aaron Bentley wrote:
> >
> >>-----BEGIN PGP SIGNED MESSAGE-----
> >>Hash: SHA1
> >>
> >>Robert Collins wrote:
> >>| I'd be happy to extend my code-practices tests to check for:
> >>| except:
> >>| except Exception[ ,].*
> >>| What do you think? (They are persistent little buggers, keep coming
> >>| back.)
> >>
> >>I think we'd need a way to flag some of the second type as legit. And
> >>there's a risk of improper flagging, then. But on the whole, sure.
> >
> >
> > I'll put in a counter - which we can change as needed - to accomodate
> > the aggregate legit ones. That said, I *still* have never seen one.
>
> What about the exception logging?
>
> except Exception, e:
> # used to handle AssertionError and KeyboardInterrupt
> # specially here, but hopefully they're handled ok by the logger now
> import errno
> if (isinstance(e, IOError)
> and hasattr(e, 'errno')
> and e.errno == errno.EPIPE):
> bzrlib.trace.note('broken pipe')
> return 3
> else:
> bzrlib.trace.log_exception()
> if os.environ.get('BZR_PDB'):
> print '**** entering debugger'
> import pdb
> pdb.post_mortem(sys.exc_traceback)
> return 3
AssertionError and KeyboardInterrupt still should not be caught
unless we *want* them to drop into the debugger here.
if we do - then the comment is wrong, otherwise the following will be
buggy. And yes, I'll admit thata this is a valid use of bare excepts -
but only because its 'drop into the debugger' - its not used for flow
control or error handling. I'd write it as follows:
def do_debug():
"""Print the exception we have encountered.
If we have post mortem mode enabled, drop into the debugger.
"""
bzrlib.trace.log_exception()
if os.environ.get('BZR_PDB'):
print '**** entering debugger'
import pdb
pdb.post_mortem(sys.exc_traceback)
return 3
except IOError, e:
# all IOErrors have errno
if e.errno == errno.EPIPE:
bzrlib.trace.note('broken pipe')
return 3
return do_debug()
except:
# this catches string exceptions which
# except Exception, e: does not.
return do_debug()
Rob
--
GPG key available at: <http://www.robertcollins.net/keys.txt>.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part
Url : https://lists.ubuntu.com/archives/bazaar/attachments/20060104/274d7851/attachment.pgp
More information about the bazaar
mailing list