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

Fernando Perez fperez.net at gmail.com
Thu Sep 6 02:40:55 EDT 2007

On 9/6/07, Ville M. Vainio <vivainio at gmail.com> wrote:
> On 9/6/07, Fernando Perez <fperez.net at gmail.com> wrote:
> > 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
> We really have no certainty what "obj" will be anymore (even though
> it's typically a 2-tuple), so we can fall back to just rendering str
> representation of the object if we get an unknown exception. I.e.
> except does not signal an error situation. If this masked some
> exception raised in str, it would be raised again on next line.
> In short, this particular except was for necessity (duck typing),
> rather than laziness :-).

>From your explanation, it sounds like the proper catch would then be

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

which would catch either empty tuples or non-indexable objects, while
ensuring that something like a NameError doesn't go silently ignored
(which has happened many times to me from modifying the code in the
try clause, inadvertently making a small typo, and then wondering why
my changes don't work at all).

A naked except is the right answer in *very few* situations, I'm
afraid.  The fact that we have so many lying around is mostly a sign
of my own laziness in the past in thinking about the right thing to
do, so let's try to improve on this.



More information about the IPython-dev mailing list