codekingpro/portable-devtools
114k
1"""Exception Handling2 3 Exceptions4 5 To better support COM exceptions, the framework allows for an instance to be6 raised. This instance may have a certain number of known attributes, which are7 translated into COM exception details.8 9 This means, for example, that Python could raise a COM exception that includes details10 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"""15import sys16 17import pythoncom18 19 20# Note that we derive from com_error, which derives from exceptions.Exception21# Also note that we dont support "self.args", as we dont support tuple-unpacking22class COMException(pythoncom.com_error):23 """An Exception object that is understood by the framework.24 25 If the framework is presented with an exception of type class,26 it looks for certain known attributes on this class to provide rich27 error information to the caller.28 29 It should be noted that the framework supports providing this error30 information via COM Exceptions, or via the ISupportErrorInfo interface.31 32 By using this class, you automatically provide rich error information to the33 server.34 """35 36 def __init__(37 self,38 description=None,39 scode=None,40 source=None,41 helpfile=None,42 helpContext=None,43 desc=None,44 hresult=None,45 ):46 """Initialize an exception47 **Params**48 49 description -- A string description for the exception.50 scode -- An integer scode to be returned to the server, if necessary.51 The pythoncom framework defaults this to be DISP_E_EXCEPTION if not specified otherwise.52 source -- A string which identifies the source of the error.53 helpfile -- A string which points to a help file which contains details on the error.54 helpContext -- An integer context in the help file.55 desc -- A short-cut for description.56 hresult -- A short-cut for scode.57 """58 59 # convert a WIN32 error into an HRESULT60 scode = scode or hresult61 if scode and scode != 1: # We dont want S_FALSE mapped!62 if scode >= -32768 and scode < 32768:63 # this is HRESULT_FROM_WIN32()64 scode = -2147024896 | (scode & 0x0000FFFF)65 self.scode = scode66 67 self.description = description or desc68 if scode == 1 and not self.description:69 self.description = "S_FALSE"70 elif scode and not self.description:71 self.description = pythoncom.GetScodeString(scode)72 73 self.source = source74 self.helpfile = helpfile75 self.helpcontext = helpContext76 77 # todo - fill in the exception value78 pythoncom.com_error.__init__(self, scode, self.description, None, -1)79 80 def __repr__(self):81 return "<COM Exception - scode=%s, desc=%s>" % (self.scode, self.description)82 83 84# Old name for the COMException class.85# Do NOT use the name Exception, as it is now a built-in86# COMException is the new, official name.87Exception = COMException88 89 90def IsCOMException(t=None):91 if t is None:92 t = sys.exc_info()[0]93 try:94 return issubclass(t, pythoncom.com_error)95 except TypeError: # 1.5 in -X mode?96 return t is pythoncon.com_error97 98 99def IsCOMServerException(t=None):100 if t is None:101 t = sys.exc_info()[0]102 try:103 return issubclass(t, COMException)104 except TypeError: # String exception105 return 0106 