comtypes com对象的方法返回:'tuple'对象没有属性'__ctypes_from_outparam__'

时间:2015-09-30 14:43:42

标签: python com ctypes pywin32 comtypes

我正在使用comtypes调用带有字符串参数的COM对象的方法,并返回方法(它应该返回一个COM字符串):

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-102-009507ff0086> in <module>()
----> 1 obj1=xobjData.GetDataType('string_name')

C:\Python\Python27\lib\site-packages\comtypes\__init__.pyc in call_with_inout(self_, *args, **kw)
    657             # be iterable.
    658             if len(outargs) == 1:  # rescode is not iterable
--> 659                 return rescode.__ctypes_from_outparam__()
    660 
    661             rescode = list(rescode)

AttributeError: 'tuple' object has no attribute '__ctypes_from_outparam__'

似乎非常神秘的错误,任何帮助?

%debug魔术显示如下:

> c:\python\python27\lib\site-packages\comtypes\__init__.py(659)call_with_inout()
    658             if len(outargs) == 1:  # rescode is not iterable
--> 659                 return rescode.__ctypes_from_outparam__()
    660 

ipdb> outargs
{0: VARIANT(vt=0x8, u'string_name')}
ipdb> rescode
(VARIANT(vt=0x8, u'string_name'), u'Long')
ipdb> print(dir(outargs))
['__class__', '__cmp__', '__contains__', '__delattr__', '__delitem__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'has_key', 'items', 'iteritems', 'iterkeys', 'itervalues', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values', 'viewitems', 'viewkeys', 'viewvalues']
ipdb> print(dir(rescode))
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'count', 'index']
ipdb> u
> <ipython-input-112-83ed14b8961f>(1)<module>()
----> 1 xobjData.GetDataType(u'string_name')

ipdb> d
> c:\python\python27\lib\site-packages\comtypes\__init__.py(659)call_with_inout()
    658             if len(outargs) == 1:  # rescode is not iterable
--> 659                 return rescode.__ctypes_from_outparam__()
    660 

ipdb> exit

2 个答案:

答案 0 :(得分:1)

comtype包中存在一个错误,导致与元组相关的属性错误。可以通过执行以下步骤替换一小段代码来纠正它:

  1. 在Python根文件夹中,转到Lib \ site-packages \ comtypes \并打开文件__init__.py

  2. 转到第658行,您会找到以下代码“

        if len(outargs) == 1:  # rescode is not iterable
                return rescode.__ctypes_from_outparam__()
        rescode = list(rescode)
        for outnum, o in list(outargs.items()):
            try:
                rescode[outnum] = o.__ctypes_from_outparam__()
    
  3. 用以下内容替换上面的代码:

        if len(outargs) == 1:  # rescode is not iterable
            try:
                return rescode.__ctypes_from_outparam__()
            except:
                return rescode
        rescode = list(rescode)
        for outnum, o in list(outargs.items()):
            try:
                rescode[outnum] = o.__ctypes_from_outparam__()
            except:
                rescode[outnum] = o
    
  4. 保存文件__init__.py并重新运行程序。

答案 1 :(得分:0)

这是comtypes中的一个错误,请参阅此处的修复:

https://github.com/enthought/comtypes/issues/87