exception attributes

Andrew Dalke dalke at acm.org
Sun Aug 19 07:58:13 EDT 2001


Frederic Giacometti wrote:
>All exception classes must be subclasses of the Exception standard class
>(see standard documentation, or use some common sense ;)).

Technically,
] All user-defined exceptions should also be derived from this
] class, but this is not (yet) enforced.

% perl -ne 'print "$1\n" if /raise (\w+)/' *.py | sort | uniq > excs.txt
% perl -ne 'print "$1\n" if /class (\w+)/' *.py | sort | uniq > class.txt
% cd ..
% ./python
Python 2.2a1 (#4, Aug  6 2001, 22:06:49)
[GCC egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> classes = [line.rstrip() for line in open("Lib/class.txt")]
>>> excs = [line.rstrip() for line in open("Lib/excs.txt")]
>>> for e in excs:
...     if e not in classes and not hasattr(exceptions, e):
...         print e
...
BdbQuit   <-- in bdb.py; old-style string excption: BdbQuit = 'bdb.BdbQuit'
apply     <-- OK; in comments or docstrings
arguments <-- OK; in a comment
category  <-- OK; used in warnings.py as a variable for an existing
exception
e         <-- OK; used in ConfigParser as a variable
exc       <-- OK; used in os.py as a variable
exceptions <- OK; used in comments
getopt    <-- OK; referecnce to getopt.error
norm_error <- in macpath.py; old-style string execption; norm_error =
              'macpath.norm_error: path cannot be normalized'
os        <-- OK; reference to os.error
self      <-- OK; used in imaplib to allow subclasses to override the error
              and in unittest to determine class is a failure vs. error
socket    <-- OK; reference to socket.error
the       <-- OK; in comment string

So there are still two old-style string exceptions in the 2.2 Python
code, although class exceptions do all finally derive from Exception.

                    Andrew
                    dalke at dalkescientific.com






More information about the Python-list mailing list