[IPython-dev] [Ipython-svndiff] 2717 - improve callable alias inspection

Fernando Perez fperez.net at gmail.com
Thu Sep 6 02:06:43 EDT 2007


Hey,

On 9/5/07, ipython-svndiff at scipy.org <ipython-svndiff at scipy.org> wrote:

> +                try:
> +                    ds = "Alias to the system command:\n  %s" % obj[1]
> +                except:
> +                    ds = "Alias: " + str(obj)

What is this bare 'except' clause trying to stop?  Is it the
IndexError from obj[1] or a possible error on str()?  Catch-all naked
excepts should really be avoided except for a few occasions where they
are there as last-resort measures to prevent a full crash.  They tend
to mask the real intent of the code and hide possible unintended bugs.

We unfortunately have way too many already:

maqroll[IPython]> grep 'except:' *.py | wc -l
114

but at least we should try to trim them down, not add more.  Unless
this is one of those cases where the code underneath can explode in
weird ways, and a naked except is really needed.  But in such cases,
let's  in general try to at least document *why* such a catch-all is
needed.  Here's an example from OInspect:

    try:
        ds = inspect.getdoc(obj)
    except:
        # Harden against an inspect failure, which can occur with
        # SWIG-wrapped extensions.
        pass

(just below that there's another without a comment that could use one).

Cheers,

f



More information about the IPython-dev mailing list