Skip to content

Commit 258da0e

Browse files
authored
Enhance tests verifying custom COMObjects raise errors. (#929)
* 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. * refactor: Replace `clsid` with a class variable.
1 parent 9c5791b commit 258da0e

1 file changed

Lines changed: 57 additions & 2 deletions

File tree

comtypes/test/test_comobject.py

Lines changed: 57 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,55 @@ 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+
_reg_clsid_ = GUID.create_new()
174+
175+
def CreateInstance(self, this, punkOuter, riid, ppv):
176+
return ReportError(
177+
f"{ERR_DESC}: CreateInstance",
178+
IClassFactory._iid_,
179+
self._reg_clsid_,
180+
ERR_HELPFILE,
181+
ERR_HELPCTX,
182+
hresult.E_UNEXPECTED,
183+
)
184+
185+
def LockServer(self, this, fLock):
186+
return ReportError(
187+
f"{ERR_DESC}: LockServer",
188+
IClassFactory._iid_,
189+
self._reg_clsid_,
190+
ERR_HELPFILE,
191+
ERR_HELPCTX,
192+
hresult.E_FAIL,
193+
)
194+
195+
# Get a COM pointer to the interface of our custom object.
196+
cf = MyClassFactory().QueryInterface(IClassFactory)
197+
# calling `LockServer`
198+
with self.assertRaises(COMError) as cm:
199+
cf.CreateInstance(interface=IUnknown)
200+
self.assertEqual(cm.exception.hresult, hresult.E_UNEXPECTED)
201+
self.assertEqual(cm.exception.text, FormatError(hresult.E_UNEXPECTED))
202+
self.assertEqual(
203+
cm.exception.details,
204+
(f"{ERR_DESC}: CreateInstance", None, ERR_HELPFILE, ERR_HELPCTX, None),
205+
)
206+
# calling `LockServer`
207+
with self.assertRaises(COMError) as cm:
208+
cf.LockServer(True)
209+
self.assertEqual(cm.exception.hresult, hresult.E_FAIL)
210+
self.assertEqual(cm.exception.text, FormatError(hresult.E_FAIL))
211+
self.assertEqual(
212+
cm.exception.details,
213+
(f"{ERR_DESC}: LockServer", None, ERR_HELPFILE, ERR_HELPCTX, None),
214+
)

0 commit comments

Comments
 (0)