Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

#133 DispatchWithEvents fix for "new" style objects

Unstable (example)
open
Mark Hammond
5
2013-11-10
2013-11-10
brett
No

--- C:\Python27\Lib\site-packages\win32com\client_init.py.orig 2013-11-10 05:09:17.000000000 -0500
+++ C:\Python27\Lib\site-packages\win32com\client__init
.py 2013-11-08 04:43:17.000000000 -0500
@@ -258,16 +258,16 @@
disp_class = disp.class
# If the clsid was an object, get the clsid
clsid = disp_class.CLSID
# Create a new class that derives from 3 classes - the dispatch class, the event sink class and the user class.
# XXX - we are still "classic style" classes in py2x, so we need can't yet
# use 'type()' everywhere - revisit soon, as py2x will move to new-style too...
- try:
+ if isinstance(user_event_class,type):
+ new_type = type
+ else:
from types import ClassType as new_type
- except ImportError:
- new_type = type # py3k
events_class = getevents(clsid)
if events_class is None:
raise ValueError("This COM object does not support events.")
result_class = new_type("COMEventClass", (disp_class, events_class, user_event_class), {"setattr" : _event_setattr
})
instance = result_class(disp.oleobj) # This only calls the first base class init.
events_class.init(instance, instance)
@@ -314,16 +314,16 @@
else:
disp_class = disp.class
# Get the clsid
clsid = disp_class.CLSID
# Create a new class that derives from 2 classes - the event sink
# class and the user class.
- try:
+ if isinstance(user_event_class,type):
+ new_type = type
+ else:
from types import ClassType as new_type
- except ImportError:
- new_type = type # py3k
events_class = getevents(clsid)
if events_class is None:
raise ValueError("This COM object does not support events.")
result_class = new_type("COMEventClass", (events_class, user_event_class), {})
instance = result_class(disp) # This only calls the first base class init.
if hasattr(user_event_class, "init"):

Discussion

  • brett
    brett
    2013-11-10

    The above patch let me create an events class that derived from an object type instead of an old style class. If the input events class isn't derived from type(), using old style ClassType is probably fine.