Serhiy Storchaka added the comment:

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.

----------
components: +Extension Modules, Interpreter Core
nosy: +loewis, ned.deily, rhettinger
stage:  -> patch review
Added file: http://bugs.python.org/file42564/py_refcnt.diff

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue26824>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to