home *** CD-ROM | disk | FTP | other *** search
/ linuxmafia.com 2016 / linuxmafia.com.tar / linuxmafia.com / pub / palmos / pippy-0.6beta-src.tar.gz / pippy-0.6beta-src.tar / pippy-0.6beta-src / src / Lib / exceptions.py < prev    next >
Text File  |  2000-12-21  |  7KB  |  246 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.       |         +-- WindowsError(*)
  38.       |
  39.       +-- EOFError
  40.       +-- RuntimeError
  41.       |    |
  42.       |    +-- NotImplementedError(*)
  43.       |    +-- MissingFeatureError(*)
  44.       |
  45.       +-- NameError
  46.       |    |
  47.       |    +-- UnboundLocalError(*)
  48.       |
  49.       +-- AttributeError
  50.       +-- SyntaxError
  51.       +-- TypeError
  52.       +-- AssertionError
  53.       +-- LookupError(*)
  54.       |    |
  55.       |    +-- IndexError
  56.       |    +-- KeyError
  57.       |
  58.       +-- ArithmeticError(*)
  59.       |    |
  60.       |    +-- OverflowError
  61.       |    +-- ZeroDivisionError
  62.       |    +-- FloatingPointError
  63.       |
  64.       +-- ValueError
  65.       +-- SystemError
  66.       +-- MemoryError
  67. """
  68.  
  69. class Exception:
  70.     """Proposed base class for all exceptions."""
  71.     def __init__(self, *args):
  72.         self.args = args
  73.  
  74.     def __str__(self):
  75.         if not self.args:
  76.             return ''
  77.         elif len(self.args) == 1:
  78.             return str(self.args[0])
  79.         else:
  80.             return str(self.args)
  81.  
  82.     def __getitem__(self, i):
  83.         return self.args[i]
  84.  
  85. class StandardError(Exception):
  86.     """Base class for all standard Python exceptions."""
  87.     pass
  88.  
  89. class SyntaxError(StandardError):
  90.     """Invalid syntax."""
  91.     filename = lineno = offset = text = None
  92.     msg = ""
  93.     def __init__(self, *args):
  94.         self.args = args
  95.         if len(self.args) >= 1:
  96.             self.msg = self.args[0]
  97.         if len(self.args) == 2:
  98.             info = self.args[1]
  99.             try:
  100.                 self.filename, self.lineno, self.offset, self.text = info
  101.             except:
  102.                 pass
  103.     def __str__(self):
  104.         return str(self.msg)
  105.  
  106. class EnvironmentError(StandardError):
  107.     """Base class for I/O related errors."""
  108.     def __init__(self, *args):
  109.         self.args = args
  110.         self.errno = None
  111.         self.strerror = None
  112.         self.filename = None
  113.         if len(args) == 3:
  114.             # open() errors give third argument which is the filename.  BUT,
  115.             # so common in-place unpacking doesn't break, e.g.:
  116.             #
  117.             # except IOError, (errno, strerror):
  118.             #
  119.             # we hack args so that it only contains two items.  This also
  120.             # means we need our own __str__() which prints out the filename
  121.             # when it was supplied.
  122.             self.errno, self.strerror, self.filename = args
  123.             self.args = args[0:2]
  124.         if len(args) == 2:
  125.             # common case: PyErr_SetFromErrno()
  126.             self.errno, self.strerror = args
  127.  
  128.     def __str__(self):
  129.         if self.filename is not None:
  130.             return '[Errno %s] %s: %s' % (self.errno, self.strerror,
  131.                                           repr(self.filename))
  132.         elif self.errno and self.strerror:
  133.             return '[Errno %s] %s' % (self.errno, self.strerror)
  134.         else:
  135.             return StandardError.__str__(self)
  136.  
  137. class IOError(EnvironmentError):
  138.     """I/O operation failed."""
  139.     pass
  140.  
  141. class OSError(EnvironmentError):
  142.     """OS system call failed."""
  143.     pass
  144.  
  145. class WindowsError(OSError):
  146.     """MS-Windows OS system call failed."""
  147.     pass
  148.  
  149. class RuntimeError(StandardError):
  150.     """Unspecified run-time error."""
  151.     pass
  152.  
  153. class NotImplementedError(RuntimeError):
  154.     """Method or function hasn't been implemented yet."""
  155.     pass
  156.  
  157. class MissingFeatureError(RuntimeError):
  158.     """Standard feature has been eliminated in this python build."""
  159.     pass
  160.  
  161. class SystemError(StandardError):
  162.     """Internal error in the Python interpreter.
  163.  
  164.     Please report this to the Python maintainer, along with the traceback,
  165.     the Python version, and the hardware/OS platform and version."""
  166.     pass
  167.  
  168. class EOFError(StandardError):
  169.     """Read beyond end of file."""
  170.     pass
  171.  
  172. class ImportError(StandardError):
  173.     """Import can't find module, or can't find name in module."""
  174.     pass
  175.  
  176. class TypeError(StandardError):
  177.     """Inappropriate argument type."""
  178.     pass
  179.  
  180. class ValueError(StandardError):
  181.     """Inappropriate argument value (of correct type)."""
  182.     pass
  183.  
  184. class KeyboardInterrupt(StandardError):
  185.     """Program interrupted by user."""
  186.     pass
  187.  
  188. class AssertionError(StandardError):
  189.     """Assertion failed."""
  190.     pass
  191.  
  192. class ArithmeticError(StandardError):
  193.     """Base class for arithmetic errors."""
  194.     pass
  195.  
  196. class OverflowError(ArithmeticError):
  197.     """Result too large to be represented."""
  198.     pass
  199.  
  200. class FloatingPointError(ArithmeticError):
  201.     """Floating point operation failed."""
  202.     pass
  203.  
  204. class ZeroDivisionError(ArithmeticError):
  205.     """Second argument to a division or modulo operation was zero."""
  206.     pass
  207.  
  208. class LookupError(StandardError):
  209.     """Base class for lookup errors."""
  210.     pass
  211.  
  212. class IndexError(LookupError):
  213.     """Sequence index out of range."""
  214.     pass
  215.  
  216. class KeyError(LookupError):
  217.     """Mapping key not found."""
  218.     pass
  219.  
  220. class AttributeError(StandardError):
  221.     """Attribute not found."""
  222.     pass
  223.  
  224. class NameError(StandardError):
  225.     """Name not found globally."""
  226.     pass
  227.  
  228. class UnboundLocalError(NameError):
  229.     """Local name referenced but not bound to a value."""
  230.     pass
  231.  
  232. class MemoryError(StandardError):
  233.     """Out of memory."""
  234.     pass
  235.  
  236. class SystemExit(Exception):
  237.     """Request to exit from the interpreter."""
  238.     def __init__(self, *args):
  239.         self.args = args
  240.         if len(args) == 0:
  241.             self.code = None
  242.         elif len(args) == 1:
  243.             self.code = args[0]
  244.         else:
  245.             self.code = args
  246.