获取 win32com 自动化对象的底层 OLE 对象标识 [英] getting underlying OLE object identity for win32com automation objects

查看:84
本文介绍了获取 win32com 自动化对象的底层 OLE 对象标识的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大多数内置 Python 数据类型和库都强调返回相同对象(a is b,而不仅仅是 a==bcode>) 即使您以不同的方式要求它.一个非常简单的例子:

Most built-in Python data types and libraries make a point of returning the same object (a is b, not just a==b) even if you ask for it in different ways. A very simple example:

list = [ "foo", "bar", {"name": [1,2,3]} ]
a = list[-1]["name"]
b = list[2].values()[0]
print (a is b) # True!

然而,对于 win32com 自动化返回的多种非标量对象,情况似乎并非如此.以下代码连接到 自动化,然后获取同一个数据表对象的两个句柄.在 Python 级别,这两个自动化对象不共享身份:

However, this doesn't seem to be the case for many kinds of non-scalar objects returned by win32com automation. The following code connects to sas-jmp automation and then gets two handles to the same data table object. At the Python level, these two automation objects don't share an identity:

from win32com.client import gencache
mod = gencache.GetModuleForProgID("JMP.Application")
app = mod.Application()
table = app.GetTableHandleFromName("Table1")
same_table = app.GetTableHandleFromName("Table1")
print table
print same_table
print table is same_table
# <win32com.gen_py.DCD36DE0-78F8-11CF-9E68-0020AF24E9FEx0x1x0.IAutoDataTable instance at 0x54418504>
# <win32com.gen_py.DCD36DE0-78F8-11CF-9E68-0020AF24E9FEx0x1x0.IAutoDataTable instance at 0x54432456>
# False

似乎所有 win32com OLE 自动化对象也具有 _oleobj_ 属性._oleobj_ 是一个 PyIDispatch 对象,它只有一些方法,它们似乎都与对象身份问题无关.但是,_oleobj_repr() 似乎指向了底层的 OLE 自动化对象:

It appears that all win32com OLE automation objects also have an _oleobj_ property. _oleobj_ is a PyIDispatch object, which only has a few methods, none of which seem pertinent to the question of object identity. However, the repr() of _oleobj_ seems to point to the underlying OLE automation object:

print table._oleobj_
print same_table._oleobj_
# <PyIDispatch at 0x0000000003459530 with obj at 0x00000000003E2928>
# <PyIDispatch at 0x0000000003459620 with obj at 0x00000000003E2928>

为了确认两个对象引用同一个底层 OLE 对象,我求助于解析 repr() 字符串并比较十六进制地址("obj at 0x...").

In order to confirm that two objects refer to the same underlying OLE object, I've resorted to parsing the repr() strings and comparing the hexadecimal addresses ("obj at 0x...").

是否有更好、更简洁的方法来使用 win32com 比较 OLE 对象标识?

Is there a better, cleaner way to compare OLE object identity using win32com?

推荐答案

*打自己脸*

事实证明,有一种非常简单的方法可以做到这一点:http://mail.python.org/pipermail/python-win32/2014-October/013288.html

It turns out there's quite an easy way to do this: http://mail.python.org/pipermail/python-win32/2014-October/013288.html

尽管 is 操作符不起作用,因为 Python 对象是不同的,== 对象通过 win32com 实现了这个目的-包装的对象:

Although the is operator does not work since the Python objects are distinct, the == object accomplishes this purpose with win32com-wrapped objects:

from win32com.client import gencache
mod = gencache.GetModuleForProgID("JMP.Application")
app = mod.Application()
table = app.GetTableHandleFromName("Table1")
same_table = app.GetTableHandleFromName("Table1")
print table is same_table, table._oleobj_ is same_table._oleobj_
print table==same_table, table._oleobj_==same_table._oleobj_
# False, False
# True, True

这篇关于获取 win32com 自动化对象的底层 OLE 对象标识的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆