why PyObject_VAR_HEAD?

K

kio

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. To
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
 
B

Benjamin Peterson

kio said:
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. To
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?

No, PyObject_VAR_HEAD adds a ob_size field, which is used to indicate the size
of the list. PyList uses this just as a convenience; it does not actually use
varsize mallocing. You could use PyObject_HEAD if you added an additional size
field.
 
E

Eric Wong

kio said:
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. To
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,
#else
#define _PyObject_HEAD_EXTRA
#define _PyObject_EXTRA_INIT
#endif

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_VAR_HEAD
PyObject **ob_item;
int allocated;
}PyListObject;

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

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,201
Messages
2,571,049
Members
47,655
Latest member
eizareri

Latest Threads

Top