[Python-Dev] getter/setter function signatures

Thomas Heller thomas.heller@ion-tof.com
Thu, 18 Apr 2002 15:52:01 +0200


As an extension writer, this bugs me [1].

Looking into Python's sources, I find mostly code like this:

static PyObject *
func_get_code(PyFunctionObject *op)
{
        ....
}

static PyGetSetDef func_getsetlist[] = {
        {"func_code", (getter)func_get_code, (setter)func_set_code},
        ....
        {NULL} /* Sentinel */
};



in other words, casting like hell to the correct function type.

Should a style like this should be preferred:

static PyObject *
func_get_code(PyObject *py)
{
        PyFunctionObject *op = (PyFunctionObject *)py;
        ....
}

static PyGetSetDef func_getsetlist[] = {
        {"func_code", func_get_code, func_set_code},
        ....
        {NULL} /* Sentinel */
};


in other words, move the cast where you are more sure about it's correctness,
and give the compiler a change for meaningfull warnings.

Thomas

[1] Actually it's getting better since the documentation has improved
a lot, before that I was mainly looking into the source or for examples.