ask help about raise, thank u

Emile van Sebille emile at fenx.com
Wed Feb 27 01:49:28 EST 2002


"John Machin"
> "Emile van Sebille"
> > Isn't this what raise by itself does?
> >
>
> Quite possibly. I profess ignorance -- see the disclaimer in my post.
> I posted merely in case the cognoscenti ignored an unfortunate whose
> post turned up (in Google Groups at least) encoded in base64.
>
> Could you please enlighten me on the following:
>
> If a bare 0-arg raise is (mostly) equivalent to:
>    x, y, z = sys.exc_info()
>    raise x, y, z
> (apart from the circular reference problem) then what is the 3-arg
> raise useful for?

Well, now you're raising the same question suspiciously absent but
implied in my original reply to the OP.  ;-))  I imagine it could be
used in a "smart" error handler, of course, but it's not clear to me.

>
> The reference manual says:
> """If a third object is present, and it is not None, it should be a
> traceback object (see section 3.2), and it is substituted instead of
> the current location as the place where the exception occurred. This
> is useful to re-raise an exception transparently in an except
> clause."""
>
> There is a very interesting 39-message thread back in April 1998 that
> actually documents the conception & birth of 0-arg raise --
> Google("raise traceback"). Seems like 3-arg raise is now not very
> useful, and the manual could perhaps point out that 0-arg raise now
> handles the "re-raise transparently" functionality.
>
> I note that of the 4 examples of 3-arg raise in urllib.py, one seems
> to have the circular reference problem.

Thanks for looking at those.  Now that you have, you know more than I
about their potential use. ;-)

>
> While seeking enlightenment: What mechanism did you use to search all
> of Lib/*.py for 3-arg raises?

I wrote a q&d in python, then poked through the results.  I didn't save
it, but here's one that does a better job recognizing lines:

import os, glob, tokenize, StringIO

def outsink(*args): pass

pyfiles = glob.glob(os.path.join(os.path.dirname(os.__file__),
'url*.py'))
for file in pyfiles:
    pgmlines = open(file).readlines()
    buf = []
    for line in pgmlines:
        buf.append(line)
        line = ''.join( [x.strip() for x in buf ]).strip()
        try:
            tokenize.tokenize(StringIO.StringIO(line).readline, outsink)
            if line.startswith('raise') and line.count(',')>1:
                print '\n\n%s:%s' % (file, line)
            buf = []
        except: pass


See you,

Emile van Sebille
emile at fenx.com







More information about the Python-list mailing list