Skip to content
Merged
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
59 changes: 57 additions & 2 deletions comtypes/test/test_comobject.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import ctypes
import unittest as ut
from ctypes import POINTER, byref, pointer
from _ctypes import COMError
from ctypes import POINTER, FormatError, byref, pointer
from unittest import mock

import comtypes.client
from comtypes import CLSCTX_SERVER, COMObject, IPersist, IUnknown, hresult
from comtypes import CLSCTX_SERVER, GUID, COMObject, IPersist, IUnknown, hresult
from comtypes._post_coinit.misc import _CoCreateInstance
from comtypes.automation import IDispatch
from comtypes.errorinfo import ReportError
from comtypes.server import IClassFactory
from comtypes.typeinfo import GUIDKIND_DEFAULT_SOURCE_DISP_IID

comtypes.client.GetModule("UIAutomationCore.dll")
Expand Down Expand Up @@ -157,3 +160,55 @@ def test_com_pointers(self):
self.assertNotIn(IDispatch._iid_, cuia._com_pointers_)
self.assertIn(stdole.IPictureDisp._iid_, stdpic._com_pointers_)
self.assertIn(uiac.IUIAutomation._iid_, cuia._com_pointers_)


class Test_CustomImplementation(ut.TestCase):
def test_raises_comerror(self):
ERR_DESC = "Simulated COMError"
ERR_HELPFILE = "test.hlp"
ERR_HELPCTX = 42

class MyClassFactory(COMObject):
_com_interfaces_ = [IClassFactory]
_reg_clsid_ = GUID.create_new()

def CreateInstance(self, this, punkOuter, riid, ppv):
return ReportError(
f"{ERR_DESC}: CreateInstance",
IClassFactory._iid_,
self._reg_clsid_,
ERR_HELPFILE,
ERR_HELPCTX,
hresult.E_UNEXPECTED,
)

def LockServer(self, this, fLock):
return ReportError(
f"{ERR_DESC}: LockServer",
IClassFactory._iid_,
self._reg_clsid_,
ERR_HELPFILE,
ERR_HELPCTX,
hresult.E_FAIL,
)

# Get a COM pointer to the interface of our custom object.
cf = MyClassFactory().QueryInterface(IClassFactory)
# calling `LockServer`
with self.assertRaises(COMError) as cm:
cf.CreateInstance(interface=IUnknown)
self.assertEqual(cm.exception.hresult, hresult.E_UNEXPECTED)
self.assertEqual(cm.exception.text, FormatError(hresult.E_UNEXPECTED))
self.assertEqual(
cm.exception.details,
(f"{ERR_DESC}: CreateInstance", None, ERR_HELPFILE, ERR_HELPCTX, None),
)
# calling `LockServer`
with self.assertRaises(COMError) as cm:
cf.LockServer(True)
self.assertEqual(cm.exception.hresult, hresult.E_FAIL)
self.assertEqual(cm.exception.text, FormatError(hresult.E_FAIL))
self.assertEqual(
cm.exception.details,
(f"{ERR_DESC}: LockServer", None, ERR_HELPFILE, ERR_HELPCTX, None),
)