[Python-Dev] Major revision of PEP 348 committed

Steven Bethard steven.bethard at gmail.com
Tue Aug 9 08:28:08 CEST 2005


Raymond Hettinger wrote:
> If the PEP can't resist the urge to create new intermediate groupings,
> then start by grepping through tons of Python code to find-out which
> exceptions are typically caught on the same line.  That would be a
> worthwhile empirical study and may lead to useful insights.

I was curious, so I did a little grepping (ok, os.walking and
re.findalling) ;-) through the Python source.  The only exceptions
that were caught together more than 5 times were:

AttributeError and TypeError (23 instances) in
code.py
doctest.py
linecache.py
mailbox.py
idlelib/rpc.py
lib-old/newdir.py
lib-tk/Tkinter.py
test/test_descr.py
test/test_file.py
test/test_funcattrs.py
test/test_os.py
Though these occur in a few different contexts, one relatively common
one was when the code tried to set a possibly read-only attribute.

ImportError and AttributeError (9 instances), in
getpass.py
locale.py
pydoc.py
tarfile.py
xmlrpclib.py
lib-tk/tkFileDialog.py
test/test_largefile.py
test/test_tarfile.py
This seemed to be used when an incompatible module might be present. 
(Attributes were tested to make sure the module was the right one.) 
Also used when code tried to use "private" module attributes (e.g.
_getdefaultlocale()).

OverflowError and ValueError (9 instances), in
csv.py
ftplib.py
mhlib.py
mimify.py
warnings.py
test/test_resource.py
These were generally around a call to int(x).  I assume they're
generally unnecessary now that int() silently converts to longs.

IOError and OSError (6 instances), in
pty.py
tempfile.py
whichdb.py
distutils/dir_util.py
idlelib/configHandler.py
test/test_complex.py
These were all around file/directory handling that I didn't study in
too much detail.  With the current hierarchy, there's no reason these
couldn't just be catching EnvironmentError anyway.

As you can see, even for the most common pairs of exceptions, the
total number of times these pairs were caught was pretty small.  Even
ignoring the low counts, we see that the last two pairs or exceptions
aren't really necessary, thanks to int/long unification and the
existence of EnvironmentError, and the former two pairs argue
*against* added nesting as it is unclear whether to group
AttributeError with ImportError or TypeError.  So it doesn't really
look like the stdlib's going to provide much of a case for adding
nesting to the exception hierarchy.

Anyway, I know PEP 348's been scaled back at this point anyway, but I
figured I might as well post my findings in case anyone was curious.

STeVe
-- 
You can wordify anything if you just verb it.
        --- Bucky Katt, Get Fuzzy


More information about the Python-Dev mailing list