[python-win32] COM: Parameterless functions seen as properties
Mark Hammond
mhammond at skippinet.com.au
Tue Sep 16 01:06:01 CEST 2008
It looks like 2 things are going on:
* Your object isn't returning typelib info to Python when it asks. If your
IDispatch returned something from GetTypeInfo() we could use, this wouldn't
happen.
* Without a typelib, Python can't tell if a reference to 'a.foo' is going to
end up being a method call or not (ie, from Python's POV, a.foo() is just a
property fetch for 'foo' and then calling the result.) When making a
property reference, win32com checks for the ERRORS_BAD_CONTEXT values
defined in win32com\client\dynamic.py - if it sees one of those errors, it
assumes it really hit a method and handles it accordgingly. But - in your
case, your object is succeeding! The simplest thing to do here is to have
your object refuse to work as a property using one of the standard error
codes, and Python will then try it as a method.
Mark
From: python-win32-bounces+skippy.hammond=gmail.com at python.org
[mailto:python-win32-bounces+skippy.hammond=gmail.com at python.org] On Behalf
Of Obendorf, Keston
Sent: Tuesday, 16 September 2008 8:19 AM
To: python-win32 at python.org
Subject: [python-win32] COM: Parameterless functions seen as properties
I'm doing COM development in both Python and C#. Things are working well,
but I'm running into an issue where trying to get parameterless function
returns the return value of that function, which Python then tries to call,
causing an error.
The C# class is an implementation of an IDL-defined interface.
//IDL
interface IService : IDispatch
{
[id(101)] HRESULT foo([out, retval] BSTR * pstr);
}
//C#
[ComImport]
[Guid("/*GUID of IService*/")]
interface IService{}
[ComVisible(true)]
[ProgId("bar")]
[Guid("Valid GUID")]
public class bar : IService
{
public bar(){}
public string foo()
{
return "foo";
}
}
Now, getting a Dispatch of "bar" in Python is where the issue lies.
from win32com.client import Dispatch
bar = Dispatch("bar")
str = bar.foo()
>>TypeError: 'unicode' object is not callable
This is only a problem with the C# class. If the interface is implemented in
Pythong, the above code will work.
Now, this issue can be fixed by adding in _FlagAsMethod before calling
bar.foo()
bar._FlagAsMethod("foo")
I really don't want to have to do that every time I try to call a
parameterless method of a C# COM-object from Python. That's a huge headache,
especially because I am trying to keep the language any one component is
written in a non-issue.
I've started to dive into the source code of dynamic.py and other Client
modules to try and figure out how I can make this distinction at
Dispatch-time, but it's rough going understanding everything that's going on
in there. Does anyone have any pointers or a straight up solution to this
problem?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-win32/attachments/20080916/eaf323be/attachment-0001.htm>
More information about the python-win32
mailing list