[python-win32] More win32com woes: Wrapping COM objects

Celvin read.beyond.data at gmx.net
Fri Nov 6 01:09:29 CET 2009


Hi,

while my recent problem got solved due to Mark being of great help,
interacting with COM from Python seems to bring up a few problems I
hadn't thought about at first. This is what is taxing my brain at the
moment:

I would like to wrap a COM object instance with a Python class in
order to manage the COM class and additional data associated with that
class, but I have trouble accessing the wrapper class from the COM event
instance. Code is something like this:

class event_handler:
    def OnSomething(self, param):
       c = someclass(param)
       obj = c.method()
       self.register(obj)
       # self.register won't work, as self obviously has no attribute
       # register, additionally, I'm worried about circular references

class application(object):
    def __init__(self):
        self.__com_obj = DispatchWithEvents("{GUID}", event_handler)
        self.__objects = []

    def register(self, obj):
        self.__objects.append(obj)

Basically, I'm trying to manage a list of objects returned via a call
to a COM object, so that I can access them at later times in Python
code and to keep them referenced so that the event handler won't get
disconnected when Python determines the object is no longer in use.
(getevents() alone isn't an option here as it wouldn't solve the problem
of accessing the application object everywhere I need it).

The real application class is obviously more complex and contains data
I need in the event handler. First thing is, I can't think of a clean
way to provide event_handler with a reference to an application
instance without using globals (ugly), and second, even if I could access
an application instance in event_handler, I'm wondering if this wouldn't
create a circular reference (i.e. the event handler class would maintain
a reference to the COM object via application.__com_obj, while the COM
object maintains a reference to the event handler, established via
DispatchWithEvents - I don't know if the EventProxy class would
somehow prevent this).

So: Long story short, anyone knows a clean way to access my
application instance in the event_handler class?

Any help would be _really_ appreciated.

Regards,
Celvin



More information about the python-win32 mailing list