Skip to content

Commit b29c581

Browse files
committed
test: Add error reporting tests for custom COMObject implementations.
- Add `Test_CustomImplementation` to `test_comobject.py`. - Verify that `ReportError` correctly raises `COMError` with expected `hresult` and `details` for custom `COMObject` methods.
1 parent 9c5791b commit b29c581

1 file changed

Lines changed: 56 additions & 2 deletions

File tree

comtypes/test/test_comobject.py

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import ctypes
22
import unittest as ut
3-
from ctypes import POINTER, byref, pointer
3+
from _ctypes import COMError
4+
from ctypes import POINTER, FormatError, byref, pointer
45
from unittest import mock
56

67
import comtypes.client
7-
from comtypes import CLSCTX_SERVER, COMObject, IPersist, IUnknown, hresult
8+
from comtypes import CLSCTX_SERVER, GUID, COMObject, IPersist, IUnknown, hresult
89
from comtypes._post_coinit.misc import _CoCreateInstance
910
from comtypes.automation import IDispatch
11+
from comtypes.errorinfo import ReportError
12+
from comtypes.server import IClassFactory
1013
from comtypes.typeinfo import GUIDKIND_DEFAULT_SOURCE_DISP_IID
1114

1215
comtypes.client.GetModule("UIAutomationCore.dll")
@@ -157,3 +160,54 @@ def test_com_pointers(self):
157160
self.assertNotIn(IDispatch._iid_, cuia._com_pointers_)
158161
self.assertIn(stdole.IPictureDisp._iid_, stdpic._com_pointers_)
159162
self.assertIn(uiac.IUIAutomation._iid_, cuia._com_pointers_)
163+
164+
165+
class Test_CustomImplementation(ut.TestCase):
166+
def test_raises_comerror(self):
167+
ERR_DESC = "Simulated COMError"
168+
ERR_HELPFILE = "test.hlp"
169+
ERR_HELPCTX = 42
170+
171+
class MyClassFactory(COMObject):
172+
_com_interfaces_ = [IClassFactory]
173+
174+
def CreateInstance(self, this, punkOuter, riid, ppv):
175+
return ReportError(
176+
f"{ERR_DESC}: CreateInstance",
177+
IClassFactory._iid_,
178+
GUID.create_new(),
179+
ERR_HELPFILE,
180+
ERR_HELPCTX,
181+
hresult.E_UNEXPECTED,
182+
)
183+
184+
def LockServer(self, this, fLock):
185+
return ReportError(
186+
f"{ERR_DESC}: LockServer",
187+
IClassFactory._iid_,
188+
GUID.create_new(),
189+
ERR_HELPFILE,
190+
ERR_HELPCTX,
191+
hresult.E_FAIL,
192+
)
193+
194+
# Get a COM pointer to the interface of our custom object.
195+
cf = MyClassFactory().QueryInterface(IClassFactory)
196+
# calling `LockServer`
197+
with self.assertRaises(COMError) as cm:
198+
cf.CreateInstance(interface=IUnknown)
199+
self.assertEqual(cm.exception.hresult, hresult.E_UNEXPECTED)
200+
self.assertEqual(cm.exception.text, FormatError(hresult.E_UNEXPECTED))
201+
self.assertEqual(
202+
cm.exception.details,
203+
(f"{ERR_DESC}: CreateInstance", None, ERR_HELPFILE, ERR_HELPCTX, None),
204+
)
205+
# calling `LockServer`
206+
with self.assertRaises(COMError) as cm:
207+
cf.LockServer(True)
208+
self.assertEqual(cm.exception.hresult, hresult.E_FAIL)
209+
self.assertEqual(cm.exception.text, FormatError(hresult.E_FAIL))
210+
self.assertEqual(
211+
cm.exception.details,
212+
(f"{ERR_DESC}: LockServer", None, ERR_HELPFILE, ERR_HELPCTX, None),
213+
)

0 commit comments

Comments
 (0)