[Python-Dev] Major revision of PEP 348 committed

Michael Hudson mwh at python.net
Tue Aug 9 11:50:08 CEST 2005

Steven Bethard <steven.bethard at gmail.com> writes:

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

This TypeError/AttributeError one is long known, and a bit of a mess,
really.  Finding an attribute usually fails because the object is not
of the expected type, after all.

> 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()).

This seems like ever-so-faintly lazy programming to me, but maybe
that's overly purist.

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

Yes, I think so.

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

Heh.  I'd have to admit that I rarely know which of IOError or OSError
I should be expecting in a given situation, nor that EnvironmentError
is a common subclass that I could catch instead...


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

Was interesting, thanks!


  <freeside> On a scale of One to AWESOME, twisted.web is PRETTY
             ABSTRACT!!!!                       -- from Twisted.Quotes

More information about the Python-Dev mailing list