[PyQt] [PATCH 4/5] Fix GCC cast-function-type warning

Stefan BrĂ¼ns stefan.bruens at rwth-aachen.de
Sat Sep 29 15:27:48 BST 2018


Python bindings require to cast PyCFunctionWithKeywords to PyCFunction.
The canonical way to do this is via an intermediate cast to
<void(*)(void)>:

"The function type "void (*) (void)" is special and matches everything,
which can be used to suppress this [-Wcast-function-type] warning."

The initial cast to PyCFunctionWithKeywords rejects any other type,
serving as a saveguard.
---
 sipgen/gencode.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/sipgen/gencode.c b/sipgen/gencode.c
index e0d61c5..20dea03 100644
--- a/sipgen/gencode.c
+++ b/sipgen/gencode.c
@@ -4786,16 +4786,23 @@ static void prMethodTable(sipSpec *pt, sortedMethTab *mtable, int nr,
     for (i = 0; i < nr; ++i)
     {
         memberDef *md = mtable[i].md;
-        const char *cast, *flags;
+        const char *cast, *flags, *cast_end;
 
         if (noArgParser(md) || useKeywordArgs(md))
         {
-            cast = "(PyCFunction)";
+            if (generating_c) {
+                cast = "(PyCFunction)(";
+                cast_end = ")";
+            } else {
+                cast = "reinterpret_cast<PyCFunction>(reinterpret_cast<void(*)(void)>(reinterpret_cast<PyCFunctionWithKeywords>(";
+                cast_end = ")))";
+            }
             flags = "|METH_KEYWORDS";
         }
         else
         {
             cast = "";
+            cast_end = "";
             flags = "";
         }
 
@@ -4803,7 +4810,7 @@ static void prMethodTable(sipSpec *pt, sortedMethTab *mtable, int nr,
         md->membernr = i;
 
         prcode(fp,
-"    {SIP_MLNAME_CAST(%N), %smeth_%L_%s, METH_VARARGS%s, ", md->pyname, cast, iff, md->pyname->text, flags);
+"    {SIP_MLNAME_CAST(%N), %smeth_%L_%s%s, METH_VARARGS%s, ", md->pyname, cast, iff, md->pyname->text, cast_end, flags);
 
         if (hasMemberDocstring(pt, overs, md, iff))
             prcode(fp, "SIP_MLDOC_CAST(doc_%L_%s)", iff, md->pyname->text);
-- 
2.19.0



More information about the PyQt mailing list