简体   繁体   English

访问python对象指针数据

[英]accessing python object pointer data

I have a python set that contains a collection of non-hashable python objects with uniform type which I want to process. 我有一个python set ,其中包含我要处理的具有统一类型的非哈希python对象的集合。

To improve efficiency of my algorithms, I would like to interface using ctypes with an external index implementation that accepts only uint64 as data values. 为了提高算法效率,我想使用ctypes与仅接受uint64作为数据值的外部索引实现进行接口。

I was hoping that I could to pass pointer references to the python object into this external library as uint64 ? 我希望我可以将指向python对象的指针引用作为uint64到此外部库中?

I tried ctypes.cast(ctypes.py_object(my_python_object), ctypes.c_uint64) but am getting ctypes.ArgumentError: argument 1: <class 'TypeError'>: wrong type . 我尝试了ctypes.cast(ctypes.py_object(my_python_object), ctypes.c_uint64)但正在获取ctypes.ArgumentError: argument 1: <class 'TypeError'>: wrong type

Also, what about the reverse, getting a reference to a python object as uint64 and turning it into a "real" python object? 另外,相反,将python对象引用为uint64并将其转换为“真实” python对象呢?

Why wouldn't you simply use the id() function in CPython? 为什么不简单地在CPython中使用id()函数呢?

>>> x
<object object at 0x7fd2fc742090>
>>> hex(id(x))
'0x7fd2fc742090'

The CPython documentation of id() says that CPython id()文档指出

id(object)

Return the “identity” of an object. 返回对象的“身份”。 This is an integer which is guaranteed to be unique and constant for this object during its lifetime. 这是一个整数,可以保证在此对象的生存期内唯一且恒定。 Two objects with non-overlapping lifetimes may have the same id() value. 具有不重叠生存期的两个对象可能具有相同的id()值。

CPython implementation detail: This is the address of the object in memory. CPython实现细节: 这是对象在内存中的地址。


You also need to mess with the reference counts and such, if you're to "convert" this uint64_t of yours back to a Python object. 如果要将“ uint64_t ”转换为Python对象,还需要弄乱引用计数等。 As far as I know, ctypes do not easily let one to increase/decrease the reference counts of Python 据我所知, ctypes不会轻易让一个人增加/减少Python的引用计数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM