Python and COM: a bug?

Bill Tutt billtut at microsoft.com
Wed Nov 24 09:37:24 EST 1999


> From: Gaetan Corneau [mailto:Gaetan_Corneau at baan.com]
> Here is my original post:
> 
> 
> Hello,
> 
> I have encountered a weird problem a few times in the past months. I'm
> testing COM server (written here) with Python, and it seems 
> that there is
> something wrong in the code that optimizes the calls to COM objects.
> Sometimes (a very rare occurence) the wrong function gets 
> called when I
> access a property. Suppose I have object O with properties a 
> (an int), b (a
> date), c (a string), then
> 
> print O.b
> 
> would give me 
> "Hello, world"
> 
> The error always seem to be off by one (in the same 
> direction), but I'm not
> absolutely sure about that.
> 

How is this "O" written?
Is it an ATL C++ COM object that uses IDispatchImpl? (i.e. a dual interface)
Is it a C++ COM object that implements IDispatch on its own? (not a dual
interface)
Is it a C++ COM object that uses OLEAUT's builtin IDispatch, but just
doesn't use ATL? (i.e. a dual interface)

My hunch would either be a bug in Python dispid code, or your COM object has
a bug in some custom IDispatch code.

I also don't think we try to optimize any calls. COM doesn't give one enough
information to even bother optimizing anything other than caching Name ->
DispID mappings.

> And sometimes, I get something like that:
> 
> >>> Object
> <COMObject <unknown>>
> >>> P = Object.Parent # Parent is an attribute that is the object that
> created the current object
> >>> P
> <method CDispatch.Parent of CDispatch instance at c5e780>
> >>> P = Object.get_Parent()
> >>> P
> <COMObject <unknown>>
> >>>
> 

What really helps in cases like this is to have the IDL for your
dispatch/dual interface so that we can examine whats happening much easier.
:)

The otherthing to note is that the <COMObject <unknown>> occurs because you
aren't wrapping the typelibrary with Python helper classes. Using
win32com.client.EnsureModule("<typelib guid>") allows you to support a
couple of features that are impossible to deterimine without the typelib. I
forget exactly what those are off the top of my head.


> I have workarounds, but what really annoys me is that I'm 
> still fighting a
> couple of guys here to get Python accepted: they always ask 
> "why don't you
> just use VBScript?", and it's true that I don't have those 
> problems with
> VBScript (because VBScript doesn't try to optimize calls).
> 

The answer believe it or not is, "Python is the most powerful scripting
language on the planet for Win32 programming." its multi threaded, COM
happy, ActiveX Scripting Engine happy, and really easy to talk to. VBScript
on the other hand... Don't get me started... :) 

> I'll try to find what the problem is by tracing the Python 
> code, but since
> I'm not familiar with it, I don't expect to find the cause. 
> At this point,
> all I want to know is if I'm the only one with that kind of strange
> behavior...
> 

I've seen behavior like the above "Parent" issue before. 
I haven't ever seen the first behavior you mentioned...

Bill




More information about the Python-list mailing list