ob_refcnt and Py_BuildValue

Dan puter_programmer at yahoo.com
Mon Oct 11 05:53:33 EDT 1999


I'm just learning how to write C extension modules in Python and I
have a question about the reference counts.  Is there any reason
Py_BuildValue would output an object with a reference count other than
1?  I put together a simple test and when building an integer object I
get a ob_refcnt value of 4.  In other code where I'm using
Py_BuildValue for an integer object I'm getting 7 for ob_refcnt and I
really don't understand why.

I've included the test code below.  When run on my system the printf
prints out "refcnt=4" when it seems to me it should be printing out
"refcnt=1".

TIA
-Dan



#include "/usr/local/include/python1.5/Python.h"


static PyObject * 
test_test(self, args)
	PyObject * self;
	PyObject * args;
{
    PyObject *x;
    int y=10;

    x = Py_BuildValue("i", y);
    printf("refcnt=%d\n", x->ob_refcnt);
    Py_INCREF(Py_None);
    return Py_None;
}


static PyMethodDef test_methods[] = {
    {"test", test_test, METH_VARARGS},
    {NULL,	NULL}
};


void inittest() {
    (void) Py_InitModule("test", test_methods);
}








More information about the Python-list mailing list