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

comtypes com object's method returns: 'tuple' object has no attribute '__ctypes_from_outparam__'

我正在使用 comtypes 和方法 returns 调用带有字符串参数的 COM 对象的方法(它应该 return 一个 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

这是 comtypes 中的错误,请在此处查看修复:

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

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 并再次 运行 您的程序。