简体   繁体   English

如何获取给定 C++ 指针的 PyBindGen PyObject?

[英]How to get the PyBindGen PyObject for a given C++ pointer?

Given the PyObject created by PyBindGen , it's easy to get the pointer to the wrapped C++ object, just use the obj member in the struct:鉴于PyBindGen创建的PyObject ,很容易获得指向包装的 C++ object 的指针,只需在结构中使用obj成员:

typedef struct {
    PyObject_HEAD
    MyWrappedClass *obj;
    PyObject *inst_dict;
    PyBindGenWrapperFlags flags:8;
} PyMyWrappedClass;

However, let's say I have just MyWrappedClass* , and want to get the PyObject (if any, it might not exist) that wraps it.但是,假设我只有MyWrappedClass* ,并且想要获取包装它的PyObject (如果有,它可能不存在)。 Is there any way to do this without maintaining my own dictionary of backpointers?有没有办法在不维护我自己的反向指针字典的情况下做到这一点?

I would have added this as a comment if I had enough reputation as I'm not familiar with PyBindGen , but unless inst_dict is a dictionary of backpointers (as the name might suggest) I think you're out of luck (Unless you want to trawl through the Python heap to see if there are any instances of your class there).如果我有足够的声誉,我会将此添加为评论,因为我不熟悉PyBindGen ,但除非inst_dict是一个反向指针字典(顾名思义)我认为你不走运(除非你想通过 Python 堆来查看是否有任何 class 实例)。

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

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