exception.py 3.1 KB

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