This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author serhiy.storchaka
Recipients loewis, ned.deily, rhettinger, serhiy.storchaka, xiang.zhang
Date 2016-04-22.06:03:48
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1461305029.09.0.639964917048.issue26824@psf.upfronthosting.co.za>
In-reply-to
Content
In Python 2.6 PyObject_HEAD was defined as

#define PyObject_HEAD                   \
    _PyObject_HEAD_EXTRA                \
    Py_ssize_t ob_refcnt;               \
    struct _typeobject *ob_type;

Every extension type structure contained fields ob_refcnt and ob_type. For the pointer of type (FooObject *) you used foo->ob_refcnt and foo->ob_type. You could also use ob = (PyObject *)foo; ob->ob_type, but this is undefined behavior, as estimated in PEP3123.

Now PyObject_HEAD is defined as

#define PyObject_HEAD                   PyObject ob_base;

Every extension type structure no longer contain fields ob_refcnt and ob_type, and you can't access them directly. For convenience and compatibility with 2.6 there were added macros Py_TYPE() and Py_REFCNT(). Direct access to ob_refcnt and ob_type is legal, just your can't use it with arbitrary pointer type without casting it to PyObject*. That just can't be compiled. If you have a pointer of type PyObject*, you can use direct access to ob_refcnt and ob_type.

Direct access to ob_type is used about 300 times in the source code. Changing all this case to use the Py_TYPE() macro causes code churn.

There is different situation with ob_refcnt. It is less used, and changing the code to use Py_REFCNT() causes less code churn. But this can make the code looks more pretty. Here is a patch (written a day ago) that makes the code to use Py_REFCNT(). I was not sure about the code that directly modifies the reference count and left it unchanged. I'm not sure that it is worth to apply this patch, may be it still makes too much code churn.
History
Date User Action Args
2016-04-22 06:03:49serhiy.storchakasetrecipients: + serhiy.storchaka, loewis, rhettinger, ned.deily, xiang.zhang
2016-04-22 06:03:49serhiy.storchakasetmessageid: <1461305029.09.0.639964917048.issue26824@psf.upfronthosting.co.za>
2016-04-22 06:03:48serhiy.storchakalinkissue26824 messages
2016-04-22 06:03:48serhiy.storchakacreate