home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 July / CMCD0704.ISO / Software / Shareware / Comunicatii / jyte / jyte.exe / exception.py < prev    next >
Text File  |  2003-06-30  |  3KB  |  92 lines

  1. """Exception Handling
  2.  
  3.  Exceptions
  4.  
  5.      To better support COM exceptions, the framework allows for an instance to be
  6.      raised.  This instance may have a certain number of known attributes, which are
  7.      translated into COM exception details.
  8.     
  9.      This means, for example, that Python could raise a COM exception that includes details
  10.      on a Help file and location, and a description for the user.
  11.     
  12.      This module provides a class which provides the necessary attributes.
  13.  
  14. """
  15. import sys, pythoncom
  16.  
  17. # Note that we derive from com_error, which derives from exceptions.Exception
  18. # Also note that we dont support "self.args", as we dont support tuple-unpacking
  19. class COMException(pythoncom.com_error):
  20.     """An Exception object that is understood by the framework.
  21.     
  22.     If the framework is presented with an exception of type class,
  23.     it looks for certain known attributes on this class to provide rich
  24.     error information to the caller.
  25.  
  26.     It should be noted that the framework supports providing this error
  27.     information via COM Exceptions, or via the ISupportErrorInfo interface.
  28.  
  29.     By using this class, you automatically provide rich error information to the
  30.     server.
  31.     """
  32.     def __init__(self, description = None, scode = None,
  33.                  source = None, helpfile = None, helpContext = None,
  34.                  desc = None, hresult = None):
  35.         """Initialize an exception
  36.         **Params**
  37.  
  38.         description -- A string description for the exception.
  39.         scode -- An integer scode to be returned to the server, if necessary.
  40.         The pythoncom framework defaults this to be DISP_E_EXCEPTION if not specified otherwise.
  41.         source -- A string which identifies the source of the error.
  42.         helpfile -- A string which points to a help file which contains details on the error.
  43.         helpContext -- An integer context in the help file.
  44.         desc -- A short-cut for description.
  45.         hresult -- A short-cut for scode.
  46.         """
  47.  
  48.         # convert a WIN32 error into an HRESULT
  49.         scode = scode or hresult
  50.         if scode and scode != 1: # We dont want S_FALSE mapped!
  51.             if scode >= -32768 and scode < 32768:
  52.                 # this is HRESULT_FROM_WIN32()
  53.                 scode = -2147024896 | (scode & 0x0000FFFF)
  54.         self.scode = scode
  55.  
  56.         self.description = description or desc
  57.         if scode==1 and not self.description:
  58.             self.description = "S_FALSE"
  59.         elif scode and not self.description:
  60.             self.description = pythoncom.GetScodeString(scode)
  61.  
  62.         self.source = source
  63.         self.helpfile = helpfile
  64.         self.helpcontext = helpContext
  65.  
  66.         # todo - fill in the exception value
  67.         pythoncom.com_error.__init__(self, scode, self.description, None, -1)
  68.  
  69.     def __repr__(self):
  70.         return "<COM Exception - scode=%s, desc=%s>" % (self.scode, self.description)
  71.  
  72. # Old name for the COMException class.
  73. # Do NOT use the name Exception, as it is now a built-in
  74. # COMException is the new, official name.
  75. Exception = COMException
  76.  
  77. def IsCOMException(t = None):
  78.     if t is None:
  79.         t = sys.exc_info()[0]
  80.     try:
  81.         return issubclass(t, pythoncom.com_error)
  82.     except TypeError: # 1.5 in -X mode?
  83.         return t is pythoncon.com_error
  84.  
  85. def IsCOMServerException(t = None):
  86.     if t is None:
  87.         t = sys.exc_info()[0]
  88.     try:
  89.         return issubclass(t, COMException)
  90.     except TypeError: # String exception
  91.         return 0
  92.