From 3b03f920e7ac69924e816c1951c9027514432bdd Mon Sep 17 00:00:00 2001 From: Olly Betts Date: Sun, 20 Jan 2019 12:24:45 +1300 Subject: [PATCH] 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. --- CHANGES.current | 7 +++---- Source/Modules/python.cxx | 18 ++++++++++++++---- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/CHANGES.current b/CHANGES.current index 27eb7d1c5..18d694a16 100644 --- a/CHANGES.current +++ b/CHANGES.current @@ -468,10 +468,9 @@ Version 4.0.0 (in progress) 2018-06-11: olly [Python] Fix new GCC8 warnings in generated code by avoiding casts - between incompatible function types where possible (when keyword - args are in use, it is not possible to avoid such warnings as they - are inherent in the design of Python's C API in that particular - case). Fixes #1259. + between incompatible function types where possible, and by + suppressing the warning when it's due to the design of Python's C + API. Fixes #1259. 2018-06-08: philippkraft [Python] Stop exposing _swigregister to Python. It's not diff --git a/Source/Modules/python.cxx b/Source/Modules/python.cxx index d58e21ba7..fe681f768 100755 --- a/Source/Modules/python.cxx +++ b/Source/Modules/python.cxx @@ -2343,7 +2343,10 @@ public: Printf(methods, "\t { \"%s\", %s, METH_VARARGS, ", name, function); } } 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) { @@ -4503,7 +4506,11 @@ public: int argcount = Getattr(n, "python:argcount") ? atoi(Char(Getattr(n, "python:argcount"))) : 2; String *ds = have_docstring(n) ? cdocstring(n, AUTODOC_METHOD) : NewString(""); 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) { Printf(builtin_methods, " { \"%s\", %s, METH_NOARGS, \"%s\" },\n", symname, wname, ds); } else if (argcount == 1) { @@ -4603,12 +4610,15 @@ public: Append(pyflags, "METH_O"); else 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)) { 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); } 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(wname);