如何获取给定 C++ 的 PyBindGen PyObject指针?
给定由 PyBindGen
创建的 PyObject
,很容易获得指向包装的 C++ 对象的指针,只需在结构中使用 obj
成员即可:
typedef struct {
PyObject_HEAD
MyWrappedClass *obj;
PyObject *inst_dict;
PyBindGenWrapperFlags flags:8;
} PyMyWrappedClass;
但是,假设我只有 MyWrappedClass*
,并且想要获取包装它的 PyObject
(如果有,它可能不存在)。有没有办法在不维护我自己的反向指针字典的情况下做到这一点?
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:
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. Is there any way to do this without maintaining my own dictionary of backpointers?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我有足够的声誉,我会将其添加为评论,因为我不熟悉
PyBindGen
,但除非inst_dict
是反向指针的字典(正如名称所暗示的那样) )我认为你运气不好(除非你想遍历Python堆来查看那里是否有你的类的实例)。I would have added this as a comment if I had enough reputation as I'm not familiar with
PyBindGen
, but unlessinst_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).