[python-win32] wrapping objects from a COM server

Jonathan LaCour jonathan-lists at cleverdevil.org
Wed Jan 24 15:10:26 CET 2007


Mark Hammond wrote:

>> Well, thats basically what I am doing now, but these object
>> structures that are returned can be arbitrarily complex, with lists
>> of objects that contain other kinds of objects, which have lists
>> of even more objects.  Having to walk through arbitrary object
>> structures and look for lists/tuples of other objects, etc. is a bit
>> of a pain, and I am not sure if I can really do this reliably.
>
> You don't need to recurse deeply though.  If all your objects use the
> same policy, the sub-objects will work automatically

Really?  This is not my experience.  I am using whatever the default
policy is on all of my objects, and a test case very much like my
"Person" and "Title" example below fails unless I loop through the
`titles` attribute of my Person instances and manually wrap the Title
instances before returning the Person instance.

Am I doing something wrong?  Do I need to use a different policy to
make this happen?

>>      from win32com.server.util import wrap, register_wrapper
>>
>>      register_wrapper(Title, wrap)
>>      register_wrapper(Person, wrap)
>
> That makes sense - but such a facility doesn't exist.  It would work -
> but I'd be leaning towards the more explicit:
>
> class Title:
>   _com_wrap_ = wrap
>   _public_methods_ = [...]
>
> (although the name _com_wrap_ sucks :) This would be backwards
> compatible.  The C code in pythoncom would check for that special
> attribute and call it to perform a conversion into a COM object.

Yes, I think your way fits more into the convention that pythoncom
already sets.  The only downside is that its a bit more difficult to
register a wrapper function onto a pre-existing object (which is
basically what I am doing).

> It might get a little complicated though with respect to object
> identity.

Fair enough, but this wouldn't be an issue in my case.  I am only
passing back and forth instances that have a unique ID on them, and
use that for all comparisons.  But, I see your point :)

> But in general, this is something that I've always felt should
> be implemented in one way or another.  I don't have much time at
> the moment though, but would be happy to help if you wanted to
> investigate.

Depending on how far I get without it, I might just take you up on
that.  Thanks for all your help.

--
Jonathan LaCour
http://cleverdevil.org





More information about the Python-win32 mailing list