PyType_GenericAlloc

Wavy Gravy wavy_gravy at worldnet.att.net
Thu Apr 18 10:13:27 EDT 2002


Greetings,
 I am trying to get familiar with the new Type/Class API. Browsing
through various sources (only few exist), I am trying to figure out
what exactly this function returns? I notice that this is the most
common fuction in the "tp_alloc" slot for your own builtin type.
Probably an example shoud clarify, ex. say you have your object as:

typedef struct {
  char *name;
} PyCatObject;

staticforward PyTypeObject PyCat_Type;

OLD_WAY Creation
-----------------
 PyObject_New(<PyCatObject pointer),(PyCat_Type pointer). And this
make sense
 because I can see the return type being PyCatObject *.

NEW_WAY (2.2)
------------
 fill the tp_new slot with your function and then in general fill the
tp_alloc slot with PyType_GenericAlloc. And then in your tp_new
function in general make a call to type->tp_alloc(type,0).
 So in the example above, what I am not able to figure out is how does
the PyType_GenericAlloc know to return PyCatObject * ? I can see that
it knows the size because we fill it in the appropriate slot.
 
Maybe i don't understand the old way to clearly? Is the "actual"
object the first thing in the returned malloced bytes? Is there a any
doc on the actual layout of the PyObject* that is returned by this
calls?

Thanks



More information about the Python-list mailing list