Xavier de Gaye <xdeg...@gmail.com> added the comment:

I propose to enhance the changes made by PR 6748 and PR 6749 so that gcc 8 
triggers a warning when the function type of a PyMethodDef element does not 
match its flags by using a macro (see below) that casts the function first to 
the function type corresponding to those flags and then to (void *) as done in 
those PRs to silence the warning when cast to PyCFunction. This would allow 
detecting bugs such as the one found in issue33454.

With the following patch (it is just an example) gcc 8 would emit a warning if 
bisect_right() does not match the type (in the sense defined by 
-Wcast-function-type) of PyCFunctionWithKeywords:

diff --git a/Modules/_bisectmodule.c b/Modules/_bisectmodule.c
index 831e10aa60..85fb0c188e 100644
--- a/Modules/_bisectmodule.c
+++ b/Modules/_bisectmodule.c
@@ -216,15 +216,14 @@ If x is already in a, insert it to the left of the 
leftmost x.\n\
 Optional args lo (default 0) and hi (default len(a)) bound the\n\
 slice of a to be searched.\n");
 
+#define SET_METH_VARARGS_KEYWORDS(func) \
+    (PyCFunction)(void *)(PyCFunctionWithKeywords)(func), 
METH_VARARGS|METH_KEYWORDS
+
 static PyMethodDef bisect_methods[] = {
-    {"bisect_right", (PyCFunction)bisect_right,
-        METH_VARARGS|METH_KEYWORDS, bisect_right_doc},
-    {"insort_right", (PyCFunction)insort_right,
-        METH_VARARGS|METH_KEYWORDS, insort_right_doc},
-    {"bisect_left", (PyCFunction)bisect_left,
-        METH_VARARGS|METH_KEYWORDS, bisect_left_doc},
-    {"insort_left", (PyCFunction)insort_left,
-        METH_VARARGS|METH_KEYWORDS, insort_left_doc},
+    {"bisect_right", SET_METH_VARARGS_KEYWORDS(bisect_right), 
bisect_right_doc},
+    {"insort_right", SET_METH_VARARGS_KEYWORDS(insort_right), 
insort_right_doc},
+    {"bisect_left", SET_METH_VARARGS_KEYWORDS(bisect_left), bisect_left_doc},
+    {"insort_left", SET_METH_VARARGS_KEYWORDS(insort_left), insort_left_doc},
     {NULL, NULL} /* sentinel */
 };

----------
nosy: +xdegaye

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue33012>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to