home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_python.idb / usr / freeware / lib / python1.5 / exceptions.py.z / exceptions.py
Encoding:
Python Source  |  1999-04-16  |  6.3 KB  |  227 lines

  1. """Class based built-in exception hierarchy.
  2.  
  3. New with Python 1.5, all standard built-in exceptions are now class objects by
  4. default.  This gives Python's exception handling mechanism a more
  5. object-oriented feel.  Traditionally they were string objects.  Python will
  6. fallback to string based exceptions if the interpreter is invoked with the -X
  7. option, or if some failure occurs during class exception initialization (in
  8. this case a warning will be printed).
  9.  
  10. Most existing code should continue to work with class based exceptions.  Some
  11. tricky uses of IOError may break, but the most common uses should work.
  12.  
  13. Here is a rundown of the class hierarchy.  You can change this by editing this
  14. file, but it isn't recommended.  The class names described here are expected
  15. to be found by the bltinmodule.c file.  If you add classes here, you must
  16. modify bltinmodule.c or the exceptions won't be available in the __builtin__
  17. module, nor will they be accessible from C.
  18.  
  19. The classes with a `*' are new since Python 1.5.  They are defined as tuples
  20. containing the derived exceptions when string-based exceptions are used.  If
  21. you define your own class based exceptions, they should be derived from
  22. Exception.
  23.  
  24. Exception(*)
  25.  |
  26.  +-- StandardError(*)
  27.       |
  28.       +-- SystemExit
  29.       +-- KeyboardInterrupt
  30.       +-- ImportError
  31.       +-- EnvironmentError(*)
  32.       |    |
  33.       |    +-- IOError
  34.       |    +-- OSError(*)
  35.       |
  36.       +-- EOFError
  37.       +-- RuntimeError
  38.       |    |
  39.       |    +-- NotImplementedError(*)
  40.       |
  41.       +-- NameError
  42.       +-- AttributeError
  43.       +-- SyntaxError
  44.       +-- TypeError
  45.       +-- AssertionError
  46.       +-- LookupError(*)
  47.       |    |
  48.       |    +-- IndexError
  49.       |    +-- KeyError
  50.       |
  51.       +-- ArithmeticError(*)
  52.       |    |
  53.       |    +-- OverflowError
  54.       |    +-- ZeroDivisionError
  55.       |    +-- FloatingPointError
  56.       |
  57.       +-- ValueError
  58.       +-- SystemError
  59.       +-- MemoryError
  60. """
  61.  
  62. class Exception:
  63.     """Proposed base class for all exceptions."""
  64.     def __init__(self, *args):
  65.         self.args = args
  66.  
  67.     def __str__(self):
  68.         if not self.args:
  69.             return ''
  70.         elif len(self.args) == 1:
  71.             return str(self.args[0])
  72.         else:
  73.             return str(self.args)
  74.  
  75.     def __getitem__(self, i):
  76.         return self.args[i]
  77.  
  78. class StandardError(Exception):
  79.     """Base class for all standard Python exceptions."""
  80.     pass
  81.  
  82. class SyntaxError(StandardError):
  83.     """Invalid syntax."""
  84.     filename = lineno = offset = text = None
  85.     msg = ""
  86.     def __init__(self, *args):
  87.         self.args = args
  88.         if len(self.args) >= 1:
  89.             self.msg = self.args[0]
  90.         if len(self.args) == 2:
  91.             info = self.args[1]
  92.             try:
  93.                 self.filename, self.lineno, self.offset, self.text = info
  94.             except:
  95.                 pass
  96.     def __str__(self):
  97.         return str(self.msg)
  98.  
  99. class EnvironmentError(StandardError):
  100.     """Base class for I/O related errors."""
  101.     def __init__(self, *args):
  102.         self.args = args
  103.         self.errno = None
  104.         self.strerror = None
  105.         self.filename = None
  106.         if len(args) == 3:
  107.             # open() errors give third argument which is the filename.  BUT,
  108.             # so common in-place unpacking doesn't break, e.g.:
  109.             #
  110.             # except IOError, (errno, strerror):
  111.             #
  112.             # we hack args so that it only contains two items.  This also
  113.             # means we need our own __str__() which prints out the filename
  114.             # when it was supplied.
  115.             self.errno, self.strerror, self.filename = args
  116.             self.args = args[0:2]
  117.         if len(args) == 2:
  118.             # common case: PyErr_SetFromErrno()
  119.             self.errno, self.strerror = args
  120.  
  121.     def __str__(self):
  122.         if self.filename is not None:
  123.             return '[Errno %s] %s: %s' % (self.errno, self.strerror,
  124.                                           repr(self.filename))
  125.         elif self.errno and self.strerror:
  126.             return '[Errno %s] %s' % (self.errno, self.strerror)
  127.         else:
  128.             return StandardError.__str__(self)
  129.  
  130. class IOError(EnvironmentError):
  131.     """I/O operation failed."""
  132.     pass
  133.  
  134. class OSError(EnvironmentError):
  135.     """OS system call failed."""
  136.     pass
  137.  
  138. class RuntimeError(StandardError):
  139.     """Unspecified run-time error."""
  140.     pass
  141.  
  142. class NotImplementedError(RuntimeError):
  143.     """Method or function hasn't been implemented yet."""
  144.     pass
  145.  
  146. class SystemError(StandardError):
  147.     """Internal error in the Python interpreter.
  148.  
  149.     Please report this to the Python maintainer, along with the traceback,
  150.     the Python version, and the hardware/OS platform and version."""
  151.     pass
  152.  
  153. class EOFError(StandardError):
  154.     """Read beyond end of file."""
  155.     pass
  156.  
  157. class ImportError(StandardError):
  158.     """Import can't find module, or can't find name in module."""
  159.     pass
  160.  
  161. class TypeError(StandardError):
  162.     """Inappropriate argument type."""
  163.     pass
  164.  
  165. class ValueError(StandardError):
  166.     """Inappropriate argument value (of correct type)."""
  167.     pass
  168.  
  169. class KeyboardInterrupt(StandardError):
  170.     """Program interrupted by user."""
  171.     pass
  172.  
  173. class AssertionError(StandardError):
  174.     """Assertion failed."""
  175.     pass
  176.  
  177. class ArithmeticError(StandardError):
  178.     """Base class for arithmetic errors."""
  179.     pass
  180.  
  181. class OverflowError(ArithmeticError):
  182.     """Result too large to be represented."""
  183.     pass
  184.  
  185. class FloatingPointError(ArithmeticError):
  186.     """Floating point operation failed."""
  187.     pass
  188.  
  189. class ZeroDivisionError(ArithmeticError):
  190.     """Second argument to a division or modulo operation was zero."""
  191.     pass
  192.  
  193. class LookupError(StandardError):
  194.     """Base class for lookup errors."""
  195.     pass
  196.  
  197. class IndexError(LookupError):
  198.     """Sequence index out of range."""
  199.     pass
  200.  
  201. class KeyError(LookupError):
  202.     """Mapping key not found."""
  203.     pass
  204.  
  205. class AttributeError(StandardError):
  206.     """Attribute not found."""
  207.     pass
  208.  
  209. class NameError(StandardError):
  210.     """Name not found locally or globally."""
  211.     pass
  212.  
  213. class MemoryError(StandardError):
  214.     """Out of memory."""
  215.     pass
  216.  
  217. class SystemExit(Exception):
  218.     """Request to exit from the interpreter."""
  219.     def __init__(self, *args):
  220.         self.args = args
  221.         if len(args) == 0:
  222.             self.code = None
  223.         elif len(args) == 1:
  224.             self.code = args[0]
  225.         else:
  226.             self.code = args
  227.