-
-
Notifications
You must be signed in to change notification settings - Fork 31.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bpo-33012: Fix invalid function cast warnings with gcc 8 for METH_NOARGS #6030
Conversation
Doc/extending/newtypes.rst
Outdated
a positional argument tuple or keyword argument dictionary. This method is | ||
equivalent to the Python method:: | ||
a positional argument tuple or keyword argument dictionary. We add an unused | ||
argument to allow for a safe cast to a PyCFunction. This method is equivalent to |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
:c:type:`PyCFunction`
Objects/odictobject.c
Outdated
{"__delitem__", (PyCFunction)odict_mp_ass_sub, METH_NOARGS, | ||
odict_delitem__doc__}, | ||
{"__delitem__", (PyCFunction)odict_mp_ass_sub, | ||
METH_VARARGS | METH_KEYWORDS, odict_delitem__doc__}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks incorrect. But an existing code looks incorrect too. I suggest to revert this change (and all other questionable changes) and left them to a separate issue -- bpo-33031.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, dropped from this PR.
Objects/odictobject.c
Outdated
@@ -1466,7 +1472,7 @@ odict_dealloc(PyODictObject *self) | |||
/* tp_repr */ | |||
|
|||
static PyObject * | |||
odict_repr(PyODictObject *self) | |||
odict_repr(PyODictObject *self, PyObject *Py_UNUSED(ignored)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should be compatible with reprfunc
, not PyCFunction
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll drop this one too since it looks like it will need an additional wrapper like odict_init.
Objects/odictobject.c
Outdated
@@ -1609,15 +1615,15 @@ odict_richcompare(PyObject *v, PyObject *w, int op) | |||
/* tp_iter */ | |||
|
|||
static PyObject * | |||
odict_iter(PyODictObject *od) | |||
odict_iter(PyODictObject *od, PyObject *Py_UNUSED(ignored)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should be compatible with getiterfunc
, not PyCFunction
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Likewise, I'll deal with it separately.
Modules/_collectionsmodule.c
Outdated
@@ -1532,7 +1532,7 @@ deque_bool(dequeobject *deque) | |||
} | |||
|
|||
static PyObject * | |||
deque_get_maxlen(dequeobject *deque) | |||
deque_get_maxlen(dequeobject *deque, PyObject *Py_UNUSED(ignored)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is getter
.
void *Py_UNUSED(ignored)
``
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm tackling getters in a separate patch, so I'll drop this.
Objects/descrobject.c
Outdated
@@ -439,7 +439,7 @@ calculate_qualname(PyDescrObject *descr) | |||
} | |||
|
|||
static PyObject * | |||
descr_get_qualname(PyDescrObject *descr) | |||
descr_get_qualname(PyDescrObject *descr, PyObject *Py_UNUSED(ignored)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
void *Py_UNUSED(ignored)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Likewise, I've got getter changes in another branch, so I'll fix it there.
Objects/descrobject.c
Outdated
@@ -1139,7 +1139,7 @@ wrapper_text_signature(wrapperobject *wp, void *closure) | |||
static PyObject * | |||
wrapper_qualname(wrapperobject *wp) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Needs the second argument of type void *
. As well as other getters.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've made a separate bug report for the getters: issue33029.
@@ -4554,7 +4554,7 @@ long_or(PyObject *a, PyObject *b) | |||
} | |||
|
|||
static PyObject * | |||
long_long(PyObject *v) | |||
long_long(PyObject *v, PyObject *Py_UNUSED(ignored)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is used both as PyCFunction
and getter
. It would be safer to declare the second parameter as void *
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
Objects/odictobject.c
Outdated
/* forward */ | ||
static int odict_init(PyObject *self, PyObject *args, PyObject *kwds); | ||
static PyObject * | ||
odict_init(PyObject *self, PyObject *Py_UNUSED(ignored)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest to left this for a separate issue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, I'll drop this and the other patch from this PR and make a different one.
@@ -512,7 +512,7 @@ deque_inplace_concat(dequeobject *deque, PyObject *other) | |||
} | |||
|
|||
static PyObject * | |||
deque_copy(PyObject *deque) | |||
deque_copy(PyObject *deque, PyObject *Py_UNUSED(ignored)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Explicit cast to PyCFunction
at line 1590 is not needed.
The same with other functions which exactly match the signature of PyCFunction
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've kept the casts in place because we will eventually need it. To fully fix the warnings, the ml_meth
type should be like so:
PyObject *(*) (PyObject *, PyObject *, ...)
so that it matches with all of the allowed method types provided that there is an explicit cast in place. Right now I am not sure if I should change the PyCFunction
type to that or to make a new PyCFunctionAny
just for ml_meth
. Since we're on this topic, what do you think is the preferred change? I suppose both change API, but not in a completely incompatible way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The casts are not needed if the signature matches PyCFunction
. It is still needed if the function has type PyCFunctionWithKeywords
, PyNoArgsFunction
, etc. Removing unneeded casts will clean up the code and can prevent introducing new bugs when the signature be changed unintentionally.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the review, I'm pushing a version with most of the fixes you suggested, except for removal of the cast. I've kept the casts in place because we will eventually need it. To fully fix the warnings, the ml_meth
type should be like so:
PyObject *(*) (PyObject *, PyObject *, ...)
so that it matches with all of the allowed method types provided that there is an explicit cast in place. Right now I am not sure if I should change the PyCFunction
type to that or to make a new PyCFunctionAny
just for ml_meth
. Since we're on this topic, what do you think is the preferred change? I suppose both change API, but not in a completely incompatible way.
Objects/descrobject.c
Outdated
@@ -1139,7 +1139,7 @@ wrapper_text_signature(wrapperobject *wp, void *closure) | |||
static PyObject * | |||
wrapper_qualname(wrapperobject *wp) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've made a separate bug report for the getters: issue33029.
Objects/odictobject.c
Outdated
/* forward */ | ||
static int odict_init(PyObject *self, PyObject *args, PyObject *kwds); | ||
static PyObject * | ||
odict_init(PyObject *self, PyObject *Py_UNUSED(ignored)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, I'll drop this and the other patch from this PR and make a different one.
@@ -512,7 +512,7 @@ deque_inplace_concat(dequeobject *deque, PyObject *other) | |||
} | |||
|
|||
static PyObject * | |||
deque_copy(PyObject *deque) | |||
deque_copy(PyObject *deque, PyObject *Py_UNUSED(ignored)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've kept the casts in place because we will eventually need it. To fully fix the warnings, the ml_meth
type should be like so:
PyObject *(*) (PyObject *, PyObject *, ...)
so that it matches with all of the allowed method types provided that there is an explicit cast in place. Right now I am not sure if I should change the PyCFunction
type to that or to make a new PyCFunctionAny
just for ml_meth
. Since we're on this topic, what do you think is the preferred change? I suppose both change API, but not in a completely incompatible way.
Modules/_collectionsmodule.c
Outdated
@@ -1532,7 +1532,7 @@ deque_bool(dequeobject *deque) | |||
} | |||
|
|||
static PyObject * | |||
deque_get_maxlen(dequeobject *deque) | |||
deque_get_maxlen(dequeobject *deque, PyObject *Py_UNUSED(ignored)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm tackling getters in a separate patch, so I'll drop this.
Objects/descrobject.c
Outdated
@@ -439,7 +439,7 @@ calculate_qualname(PyDescrObject *descr) | |||
} | |||
|
|||
static PyObject * | |||
descr_get_qualname(PyDescrObject *descr) | |||
descr_get_qualname(PyDescrObject *descr, PyObject *Py_UNUSED(ignored)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Likewise, I've got getter changes in another branch, so I'll fix it there.
Objects/odictobject.c
Outdated
@@ -1466,7 +1472,7 @@ odict_dealloc(PyODictObject *self) | |||
/* tp_repr */ | |||
|
|||
static PyObject * | |||
odict_repr(PyODictObject *self) | |||
odict_repr(PyODictObject *self, PyObject *Py_UNUSED(ignored)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll drop this one too since it looks like it will need an additional wrapper like odict_init.
Objects/odictobject.c
Outdated
@@ -1609,15 +1615,15 @@ odict_richcompare(PyObject *v, PyObject *w, int op) | |||
/* tp_iter */ | |||
|
|||
static PyObject * | |||
odict_iter(PyODictObject *od) | |||
odict_iter(PyODictObject *od, PyObject *Py_UNUSED(ignored)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Likewise, I'll deal with it separately.
@@ -4554,7 +4554,7 @@ long_or(PyObject *a, PyObject *b) | |||
} | |||
|
|||
static PyObject * | |||
long_long(PyObject *v) | |||
long_long(PyObject *v, PyObject *Py_UNUSED(ignored)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
Objects/odictobject.c
Outdated
{"__delitem__", (PyCFunction)odict_mp_ass_sub, METH_NOARGS, | ||
odict_delitem__doc__}, | ||
{"__delitem__", (PyCFunction)odict_mp_ass_sub, | ||
METH_VARARGS | METH_KEYWORDS, odict_delitem__doc__}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, dropped from this PR.
METH_NOARGS functions need only a single argument but they are cast into a PyCFunction, which takes two arguments. This triggers an invalid function cast warning in gcc8 (unreleased as of now) due to the argument mismatch. Fix this by adding a dummy unused argument.
METH_NOARGS functions need only a single argument but they are cast into a PyCFunction, which takes two arguments. This triggers an invalid function cast warning in gcc8 (unreleased as of now) due to the argument mismatch. Fix this by adding a dummy unused argument.
Hi, here's the new patchset with dropped casts and resolved conflicts. |
@@ -631,7 +631,7 @@ static PyObject *PyMessageBox(PyObject *self, PyObject *args) | |||
return g_Py_BuildValue("i", rc); | |||
} | |||
|
|||
static PyObject *GetRootHKey(PyObject *self) | |||
static PyObject *GetRootHKey(PyObject *self, PyObject *Py_UNUSED(ignored)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Casting to PyCFunction
below no longer needed. And this is the only use of PyCFunction
, thus its declaration can be removed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah I missed this one, I'll fix it.
@@ -512,7 +512,7 @@ deque_inplace_concat(dequeobject *deque, PyObject *other) | |||
} | |||
|
|||
static PyObject * | |||
deque_copy(PyObject *deque) | |||
deque_copy(PyObject *deque, PyObject *Py_UNUSED(ignored)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The casts are not needed if the signature matches PyCFunction
. It is still needed if the function has type PyCFunctionWithKeywords
, PyNoArgsFunction
, etc. Removing unneeded casts will clean up the code and can prevent introducing new bugs when the signature be changed unintentionally.
METH_NOARGS functions need only a single argument but they are cast into a PyCFunction, which takes two arguments. This triggers an invalid function cast warning in gcc8 (unreleased as of now) due to the argument mismatch. Fix this by adding a dummy unused argument wherever it isn't present and mark it as unused to make it consistent with other similar uses.
METH_NOARGS functions need only a single argument but they are cast into a PyCFunction, which takes two arguments. This triggers an invalid function cast warning in gcc8 (unreleased as of now) due to the argument mismatch. Fix this by adding a dummy unused argument.
METH_NOARGS functions need only a single argument but they are cast into a PyCFunction, which takes two arguments. This triggers an invalid function cast warning in gcc8 (unreleased as of now) due to the argument mismatch. Fix this by adding a dummy unused argument.
gcc 8 has added a new warning heuristic to detect invalid function casts and a stock python build seems to hit that warning quite often. The most common is the cast of a METH_NOARGS function (that uses just one argument) to a PyCFunction. Fix this by adding a dummy argument to all the affected callback functions.
I have split the changes by directory and also made a separate commit where the change was not just a trivial dummy argument addition, as is the case in a4ee3ad and 58eaf31.
https://bugs.python.org/issue33012