1

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?

Walter Nissen
  • 16,451
  • 4
  • 26
  • 27
  • Why not save a pointer in the `MyWrapperClass` to the according `PyMyWrappedClass`? – Constantinius Jun 13 '11 at 19:04
  • I'd prefer to not burden the class with knowledge of the wrapper. I'm going to have hundreds of wrapped classes, so rather than doctor each of them I want a mechanism that works for any class. Thus keeping a hash or map or something from pointers to PyObjects. – Walter Nissen Jun 13 '11 at 21:19

1 Answers1

-1

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).

user786653
  • 29,780
  • 4
  • 43
  • 53