Suppress warnings about PyCFunction casts

These remaining warnings are due to the design of Python's C API,
so suppress them by casting via void(*)(void) (which GCC documents
as the way to suppress this warning).

Closes #1259.
This commit is contained in:
Olly Betts 2019-01-20 12:24:45 +13:00
commit 3b03f920e7
2 changed files with 17 additions and 8 deletions

View file

@ -468,10 +468,9 @@ Version 4.0.0 (in progress)
2018-06-11: olly 2018-06-11: olly
[Python] Fix new GCC8 warnings in generated code by avoiding casts [Python] Fix new GCC8 warnings in generated code by avoiding casts
between incompatible function types where possible (when keyword between incompatible function types where possible, and by
args are in use, it is not possible to avoid such warnings as they suppressing the warning when it's due to the design of Python's C
are inherent in the design of Python's C API in that particular API. Fixes #1259.
case). Fixes #1259.
2018-06-08: philippkraft 2018-06-08: philippkraft
[Python] Stop exposing <CLASS>_swigregister to Python. It's not [Python] Stop exposing <CLASS>_swigregister to Python. It's not

View file

@ -2343,7 +2343,10 @@ public:
Printf(methods, "\t { \"%s\", %s, METH_VARARGS, ", name, function); Printf(methods, "\t { \"%s\", %s, METH_VARARGS, ", name, function);
} }
} else { } else {
Printf(methods, "\t { \"%s\", (PyCFunction)%s, METH_VARARGS|METH_KEYWORDS, ", name, function); // Cast via void(*)(void) to suppress GCC -Wcast-function-type warning.
// Python should always call the function correctly, but the Python C API
// requires us to store it in function pointer of a different type.
Printf(methods, "\t { \"%s\", (PyCFunction)(void(*)(void))%s, METH_VARARGS|METH_KEYWORDS, ", name, function);
} }
if (!n) { if (!n) {
@ -4503,7 +4506,11 @@ public:
int argcount = Getattr(n, "python:argcount") ? atoi(Char(Getattr(n, "python:argcount"))) : 2; int argcount = Getattr(n, "python:argcount") ? atoi(Char(Getattr(n, "python:argcount"))) : 2;
String *ds = have_docstring(n) ? cdocstring(n, AUTODOC_METHOD) : NewString(""); String *ds = have_docstring(n) ? cdocstring(n, AUTODOC_METHOD) : NewString("");
if (check_kwargs(n)) { if (check_kwargs(n)) {
Printf(builtin_methods, " { \"%s\", (PyCFunction)%s, METH_VARARGS|METH_KEYWORDS, \"%s\" },\n", symname, wname, ds); // Cast via void(*)(void) to suppress GCC -Wcast-function-type
// warning. Python should always call the function correctly, but
// the Python C API requires us to store it in function pointer of a
// different type.
Printf(builtin_methods, " { \"%s\", (PyCFunction)(void(*)(void))%s, METH_VARARGS|METH_KEYWORDS, \"%s\" },\n", symname, wname, ds);
} else if (argcount == 0) { } else if (argcount == 0) {
Printf(builtin_methods, " { \"%s\", %s, METH_NOARGS, \"%s\" },\n", symname, wname, ds); Printf(builtin_methods, " { \"%s\", %s, METH_NOARGS, \"%s\" },\n", symname, wname, ds);
} else if (argcount == 1) { } else if (argcount == 1) {
@ -4603,12 +4610,15 @@ public:
Append(pyflags, "METH_O"); Append(pyflags, "METH_O");
else else
Append(pyflags, "METH_VARARGS"); Append(pyflags, "METH_VARARGS");
// Cast via void(*)(void) to suppress GCC -Wcast-function-type warning.
// Python should always call the function correctly, but the Python C
// API requires us to store it in function pointer of a different type.
if (have_docstring(n)) { if (have_docstring(n)) {
String *ds = cdocstring(n, AUTODOC_STATICFUNC); String *ds = cdocstring(n, AUTODOC_STATICFUNC);
Printf(builtin_methods, " { \"%s\", (PyCFunction)%s, %s, \"%s\" },\n", symname, wname, pyflags, ds); Printf(builtin_methods, " { \"%s\", (PyCFunction)(void(*)(void))%s, %s, \"%s\" },\n", symname, wname, pyflags, ds);
Delete(ds); Delete(ds);
} else { } else {
Printf(builtin_methods, " { \"%s\", (PyCFunction)%s, %s, \"\" },\n", symname, wname, pyflags); Printf(builtin_methods, " { \"%s\", (PyCFunction)(void(*)(void))%s, %s, \"\" },\n", symname, wname, pyflags);
} }
Delete(fullname); Delete(fullname);
Delete(wname); Delete(wname);