I want to pass a list to a COM method that expects a VARIANT. I found various
references to dealing with VARIANTs when using pythoncom but am still lost.
The PythonCOM.html file in the win32com/HTML dir says:
The internal framework always converts all variants to and from Python
types.
I took this to mean that all coversions are transparent. Passing the list
as-is to the method results in an "Undefined error" exception.
The PythonCOM.html file goes on to say:
For simple calls, there are 2 helpers available which will convert to and
from PyObjects and VARIANTS. The call to convert a Python object to a
VARIANT is simple in that it returns a VARIANT of the most appropriate type
for the Python object - ie, the type of the Python object determines the
resulting VARIANT type.
Does this mean that the PyObject to VARIANT conversion has to be explicitly
done via the helper mentioned above? If so, where are these helpers and what
are they called? I searched through the all the files in the distribution but
could not find anything remotely similar.
I realise that this may be extremely easy to do but I can't figure out how to
do this. Past messages on DejaNews also don't seem to provide a clear answer.
Any pointers on how to do this are much appreciated.
Thanks and regards,
- 'shal
> I want to pass a list to a COM method that expects a VARIANT. I found
various
> references to dealing with VARIANTs when using pythoncom but am still
lost.
> The PythonCOM.html file in the win32com/HTML dir says:
>
> The internal framework always converts all variants to and from Python
> types.
>
> I took this to mean that all coversions are transparent. Passing the list
> as-is to the method results in an "Undefined error" exception.
If you pass a list of Python objects, they will be converted to a
"SAFEARRAY". As long as you have a recent build, safearray support is
pretty complete (unless you are trying to pass an n dimensional, where n >
1!)
Is the error raised by Pythoncom, or the COM server itself - ie, is it a
COM exception, or a "standard" Python exception. If the former, then
possibly pythoncom is indeed doing the right thing...
> The PythonCOM.html file goes on to say:
>
> For simple calls, there are 2 helpers available which will convert to
and
> from PyObjects and VARIANTS. The call to convert a Python object to a
> VARIANT is simple in that it returns a VARIANT of the most appropriate
type
> for the Python object - ie, the type of the Python object determines the
> resulting VARIANT type.
>
> Does this mean that the PyObject to VARIANT conversion has to be
explicitly
> done via the helper mentioned above? If so, where are these helpers and
what
> are they called? I searched through the all the files in the distribution
but
> could not find anything remotely similar.
These are C API's, and the documentation is for people writing extension
modules who need to do this. If you are doing everything from Python, then
this is not really relevant.
Mark.
Thanks for the prompt response.
In article <01be1e4d$6b016080$0801a8c0@bobcat>, "Mark Hammond"
<mham...@skippinet.com.au> wrote:
>Harshal Chhaya <hch...@junkTHIS.ti.com> wrote in article
><74497h$a4j$1...@superb.csc.ti.com>...
>
>> I want to pass a list to a COM method that expects a VARIANT. I found various
>If you pass a list of Python objects, they will be converted to a
>"SAFEARRAY". As long as you have a recent build, safearray support is
>pretty complete (unless you are trying to pass an n dimensional, where n >
>1!)
I have the last stable version of PythonCOM from python.org, build 109, I
think.
My Python code is:
myVar = win32com.client.dynamic.Dispatch("TIVar.TIVar.1")
i = []
for line in fileinput.input():
elem = float(string.atoi(line))
i.append(elem)
myVar.SetValue(i)
# Things are OK till here. It is when I try to use myVar when things
go awry
myLink.SendVariable(myVar) #myLink is another COM object
similar to myVar
and the C++ code fragment that does this is:
VARIANT vInfo;
TSafeArrayUtil::CopyDoubleArrayIntoSafeArray(pVarList->pData, &vInfo,
pVarList->lCount);
IVar* pVar = NULL;
pVar->SetValue(vInfo);
>Is the error raised by Pythoncom, or the COM server itself - ie, is it a
>COM exception, or a "standard" Python exception. If the former, then
>possibly pythoncom is indeed doing the right thing...
The error I get is:
Traceback (innermost last):
File "serial_test.py", line 93, in ?
if __name__=="__main__": test()
File "serial_test.py", line 87, in test
init_stuff()
File "serial_test.py", line 32, in init_stuff
myLink.SendVariable(myVar)
File "<COMObject TI83Talk.TI83Talk.1>", line 2, in SendVariable
File "C:\Program Files\Python\win32com\client\dynamic.py", line 176, in
_ApplyTypes_
result = apply(self._oleobj_.InvokeTypes,
pywintypes.com_error: (-2147467259, 'Unspecified error', (0, None, None, None,
0, -2147467259), None)
[..]
>These are C API's, and the documentation is for people writing extension
>modules who need to do this. If you are doing everything from Python, then
>this is not really relevant.
Oh, OK. Thanks for the clarification. The documentation (the HTML files and
your PPT presentation on PythonCOM) suggest that VARIANTs should be handled
transparently. I guess I am being too simplistic here. Am I missing something
obvious?
Thanks again (for PythonCOM and this answer),
- 'shal
>Mark.
Harshal Chhaya <hch...@junkTHIS.ti.com> wrote in article
<7454j1$i98$1...@superb.csc.ti.com>...
> and the C++ code fragment that does this is:
>
> VARIANT vInfo;
> TSafeArrayUtil::CopyDoubleArrayIntoSafeArray(pVarList->pData,
&vInfo,
> pVarList->lCount);
> IVar* pVar = NULL;
> pVar->SetValue(vInfo);
>
I assume this code is assuming we have a contiguous array of doubles.
This, unfortunately, will not be true. While there are safearray helpers
to build these, PythonCOM does not use them. Arguably it should, but it
doesnt. PythonCOM will fill an array of variants, rather than array of
"native" doubles.
> result = apply(self._oleobj_.InvokeTypes,
> pywintypes.com_error: (-2147467259, 'Unspecified error', (0, None, None,
None,
> 0, -2147467259), None)
This simply means the COM server has returned E_FAIL from the call. And my
comments above would explain that!
It appears you have 2 choices:
* Get the COM server to handle SAFEARRAYs of VARIANTs
* Think about a patch to PythonCOM to handle this. I will help, but cant
offer too much at this time!
> >These are C API's, and the documentation is for people writing extension
> >modules who need to do this. If you are doing everything from Python,
then
> >this is not really relevant.
>
> Oh, OK. Thanks for the clarification. The documentation (the HTML files
and
> your PPT presentation on PythonCOM) suggest that VARIANTs should be
handled
> transparently. I guess I am being too simplistic here. Am I missing
something
> obvious?
Nope - they _should_ be transparent from Python, and usually they are.
However, this is one example of a limitation in PythonCOM that requires
fixing...
> Thanks again (for PythonCOM and this answer),
My pleasure!
Mark.
..
>It appears you have 2 choices:
>* Get the COM server to handle SAFEARRAYs of VARIANTs
>* Think about a patch to PythonCOM to handle this. I will help, but cant
>offer too much at this time!
I tried for option (1) but the people doing the COM stuff are not too keen on
modifying their code ("Works great with VB."). I would like to take a stab at
(2) but have no idea where to begin. I realise that you are busy with other
things but if you could point me to the files/methods that will need to be
examined and extended/modified, I'll attempt to do this.
Thanks,
- 'shal
How would you feel about joining the "pycom-developers" mailing list (even
temporarily) to discuss this? This mailing list is currently very low
volume, so you wont get swamped. There we can discuss some ideas I have
for implementing this without contributing noise to the main newsgroup. I
do have an idea which would be efficient, and not make worse some already
ugly code...
You can join the mailing list at http://www.pythonpros.com/maillists.html
Send me a quick note when you have subscribed, and I will kick it off....
Mark.