'Python embedding: How can i create a new type dynamically by c meta class in heap?

I have some new types in heap supposed to be created dynamically by meta class whose declaration may like below:

// we have meta class
PyTypeObject HeapTypeMeta {
    ...
    .tp_new = HeapType__new__
    ...
}
// and we have heap type that is desired to be created dynamically by HeapTypeMeta
class HeapType {
public:
    PyHeapTypeObject base_type;
    std::unordered_map<int, Stuff> stuffs;
};

The question is: how can i create and init the type properly?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source