Author xdegaye
Recipients cstratak, eitan.adler, martin.panter, miss-islington, pmpp, serhiy.storchaka, siddhesh, vstinner, xdegaye, ztane
Date 2018-05-26.15:16:28
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1527347788.33.0.682650639539.issue33012@psf.upfronthosting.co.za>
In-reply-to
Content
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 */
 };
History
Date User Action Args
2018-05-26 15:16:28xdegayesetrecipients: + xdegaye, vstinner, pmpp, martin.panter, serhiy.storchaka, ztane, eitan.adler, siddhesh, cstratak, miss-islington
2018-05-26 15:16:28xdegayesetmessageid: <1527347788.33.0.682650639539.issue33012@psf.upfronthosting.co.za>
2018-05-26 15:16:28xdegayelinkissue33012 messages
2018-05-26 15:16:28xdegayecreate