Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test_errorinfo. #819

Merged
merged 4 commits into from
Feb 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions comtypes/errorinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from typing import TYPE_CHECKING

from comtypes import BSTR, COMMETHOD, GUID, HRESULT, IUnknown
from comtypes.hresult import *
from comtypes.hresult import DISP_E_EXCEPTION, S_OK

if TYPE_CHECKING:
from comtypes import hints # type: ignore
Expand Down Expand Up @@ -109,7 +109,7 @@ def ReportError(
ei.SetSource(
progid
) # progid for the class or application that created the error
_SetErrorInfo(0, ei)
SetErrorInfo(ei)
return hresult


Expand Down
96 changes: 96 additions & 0 deletions comtypes/test/test_errorinfo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import unittest as ut

from comtypes import errorinfo, shelllink
from comtypes import hresult as hres


class Test_GetErrorInfo(ut.TestCase):
def test_error_has_been_set(self):
self.assertIsNone(errorinfo.GetErrorInfo())
errmsg = "sample unexpected error message for tests"
errcode = hres.E_UNEXPECTED
helpcontext = 123
hr = errorinfo.ReportError(
errmsg,
shelllink.IShellLinkW._iid_,
str(shelllink.ShellLink._reg_clsid_),
helpfile="help.chm",
helpcontext=helpcontext,
hresult=errcode,
)
self.assertEqual(errcode, hr)
pei = errorinfo.GetErrorInfo()
self.assertIsNotNone(pei)
self.assertEqual(shelllink.IShellLinkW._iid_, pei.GetGUID())
self.assertEqual("lnkfile", pei.GetSource())
self.assertEqual(errmsg, pei.GetDescription())
self.assertEqual("help.chm", pei.GetHelpFile())
self.assertEqual(helpcontext, pei.GetHelpContext())
# Calling `GetErrorInfo` clears the error state for the thread.
# Therefore, `None` is returned in the second call.
self.assertIsNone(errorinfo.GetErrorInfo())

def test_without_optional_args(self):
self.assertIsNone(errorinfo.GetErrorInfo())
errmsg = "sample unexpected error message for tests"
hr = errorinfo.ReportError(errmsg, shelllink.IShellLinkW._iid_)
self.assertEqual(hres.DISP_E_EXCEPTION, hr)
pei = errorinfo.GetErrorInfo()
self.assertIsNotNone(pei)
self.assertEqual(shelllink.IShellLinkW._iid_, pei.GetGUID())
self.assertIsNone(pei.GetSource())
self.assertEqual(errmsg, pei.GetDescription())
self.assertIsNone(pei.GetHelpFile())
self.assertEqual(0, pei.GetHelpContext())

def test_error_has_not_been_set(self):
self.assertIsNone(errorinfo.GetErrorInfo())
pei = errorinfo.GetErrorInfo()
self.assertIsNone(pei)


def raise_runtime_error():
_raise_runtime_error()


def _raise_runtime_error():
raise RuntimeError("for testing")


class Test_ReportException(ut.TestCase):
def test_without_stacklevel(self):
self.assertIsNone(errorinfo.GetErrorInfo())
iid = shelllink.IShellLinkW._iid_
try:
raise_runtime_error()
except RuntimeError:
hr = errorinfo.ReportException(hres.E_UNEXPECTED, iid)
self.assertEqual(hres.E_UNEXPECTED, hr)
pei = errorinfo.GetErrorInfo()
self.assertIsNotNone(pei)
self.assertEqual(shelllink.IShellLinkW._iid_, pei.GetGUID())
self.assertIsNone(pei.GetSource())
self.assertEqual("<class 'RuntimeError'>: for testing", pei.GetDescription())
self.assertIsNone(pei.GetHelpFile())
self.assertEqual(0, pei.GetHelpContext())
self.assertIsNone(errorinfo.GetErrorInfo())

def test_with_stacklevel(self):
self.assertIsNone(errorinfo.GetErrorInfo())
stem = "<class 'RuntimeError'>: for testing"
iid = shelllink.IShellLinkW._iid_
for slv, text in [
# XXX: If the codebase changes, the line where functions or
# methods are defined will change, meaning this test is brittle.
(0, f"{stem} ({__name__}, line 93)"),
(1, f"{stem} ({__name__}, line 53)"),
(2, f"{stem} ({__name__}, line 57)"),
]:
with self.subTest(text=text):
try:
raise_runtime_error()
except RuntimeError:
errorinfo.ReportException(hres.E_UNEXPECTED, iid, stacklevel=slv)
pei = errorinfo.GetErrorInfo()
self.assertEqual(text, pei.GetDescription())
self.assertIsNone(errorinfo.GetErrorInfo())
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ ignore = ["E402"]
"comtypes/_npsupport.py" = ["F401"]
"comtypes/_vtbl.py" = ["E722"]
"comtypes/automation.py" = ["F401", "F403", "F405"]
"comtypes/errorinfo.py" = ["F403", "F405"]
"comtypes/git.py" = ["F401", "F403", "F405"]
"comtypes/util.py" = ["F403", "F405"]
"comtypes/viewobject.py" = ["F403", "F405"]
Expand Down