|
| 1 | +import unittest as ut |
| 2 | + |
| 3 | +from comtypes import errorinfo, shelllink |
| 4 | +from comtypes import hresult as hres |
| 5 | + |
| 6 | + |
| 7 | +class Test_GetErrorInfo(ut.TestCase): |
| 8 | + def test_error_has_been_set(self): |
| 9 | + self.assertIsNone(errorinfo.GetErrorInfo()) |
| 10 | + errmsg = "sample unexpected error message for tests" |
| 11 | + errcode = hres.E_UNEXPECTED |
| 12 | + helpcontext = 123 |
| 13 | + hr = errorinfo.ReportError( |
| 14 | + errmsg, |
| 15 | + shelllink.IShellLinkW._iid_, |
| 16 | + str(shelllink.ShellLink._reg_clsid_), |
| 17 | + helpfile="help.chm", |
| 18 | + helpcontext=helpcontext, |
| 19 | + hresult=errcode, |
| 20 | + ) |
| 21 | + self.assertEqual(errcode, hr) |
| 22 | + pei = errorinfo.GetErrorInfo() |
| 23 | + self.assertIsNotNone(pei) |
| 24 | + self.assertEqual(shelllink.IShellLinkW._iid_, pei.GetGUID()) |
| 25 | + self.assertEqual("lnkfile", pei.GetSource()) |
| 26 | + self.assertEqual(errmsg, pei.GetDescription()) |
| 27 | + self.assertEqual("help.chm", pei.GetHelpFile()) |
| 28 | + self.assertEqual(helpcontext, pei.GetHelpContext()) |
| 29 | + # Calling `GetErrorInfo` clears the error state for the thread. |
| 30 | + # Therefore, `None` is returned in the second call. |
| 31 | + self.assertIsNone(errorinfo.GetErrorInfo()) |
| 32 | + |
| 33 | + def test_without_optional_args(self): |
| 34 | + self.assertIsNone(errorinfo.GetErrorInfo()) |
| 35 | + errmsg = "sample unexpected error message for tests" |
| 36 | + hr = errorinfo.ReportError(errmsg, shelllink.IShellLinkW._iid_) |
| 37 | + self.assertEqual(hres.DISP_E_EXCEPTION, hr) |
| 38 | + pei = errorinfo.GetErrorInfo() |
| 39 | + self.assertIsNotNone(pei) |
| 40 | + self.assertEqual(shelllink.IShellLinkW._iid_, pei.GetGUID()) |
| 41 | + self.assertIsNone(pei.GetSource()) |
| 42 | + self.assertEqual(errmsg, pei.GetDescription()) |
| 43 | + self.assertIsNone(pei.GetHelpFile()) |
| 44 | + self.assertEqual(0, pei.GetHelpContext()) |
| 45 | + |
| 46 | + def test_error_has_not_been_set(self): |
| 47 | + self.assertIsNone(errorinfo.GetErrorInfo()) |
| 48 | + pei = errorinfo.GetErrorInfo() |
| 49 | + self.assertIsNone(pei) |
| 50 | + |
| 51 | + |
| 52 | +def raise_runtime_error(): |
| 53 | + _raise_runtime_error() |
| 54 | + |
| 55 | + |
| 56 | +def _raise_runtime_error(): |
| 57 | + raise RuntimeError("for testing") |
| 58 | + |
| 59 | + |
| 60 | +class Test_ReportException(ut.TestCase): |
| 61 | + def test_without_stacklevel(self): |
| 62 | + self.assertIsNone(errorinfo.GetErrorInfo()) |
| 63 | + iid = shelllink.IShellLinkW._iid_ |
| 64 | + try: |
| 65 | + raise_runtime_error() |
| 66 | + except RuntimeError: |
| 67 | + hr = errorinfo.ReportException(hres.E_UNEXPECTED, iid) |
| 68 | + self.assertEqual(hres.E_UNEXPECTED, hr) |
| 69 | + pei = errorinfo.GetErrorInfo() |
| 70 | + self.assertIsNotNone(pei) |
| 71 | + self.assertEqual(shelllink.IShellLinkW._iid_, pei.GetGUID()) |
| 72 | + self.assertIsNone(pei.GetSource()) |
| 73 | + self.assertEqual("<class 'RuntimeError'>: for testing", pei.GetDescription()) |
| 74 | + self.assertIsNone(pei.GetHelpFile()) |
| 75 | + self.assertEqual(0, pei.GetHelpContext()) |
| 76 | + self.assertIsNone(errorinfo.GetErrorInfo()) |
| 77 | + |
| 78 | + def test_with_stacklevel(self): |
| 79 | + self.assertIsNone(errorinfo.GetErrorInfo()) |
| 80 | + stem = "<class 'RuntimeError'>: for testing" |
| 81 | + iid = shelllink.IShellLinkW._iid_ |
| 82 | + for slv, text in [ |
| 83 | + # XXX: If the codebase changes, the line where functions or |
| 84 | + # methods are defined will change, meaning this test is brittle. |
| 85 | + (0, f"{stem} ({__name__}, line 93)"), |
| 86 | + (1, f"{stem} ({__name__}, line 53)"), |
| 87 | + (2, f"{stem} ({__name__}, line 57)"), |
| 88 | + ]: |
| 89 | + with self.subTest(text=text): |
| 90 | + try: |
| 91 | + raise_runtime_error() |
| 92 | + except RuntimeError: |
| 93 | + errorinfo.ReportException(hres.E_UNEXPECTED, iid, stacklevel=slv) |
| 94 | + pei = errorinfo.GetErrorInfo() |
| 95 | + self.assertEqual(text, pei.GetDescription()) |
| 96 | + self.assertIsNone(errorinfo.GetErrorInfo()) |
0 commit comments