test_exceptions.py 8.4 KB

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