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

Fernando Perez fperez.net@gmail....
Thu Sep 6 01:40:55 CDT 2007


On 9/6/07, Ville M. Vainio <vivainio@gmail.com> wrote:
> On 9/6/07, Fernando Perez <fperez.net@gmail.com> wrote:
>
> > On 9/5/07, ipython-svndiff@scipy.org <ipython-svndiff@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

                try:
                   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.

Cheers,

f


More information about the IPython-dev mailing list