Skip to content

Commit c5a30d2

Browse files
authored
Fix DispatchWithEvents and WithEvents throw on 2nd call (#2491)
1 parent 03e65bc commit c5a30d2

File tree

2 files changed

+20
-19
lines changed

2 files changed

+20
-19
lines changed

CHANGES.txt

+3-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ https://mhammond.github.io/pywin32_installers.html .
1313

1414
Coming in build 310, as yet unreleased
1515
--------------------------------------
16-
* Drop support for Vista, set Windows 7 as the minimal Windows version. (#, @Avasam)
17-
* Restores many IIDs in `win32com(ext).shell.shell`. See #2486 for details.
16+
* Fixed a regression where `win32com.client.DispatchWithEvents` and win32com.client.WithEvents` would throw a `TypeError` on the second call (#2491, @Avasam)
17+
* Dropped support for Vista, set Windows 7 as the minimal Windows version. (#2487, @Avasam)
18+
* Restored many IIDs in `win32com(ext).shell.shell`. See #2486 for details. (#2487, @Avasam)
1819

1920
Build 309, released 2025/03/09
2021
------------------------------

com/win32com/client/__init__.py

+17-17
Original file line numberDiff line numberDiff line change
@@ -269,23 +269,24 @@ def __get_disp_and_event_classes(dispatch):
269269
disp = Dispatch(dispatch)
270270

271271
if disp.__class__.__dict__.get("CLSID"):
272-
return disp.__class__
272+
disp_class = disp.__class__
273+
else:
274+
# Eeek - no makepy support - try and build it.
275+
error_msg = "This COM object can not automate the makepy process - please run makepy manually for this object"
276+
try:
277+
ti = disp._oleobj_.GetTypeInfo()
278+
disp_clsid = ti.GetTypeAttr()[0]
279+
tlb, index = ti.GetContainingTypeLib()
280+
tla = tlb.GetLibAttr()
281+
gencache.EnsureModule(tla[0], tla[1], tla[3], tla[4], bValidateFile=0)
282+
# Get the class from the module.
283+
disp_class = gencache.GetClassForProgID(str(disp_clsid))
284+
except pythoncom.com_error as error:
285+
raise TypeError(error_msg) from error
286+
287+
if disp_class is None:
288+
raise TypeError(error_msg)
273289

274-
# Eeek - no makepy support - try and build it.
275-
error_msg = "This COM object can not automate the makepy process - please run makepy manually for this object"
276-
try:
277-
ti = disp._oleobj_.GetTypeInfo()
278-
disp_clsid = ti.GetTypeAttr()[0]
279-
tlb, index = ti.GetContainingTypeLib()
280-
tla = tlb.GetLibAttr()
281-
gencache.EnsureModule(tla[0], tla[1], tla[3], tla[4], bValidateFile=0)
282-
# Get the class from the module.
283-
disp_class = gencache.GetClassForProgID(str(disp_clsid))
284-
except pythoncom.com_error as error:
285-
raise TypeError(error_msg) from error
286-
287-
if disp_class is None:
288-
raise TypeError(error_msg)
289290
# Get the clsid
290291
clsid = disp_class.CLSID
291292
# Create a new class that derives from 2 classes:
@@ -333,7 +334,6 @@ class object that derives from three classes:
333334
>>> ie = DispatchWithEvents("InternetExplorer.Application", IEEvents)
334335
>>> ie.Visible = 1
335336
Visible changed: 1
336-
>>>
337337
"""
338338
disp, disp_class, events_class = __get_disp_and_event_classes(clsid)
339339
result_class = type(

0 commit comments

Comments
 (0)