Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

exception.py 3.4KB

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