why PyObject_VAR_HEAD?

Eric Wong wsysdu at gmail.com
Tue Jul 7 08:06:13 CEST 2009

kio wrote:

> Hi,
> I'm studying the CPython source code. I don’t quite understand why
> they’re using PyObject_VAR_HEAD to define struct like PyListObject. 
> define such kind of struct, could I use _PyObject_HEAD_EXTRA as a
> header and add "items" pointer and "allocated" count explicity? Is
> there any difference?
> Thanks.
> Eric

first, _PyObject_HEAD_EXTRA is only useful for debug and defined as:
#ifdef Py_TRACE_REFS
   #define _PyObject_HEAD_EXTRA 	\
      struct _object *_ob_next;   \
      struct _object *_ob_prev;
   #define _PyObject_EXTRA_INIT 0, 0,
   #define _PyObject_HEAD_EXTRA
   #define _PyObject_EXTRA_INIT

and PyObject_HEAD is defined as
#define PyObject_HEAD    \
   _PyObject_HEAD_EXTRA  \
   int ob_refcnt;        \
   struct _typeobject *ob_type;

PyObject_VAR_HEAD defined as
#define PyObject_VAR_HEAD   \
   PyObject_HEAD            \
   int ob_size;

so you can see the differences between them here.
PyListObject is defined as
typedef struct{
    PyObject **ob_item;
    int allocated;

After unfolding these macros we can get
typedef struct{
    int ob_refcnt;
    struct _typeobject *ob_type;
    int ob_size;
    PyObject **ob_item;
    int allocated;

So, if we don't use macros, we have to specify not only the "item" and 
"allocated", but also the ref count and type information of the object 
and the size info for a variable object. These information is not only 
used by PyListObject, but also PyStringObject and PyDictObject, so 
macros make it convenient and consize. 

More information about the Python-list mailing list