Skip to content

Conversation

@vstinner
Copy link
Member

@vstinner vstinner commented Nov 27, 2018

Use _Py_CAST_FUNC() to cast functions pointers to getter, setter,
destructor, etc.

Change mostly written in gvim using the command:

:%s!(\(inquiry\|destructor\|reprfunc\|getter\|setter
 \|releasebufferproc\|getbufferproc\))
 \([A-Za-z0-9_]\+\)
 !_Py_CAST_FUNC(\1, \2)!g

https://bugs.python.org/issue33012

Use _Py_CAST_FUNC() to cast functions pointers to getter, setter,
destructor, etc.

Change mostly written in gvim using the command:

    :%s!(\(inquiry\|destructor\|reprfunc\|getter\|setter
     \|releasebufferproc\|getbufferproc\))
     \([A-Za-z0-9_]\+\)
     !_Py_CAST_FUNC(\1, \2)!g
@serhiy-storchaka
Copy link
Member

The problem is not in warnings, but in incorrect function signatures. This PR just silences valid compiler warnings. See #10746.

@vstinner
Copy link
Member Author

The problem is not in warnings, but in incorrect function signatures.

What do you mean by "incorrect function signature"? Example:

static PyObject *
method_get_doc(PyMethodDescrObject *descr, void *closure)
{
    return _PyType_GetDocFromInternalDoc(descr->d_method->ml_name, descr->d_method->ml_doc);
}

static PyGetSetDef method_getset[] = {
    {"__doc__", (getter)method_get_doc},
    ...
};

Here the warning is that PyGetSetDef expects PyObject* as the first argument of the getter, whereas method_getset uses PyMethodDescrObject*. Is it an invalid signature? Would you prefer to write:

static PyObject *
method_get_doc(PyObject *descr_obj, void *closure)
{
    PyMethodDescrObject *descr = (PyMethodDescrObject *)descr_obj;
    return _PyType_GetDocFromInternalDoc(descr->d_method->ml_name, descr->d_method->ml_doc);
}

which is the "correct" signature? The additional "PyMethodDescrObject *descr = (PyMethodDescrObject *)descr_obj;" doesn't seem to provide any value to the compiler nor to the developer who read/maintain the code.

The problem is not in warnings, but in incorrect function signatures. This PR just silences valid compiler warnings. See #10746.

Right, that's a good fix, but I was too lazy to check each function pointer.

@serhiy-storchaka
Copy link
Member

serhiy-storchaka commented Nov 27, 2018

I did not get a warning for method_get_doc. Could you please attach the output of the compiler to bpo-33012?

@vstinner
Copy link
Member Author

@serhiy-storchaka: Oh wow, I don't understand how it's possible, but almost all GCC warnings are gone. It seems like I didn't try on your latest compiler warning fix. So this PR is completely useless!

@vstinner vstinner closed this Nov 30, 2018
@vstinner vstinner deleted the cast_func branch November 30, 2018 13:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants