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