COM bites - help please

Bill Tutt billtut at microsoft.com
Fri Jul 23 07:44:07 EDT 1999


> From: Karl Putland [mailto:kperacles at geocities.com]

> I've got a couple of pyCOM things that are kicking my @$$! 
> 
> 1.  Trying to wrap a list of instances into a 
> util.Collection.  All instances have been prepped with 
> _public_methods_ and _public_attrs_ and have been wrapped 
> with a call to win32com.server.util.wrap(field) Trying to 
> access the Collection from a com instance produces the 
> following error.
> 
[...]
> >>> c1.fields        # This is a util.Collection(list of 
> GMField instances)
> Traceback (innermost last):
>   File "<pyshell#56>", line 1, in ?
>     c1.fields
>   File "D:\Python\win32com\client\dynamic.py", line 394, in 
> __getattr__
>     raise pythoncom.com_error, details
> com_error: (-2147467259, 'Unspecified error', None, None)
> 

Not having the source makes it more difficult to help here...
If memory serves given a list of Python instances that can be wrapped as COM
objects, the way to make a collection is:

collection = util.Collection(map(lambda x: util.wrap(x), list)

> 3. PyUnicode objects bit me in the ass when strings are 
> passed into methods and manipulated.
> class Foo:
>     <COM stuff>
>     def bar(self, somestring):
>         somestring = string.upper(somestring)
>         return somestring
> 
> >>>foo = win32com.client.Dispatch("Python.Foo")
> >>>foo.bar("Hello World")
> 

PyUnicode strings have an upper and a lower method so this becomes:
def bar(self, somestring):
	somestring = somestring.upper()
	return somestring

> Some traceback about TypeError PyUnicode.
> 
> Causing a need to change bar to
>     def bar(self, somestring):
>         somestring=str(somestring)
>         somestring = string.upper(somestring)
>         return somestring
> This change needs to be made in ALL methods that accept and 
> manipulate strings???
> 

This is the bane of Unicode, the string module can't handle them.
You either have to pass Unicode into functions that like Unicode, or convert
the Unicode into a normal Python string. Not much else you can do about
this.
It's been rumored that Unicode support will get better in Python 1.6.

> Is there a better method than COM for allowing someone who 
> uses VB access to Python objects, short of converting all of 
> the VB users to Python or resorting to reimplementing in VB 
> or C++?  

Using Python is good. :) But yes, this is the easiest way for Python and VB
to talk to each other.

> The COM interface I've been able to expose is 
> sufficient for now but does not allow some of the access that 
> I have in Python.  Maybe that's good in the end?
> 

I think so. :)

Hope this helps out a little bit...

Bill




More information about the Python-list mailing list