|
1 | 1 | import ctypes |
2 | 2 | import unittest as ut |
3 | | -from ctypes import POINTER, byref, pointer |
| 3 | +from _ctypes import COMError |
| 4 | +from ctypes import POINTER, FormatError, byref, pointer |
4 | 5 | from unittest import mock |
5 | 6 |
|
6 | 7 | import comtypes.client |
7 | | -from comtypes import CLSCTX_SERVER, COMObject, IPersist, IUnknown, hresult |
| 8 | +from comtypes import CLSCTX_SERVER, GUID, COMObject, IPersist, IUnknown, hresult |
8 | 9 | from comtypes._post_coinit.misc import _CoCreateInstance |
9 | 10 | from comtypes.automation import IDispatch |
| 11 | +from comtypes.errorinfo import ReportError |
| 12 | +from comtypes.server import IClassFactory |
10 | 13 | from comtypes.typeinfo import GUIDKIND_DEFAULT_SOURCE_DISP_IID |
11 | 14 |
|
12 | 15 | comtypes.client.GetModule("UIAutomationCore.dll") |
@@ -157,3 +160,55 @@ def test_com_pointers(self): |
157 | 160 | self.assertNotIn(IDispatch._iid_, cuia._com_pointers_) |
158 | 161 | self.assertIn(stdole.IPictureDisp._iid_, stdpic._com_pointers_) |
159 | 162 | 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