PyXR

c:\python24\lib\site-packages\win32 \ com \ server \ exception.py



0001 """Exception Handling
0002 
0003  Exceptions
0004 
0005          To better support COM exceptions, the framework allows for an instance to be
0006          raised.  This instance may have a certain number of known attributes, which are
0007          translated into COM exception details.
0008         
0009          This means, for example, that Python could raise a COM exception that includes details
0010          on a Help file and location, and a description for the user.
0011         
0012          This module provides a class which provides the necessary attributes.
0013 
0014 """
0015 import sys, pythoncom
0016 
0017 # Note that we derive from com_error, which derives from exceptions.Exception
0018 # Also note that we dont support "self.args", as we dont support tuple-unpacking
0019 class COMException(pythoncom.com_error):
0020         """An Exception object that is understood by the framework.
0021         
0022         If the framework is presented with an exception of type class,
0023         it looks for certain known attributes on this class to provide rich
0024         error information to the caller.
0025 
0026         It should be noted that the framework supports providing this error
0027         information via COM Exceptions, or via the ISupportErrorInfo interface.
0028 
0029         By using this class, you automatically provide rich error information to the
0030         server.
0031         """
0032         def __init__(self, description = None, scode = None,
0033                                  source = None, helpfile = None, helpContext = None,
0034                                  desc = None, hresult = None):
0035                 """Initialize an exception
0036                 **Params**
0037 
0038                 description -- A string description for the exception.
0039                 scode -- An integer scode to be returned to the server, if necessary.
0040                 The pythoncom framework defaults this to be DISP_E_EXCEPTION if not specified otherwise.
0041                 source -- A string which identifies the source of the error.
0042                 helpfile -- A string which points to a help file which contains details on the error.
0043                 helpContext -- An integer context in the help file.
0044                 desc -- A short-cut for description.
0045                 hresult -- A short-cut for scode.
0046                 """
0047 
0048                 # convert a WIN32 error into an HRESULT
0049                 scode = scode or hresult
0050                 if scode and scode != 1: # We dont want S_FALSE mapped!
0051                         if scode >= -32768 and scode < 32768:
0052                                 # this is HRESULT_FROM_WIN32()
0053                                 scode = -2147024896 | (scode & 0x0000FFFF)
0054                 self.scode = scode
0055 
0056                 self.description = description or desc
0057                 if scode==1 and not self.description:
0058                         self.description = "S_FALSE"
0059                 elif scode and not self.description:
0060                         self.description = pythoncom.GetScodeString(scode)
0061 
0062                 self.source = source
0063                 self.helpfile = helpfile
0064                 self.helpcontext = helpContext
0065 
0066                 # todo - fill in the exception value
0067                 pythoncom.com_error.__init__(self, scode, self.description, None, -1)
0068 
0069         def __repr__(self):
0070                 return "<COM Exception - scode=%s, desc=%s>" % (self.scode, self.description)
0071 
0072 # Old name for the COMException class.
0073 # Do NOT use the name Exception, as it is now a built-in
0074 # COMException is the new, official name.
0075 Exception = COMException
0076 
0077 def IsCOMException(t = None):
0078         if t is None:
0079                 t = sys.exc_info()[0]
0080         try:
0081                 return issubclass(t, pythoncom.com_error)
0082         except TypeError: # 1.5 in -X mode?
0083                 return t is pythoncon.com_error
0084 
0085 def IsCOMServerException(t = None):
0086         if t is None:
0087                 t = sys.exc_info()[0]
0088         try:
0089                 return issubclass(t, COMException)
0090         except TypeError: # String exception
0091                 return 0
0092 

Generated by PyXR 0.9.4
SourceForge.net Logo