[Python-Dev] Major revision of PEP 348 committed
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
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
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.
OverflowError and ValueError (9 instances), in
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
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.
You can wordify anything if you just verb it.
--- Bucky Katt, Get Fuzzy
More information about the Python-Dev