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

Stefan BrĂ¼ns stefan.bruens at rwth-aachen.de
Mon Oct 1 02:58:47 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 5112525..e300496 100644
--- a/sipgen/gencode.c
+++ b/sipgen/gencode.c
@@ -4775,16 +4775,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 = "";
         }
 
@@ -4792,7 +4799,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