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.

test_exceptions.py 8.2KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. """Test pywin32's error semantics"""
  2. import sys
  3. import unittest
  4. import pythoncom
  5. import pywintypes
  6. import win32api
  7. import win32file
  8. import winerror
  9. class TestBase(unittest.TestCase):
  10. def _testExceptionIndex(self, exc, index, expected):
  11. # check the exception itself can be indexed if not py3k
  12. if sys.version_info < (3,):
  13. self.assertEqual(exc[index], expected)
  14. # and that exception.args can is the same.
  15. self.assertEqual(exc.args[index], expected)
  16. class TestAPISimple(TestBase):
  17. def _getInvalidHandleException(self):
  18. try:
  19. win32api.CloseHandle(1)
  20. except win32api.error as exc:
  21. return exc
  22. self.fail("Didn't get invalid-handle exception.")
  23. def testSimple(self):
  24. self.assertRaises(pywintypes.error, win32api.CloseHandle, 1)
  25. def testErrnoIndex(self):
  26. exc = self._getInvalidHandleException()
  27. self._testExceptionIndex(exc, 0, winerror.ERROR_INVALID_HANDLE)
  28. def testFuncIndex(self):
  29. exc = self._getInvalidHandleException()
  30. self._testExceptionIndex(exc, 1, "CloseHandle")
  31. def testMessageIndex(self):
  32. exc = self._getInvalidHandleException()
  33. expected = win32api.FormatMessage(winerror.ERROR_INVALID_HANDLE).rstrip()
  34. self._testExceptionIndex(exc, 2, expected)
  35. def testUnpack(self):
  36. try:
  37. win32api.CloseHandle(1)
  38. self.fail("expected exception!")
  39. except win32api.error as exc:
  40. self.assertEqual(exc.winerror, winerror.ERROR_INVALID_HANDLE)
  41. self.assertEqual(exc.funcname, "CloseHandle")
  42. expected_msg = win32api.FormatMessage(
  43. winerror.ERROR_INVALID_HANDLE
  44. ).rstrip()
  45. self.assertEqual(exc.strerror, expected_msg)
  46. def testAsStr(self):
  47. exc = self._getInvalidHandleException()
  48. err_msg = win32api.FormatMessage(winerror.ERROR_INVALID_HANDLE).rstrip()
  49. # early on the result actually *was* a tuple - it must always look like one
  50. err_tuple = (winerror.ERROR_INVALID_HANDLE, "CloseHandle", err_msg)
  51. self.assertEqual(str(exc), str(err_tuple))
  52. def testAsTuple(self):
  53. exc = self._getInvalidHandleException()
  54. err_msg = win32api.FormatMessage(winerror.ERROR_INVALID_HANDLE).rstrip()
  55. # early on the result actually *was* a tuple - it must be able to be one
  56. err_tuple = (winerror.ERROR_INVALID_HANDLE, "CloseHandle", err_msg)
  57. if sys.version_info < (3,):
  58. self.assertEqual(tuple(exc), err_tuple)
  59. else:
  60. self.assertEqual(exc.args, err_tuple)
  61. def testClassName(self):
  62. exc = self._getInvalidHandleException()
  63. # The error class has always been named 'error'. That's not ideal :(
  64. self.assertEqual(exc.__class__.__name__, "error")
  65. def testIdentity(self):
  66. exc = self._getInvalidHandleException()
  67. self.assertTrue(exc.__class__ is pywintypes.error)
  68. def testBaseClass(self):
  69. self.assertEqual(pywintypes.error.__bases__, (Exception,))
  70. def testAttributes(self):
  71. exc = self._getInvalidHandleException()
  72. err_msg = win32api.FormatMessage(winerror.ERROR_INVALID_HANDLE).rstrip()
  73. self.assertEqual(exc.winerror, winerror.ERROR_INVALID_HANDLE)
  74. self.assertEqual(exc.strerror, err_msg)
  75. self.assertEqual(exc.funcname, "CloseHandle")
  76. # some tests for 'insane' args.
  77. def testStrangeArgsNone(self):
  78. try:
  79. raise pywintypes.error()
  80. self.fail("Expected exception")
  81. except pywintypes.error as exc:
  82. self.assertEqual(exc.args, ())
  83. self.assertEqual(exc.winerror, None)
  84. self.assertEqual(exc.funcname, None)
  85. self.assertEqual(exc.strerror, None)
  86. def testStrangeArgsNotEnough(self):
  87. try:
  88. raise pywintypes.error("foo")
  89. self.fail("Expected exception")
  90. except pywintypes.error as exc:
  91. assert exc.args[0] == "foo"
  92. # 'winerror' always args[0]
  93. self.assertEqual(exc.winerror, "foo")
  94. self.assertEqual(exc.funcname, None)
  95. self.assertEqual(exc.strerror, None)
  96. def testStrangeArgsTooMany(self):
  97. try:
  98. raise pywintypes.error("foo", "bar", "you", "never", "kn", 0)
  99. self.fail("Expected exception")
  100. except pywintypes.error as exc:
  101. self.assertEqual(exc.args[0], "foo")
  102. self.assertEqual(exc.args[-1], 0)
  103. self.assertEqual(exc.winerror, "foo")
  104. self.assertEqual(exc.funcname, "bar")
  105. self.assertEqual(exc.strerror, "you")
  106. class TestCOMSimple(TestBase):
  107. def _getException(self):
  108. try:
  109. pythoncom.StgOpenStorage("foo", None, 0)
  110. except pythoncom.com_error as exc:
  111. return exc
  112. self.fail("Didn't get storage exception.")
  113. def testIs(self):
  114. self.assertTrue(pythoncom.com_error is pywintypes.com_error)
  115. def testSimple(self):
  116. self.assertRaises(pythoncom.com_error, pythoncom.StgOpenStorage, "foo", None, 0)
  117. def testErrnoIndex(self):
  118. exc = self._getException()
  119. self._testExceptionIndex(exc, 0, winerror.STG_E_INVALIDFLAG)
  120. def testMessageIndex(self):
  121. exc = self._getException()
  122. expected = win32api.FormatMessage(winerror.STG_E_INVALIDFLAG).rstrip()
  123. self._testExceptionIndex(exc, 1, expected)
  124. def testAsStr(self):
  125. exc = self._getException()
  126. err_msg = win32api.FormatMessage(winerror.STG_E_INVALIDFLAG).rstrip()
  127. # early on the result actually *was* a tuple - it must always look like one
  128. err_tuple = (winerror.STG_E_INVALIDFLAG, err_msg, None, None)
  129. self.assertEqual(str(exc), str(err_tuple))
  130. def testAsTuple(self):
  131. exc = self._getException()
  132. err_msg = win32api.FormatMessage(winerror.STG_E_INVALIDFLAG).rstrip()
  133. # early on the result actually *was* a tuple - it must be able to be one
  134. err_tuple = (winerror.STG_E_INVALIDFLAG, err_msg, None, None)
  135. if sys.version_info < (3,):
  136. self.assertEqual(tuple(exc), err_tuple)
  137. else:
  138. self.assertEqual(exc.args, err_tuple)
  139. def testClassName(self):
  140. exc = self._getException()
  141. self.assertEqual(exc.__class__.__name__, "com_error")
  142. def testIdentity(self):
  143. exc = self._getException()
  144. self.assertTrue(exc.__class__ is pywintypes.com_error)
  145. def testBaseClass(self):
  146. exc = self._getException()
  147. self.assertEqual(pywintypes.com_error.__bases__, (Exception,))
  148. def testAttributes(self):
  149. exc = self._getException()
  150. err_msg = win32api.FormatMessage(winerror.STG_E_INVALIDFLAG).rstrip()
  151. self.assertEqual(exc.hresult, winerror.STG_E_INVALIDFLAG)
  152. self.assertEqual(exc.strerror, err_msg)
  153. self.assertEqual(exc.argerror, None)
  154. self.assertEqual(exc.excepinfo, None)
  155. def testStrangeArgsNone(self):
  156. try:
  157. raise pywintypes.com_error()
  158. self.fail("Expected exception")
  159. except pywintypes.com_error as exc:
  160. self.assertEqual(exc.args, ())
  161. self.assertEqual(exc.hresult, None)
  162. self.assertEqual(exc.strerror, None)
  163. self.assertEqual(exc.argerror, None)
  164. self.assertEqual(exc.excepinfo, None)
  165. def testStrangeArgsNotEnough(self):
  166. try:
  167. raise pywintypes.com_error("foo")
  168. self.fail("Expected exception")
  169. except pywintypes.com_error as exc:
  170. self.assertEqual(exc.args[0], "foo")
  171. self.assertEqual(exc.hresult, "foo")
  172. self.assertEqual(exc.strerror, None)
  173. self.assertEqual(exc.excepinfo, None)
  174. self.assertEqual(exc.argerror, None)
  175. def testStrangeArgsTooMany(self):
  176. try:
  177. raise pywintypes.com_error("foo", "bar", "you", "never", "kn", 0)
  178. self.fail("Expected exception")
  179. except pywintypes.com_error as exc:
  180. self.assertEqual(exc.args[0], "foo")
  181. self.assertEqual(exc.args[-1], 0)
  182. self.assertEqual(exc.hresult, "foo")
  183. self.assertEqual(exc.strerror, "bar")
  184. self.assertEqual(exc.excepinfo, "you")
  185. self.assertEqual(exc.argerror, "never")
  186. if __name__ == "__main__":
  187. unittest.main()