Skip to content
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

Merged
merged 6 commits into from
Apr 29, 2018
Merged

bpo-33012: Fix invalid function cast warnings with gcc 8 for METH_NOARGS #6030

merged 6 commits into from
Apr 29, 2018

Conversation

siddhesh
Copy link
Contributor

@siddhesh siddhesh commented Mar 8, 2018

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

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:c:type:`PyCFunction`

{"__delitem__", (PyCFunction)odict_mp_ass_sub, METH_NOARGS,
odict_delitem__doc__},
{"__delitem__", (PyCFunction)odict_mp_ass_sub,
METH_VARARGS | METH_KEYWORDS, odict_delitem__doc__},
Copy link
Member

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.

Copy link
Contributor Author

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.

@@ -1466,7 +1472,7 @@ odict_dealloc(PyODictObject *self)
/* tp_repr */

static PyObject *
odict_repr(PyODictObject *self)
odict_repr(PyODictObject *self, PyObject *Py_UNUSED(ignored))
Copy link
Member

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.

Copy link
Contributor Author

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.

@@ -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))
Copy link
Member

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.

Copy link
Contributor Author

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.

@@ -1532,7 +1532,7 @@ deque_bool(dequeobject *deque)
}

static PyObject *
deque_get_maxlen(dequeobject *deque)
deque_get_maxlen(dequeobject *deque, PyObject *Py_UNUSED(ignored))
Copy link
Member

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)
``

Copy link
Contributor Author

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.

@@ -439,7 +439,7 @@ calculate_qualname(PyDescrObject *descr)
}

static PyObject *
descr_get_qualname(PyDescrObject *descr)
descr_get_qualname(PyDescrObject *descr, PyObject *Py_UNUSED(ignored))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

void *Py_UNUSED(ignored)

Copy link
Contributor Author

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.

@@ -1139,7 +1139,7 @@ wrapper_text_signature(wrapperobject *wp, void *closure)
static PyObject *
wrapper_qualname(wrapperobject *wp)
Copy link
Member

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.

Copy link
Contributor Author

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))
Copy link
Member

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 *.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

/* forward */
static int odict_init(PyObject *self, PyObject *args, PyObject *kwds);
static PyObject *
odict_init(PyObject *self, PyObject *Py_UNUSED(ignored))
Copy link
Member

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.

Copy link
Contributor Author

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))
Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Member

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.

Copy link
Contributor Author

@siddhesh siddhesh left a 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.

@@ -1139,7 +1139,7 @@ wrapper_text_signature(wrapperobject *wp, void *closure)
static PyObject *
wrapper_qualname(wrapperobject *wp)
Copy link
Contributor Author

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.

/* forward */
static int odict_init(PyObject *self, PyObject *args, PyObject *kwds);
static PyObject *
odict_init(PyObject *self, PyObject *Py_UNUSED(ignored))
Copy link
Contributor Author

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))
Copy link
Contributor Author

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.

@@ -1532,7 +1532,7 @@ deque_bool(dequeobject *deque)
}

static PyObject *
deque_get_maxlen(dequeobject *deque)
deque_get_maxlen(dequeobject *deque, PyObject *Py_UNUSED(ignored))
Copy link
Contributor Author

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.

@@ -439,7 +439,7 @@ calculate_qualname(PyDescrObject *descr)
}

static PyObject *
descr_get_qualname(PyDescrObject *descr)
descr_get_qualname(PyDescrObject *descr, PyObject *Py_UNUSED(ignored))
Copy link
Contributor Author

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.

@@ -1466,7 +1472,7 @@ odict_dealloc(PyODictObject *self)
/* tp_repr */

static PyObject *
odict_repr(PyODictObject *self)
odict_repr(PyODictObject *self, PyObject *Py_UNUSED(ignored))
Copy link
Contributor Author

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.

@@ -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))
Copy link
Contributor Author

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))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

{"__delitem__", (PyCFunction)odict_mp_ass_sub, METH_NOARGS,
odict_delitem__doc__},
{"__delitem__", (PyCFunction)odict_mp_ass_sub,
METH_VARARGS | METH_KEYWORDS, odict_delitem__doc__},
Copy link
Contributor Author

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.
@siddhesh
Copy link
Contributor Author

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))
Copy link
Member

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.

Copy link
Contributor Author

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))
Copy link
Member

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.

siddhesh and others added 4 commits April 23, 2018 01:09
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.
@serhiy-storchaka serhiy-storchaka merged commit 55edd0c into python:master Apr 29, 2018
@siddhesh siddhesh deleted the meth-noargs branch April 30, 2018 08:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants