How to dynamically create a derived type in the Python C-API

Ethan Furman ethan at stoneleaf.us
Fri Nov 11 13:47:13 EST 2011


Asking on behalf of Sven Marnach:

---------------------------------------------------------------------
Assume we have the type Noddy as defined in the tutorial on writing C 
extension modules for Python. Now we want to create a derived type, 
overwriting only the __new__() method of Noddy.

Currently I use the following approach (error checking stripped for 
readability):

PyTypeObject *BrownNoddyType =
     (PyTypeObject *)PyType_Type.tp_alloc(&PyType_Type, 0);
BrownNoddyType->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE;
BrownNoddyType->tp_name = "noddy.BrownNoddy";
BrownNoddyType->tp_doc = "BrownNoddy objects";
BrownNoddyType->tp_base = &NoddyType;
BrownNoddyType->tp_new = BrownNoddy_new;
PyType_Ready(BrownNoddyType);

This works, but I'm not sure if it is The Right Way To Do It. I would 
have expected that I have to set the Py_TPFLAGS_HEAPTYPE flag, too, 
because I dynamically allocate the type object on the heap, but doing so 
leads to a segfault in the interpreter.

I also thought about explicitly calling type() using PyObject_Call() or 
similar, but I discarded the idea. I would need to wrap the function 
BrownNoddy_new() in a Python function object and create a dictionary 
mapping __new__ to this function object, which seems silly.

What is the best way to go about this? Is my approach correct? Is there 
an interface function I missed?
---------------------------------------------------------------------

~Ethan~



More information about the Python-list mailing list