Skip to content

Commit 80f54cc

Browse files
committed
feat: Raise NotImplementedError for event interfaces lacking DISPIDs.
Replace `assert` in `CreateEventReceiver` with an explicit check for `dispid`, raising `NotImplementedError` with a message when DISPIDs are missing.
1 parent 89e5b79 commit 80f54cc

2 files changed

Lines changed: 10 additions & 5 deletions

File tree

comtypes/client/_events.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,15 @@ def _get_method_finder_(self, itf: type[IUnknown]) -> _MethodFinder:
262262
# Can dispid be at a different index? Should check code generator...
263263
# ...but hand-written code should also work...
264264
dispid = m.idlflags[0]
265-
assert isinstance(dispid, comtypes.dispid)
265+
if not isinstance(dispid, comtypes.dispid):
266+
# The interface is a subclass of `IDispatch` but its methods do not
267+
# have DISPIDs, indicating it's not an interface suitable for event
268+
# handling.
269+
raise NotImplementedError(
270+
"Event receiver creation requires event methods to have DISPIDs "
271+
f"for dispatching, but '{interface.__name__}' ({interface._iid_}) "
272+
"lacks them, even though it inherits from 'IDispatch'."
273+
)
266274
impl = finder.get_impl(interface, m.name, m.paramflags, m.idlflags)
267275
# XXX Wouldn't work for 'propget', 'propput', 'propputref'
268276
# methods - are they allowed on event interfaces?

comtypes/test/test_eventinterface.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,7 @@ def test(self):
146146
# `DFileSystemImageEvents`. Although it inherits from `IDispatch`,
147147
# it is a custom interface (`TKIND_INTERFACE`), not a dual or pure
148148
# dispatch interface (`TKIND_DISPATCH`).
149-
# Its methods are v-table bound, and the interface definition
150-
# that comtypes generates from the type info lacks the `dispid`
151-
# attributes that `GetEvents` requires.
152-
with self.assertRaises(AssertionError):
149+
with self.assertRaises(NotImplementedError):
153150
GetEvents(self.image, sink)
154151

155152

0 commit comments

Comments
 (0)