This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Invalid function cast warnings with gcc 8 for METH_NOARGS
Type: compile error Stage: resolved
Components: Build Versions: Python 3.8, Python 3.7
process
Status: closed Resolution: fixed
Dependencies: 33031 Superseder:
Assigned To: Nosy List: asn, cstratak, eitan.adler, gregory.p.smith, martin.panter, miss-islington, pmpp, resmord, serhiy.storchaka, siddhesh, terry.reedy, vstinner, xdegaye, yan12125, ztane
Priority: normal Keywords: patch

Created on 2018-03-06 11:18 by siddhesh, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 6030 merged siddhesh, 2018-03-08 15:11
PR 6652 merged serhiy.storchaka, 2018-04-30 08:24
PR 6748 merged serhiy.storchaka, 2018-05-10 10:22
PR 6749 merged serhiy.storchaka, 2018-05-10 10:23
PR 6757 merged serhiy.storchaka, 2018-05-10 18:12
PR 7112 merged miss-islington, 2018-05-25 10:17
PR 10736 merged serhiy.storchaka, 2018-11-27 10:34
PR 10744 closed vstinner, 2018-11-27 14:55
PR 10748 merged serhiy.storchaka, 2018-11-27 17:44
PR 10750 merged miss-islington, 2018-11-27 18:28
PR 10751 merged serhiy.storchaka, 2018-11-27 18:50
PR 10795 merged miss-islington, 2018-11-29 14:10
PR 10796 merged vstinner, 2018-11-29 14:22
PR 12179 merged matrixise, 2019-03-05 14:58
PR 14814 ZackerySpytz, 2019-08-14 05:33
Messages (49)
msg313317 - (view) Author: Siddhesh Poyarekar (siddhesh) * Date: 2018-03-06 11:18
gcc 8 has added a new warning heuristic to detect invalid function casts and a stock python build seems to hit that warning quite often.  The most common is the cast of a METH_NOARGS function (that uses just one argument) to a PyCFunction.  The fix is pretty simple but needs to be applied widely.  I'm slowly knocking them off in my spare time; WIP here, which has a few other types of warnings mixed in that I'll sift out during submission and also create separate bug reports for:

https://github.com/siddhesh/cpython/tree/func-cast

I'll clean up and post PR(s) once I am done but I figured I should file this report first since it is a pretty big change in terms of number of files touched and wanted to be sure that I'm making changes the way the community prefers.
msg313319 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-03-06 11:32
Argument Clinic generates the following declaration for the second parameter of METH_NOARGS functions:

    PyObject *Py_UNUSED(ignored)

It would be nice to follow the same style.

If the first parameter is of type PyObject* too, the explicit cast to PyCFunction can be removed.

Skip the curses module. It will be converted to Argument Clinic.
msg313320 - (view) Author: Charalampos Stratakis (cstratak) * Date: 2018-03-06 11:38
We are getting hit by that quite often on Fedora, with the transition to gcc 8 and it creates unnecessary noise at our build logs. Thanks for working on that.

When you sent your PR I can test it within our build system and verify if it works.
msg313875 - (view) Author: Antti Haapala (ztane) * Date: 2018-03-15 11:33
I don't have GCC 8 so I cannot verify this bug, but *function pointer casts* are fine - any function pointer can be cast to any other function pointer - it is only that they must *not* be called unless cast back again to be compatible with the function definition. Any fix to the contrary might well *cause* undefined behaviour!

Could you provide a sample of the *actual warnings* so that they could be studied?
msg313879 - (view) Author: Siddhesh Poyarekar (siddhesh) * Date: 2018-03-15 12:07
> I don't have GCC 8 so I cannot verify this bug, but *function pointer casts* are fine - any function pointer can be cast to any other function pointer - it is only that they must *not* be called unless cast back again to be compatible with the function definition. Any fix to the contrary might well *cause* undefined behaviour!

Please see the attached PR; METH_NOARGS callbacks are inconsistent in their signature and many have just one argument while they're called with two arguments, the second being NULL.  The patch fixes these to consistently take and call with two arguments.

> Could you provide a sample of the *actual warnings* so that they could be studied?

Here's one of a few hundred.

Objects/bytesobject.c:3085:25: warning: cast between incompatible function types from ‘PyObject * (*)(striterobject *)’ {aka ‘struct _object * (*)(struct <anonymous> *)’} to ‘PyObject * (*)(PyObject *, PyObject *)’ {aka ‘struct _object * (*)(struct _object *, struct _object *)’} [-Wcast-function-type]
     {"__reduce__",      (PyCFunction)striter_reduce, METH_NOARGS,
                         ^
This is a new warning in gcc8, so you'll likely not find much reference, but here's a gcc bug report that might give you more context:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=84531
msg313886 - (view) Author: Antti Haapala (ztane) * Date: 2018-03-15 15:27
Yea, I looked into `ceval.c` and the function is *called incorrectly*, so there is undefined behaviour there - it has been wrong all along, in 3.5 all the way down to 2-something

        if (flags & (METH_NOARGS | METH_O)) {
            PyCFunction meth = PyCFunction_GET_FUNCTION(func);
            PyObject *self = PyCFunction_GET_SELF(func);
            if (flags & METH_NOARGS && na == 0) {
                C_TRACE(x, (*meth)(self,NULL));

                x = _Py_CheckFunctionResult(func, x, NULL);
            }

The warning in GCC shouldn't probably have been enabled at all in `-Wall -Wextra` because the cast is explicit. However, it is somewhat true.

However, the correct way to fix would be to have the METH_NOARGS case cast the function to the right prototype. There exists lots of existing code that *is* going to break too. 

Perhaps PyCFunction should declare no prototype, i.e. empty parentheses, for backwards compatibility:

    typedef PyObject *(*PyCFunction)();

and deprecate it; start using a new typedef for it - and then add proper casts in every place that call a function.
msg313889 - (view) Author: Siddhesh Poyarekar (siddhesh) * Date: 2018-03-15 15:54
> The warning in GCC shouldn't probably have been enabled at all in `-Wall -Wextra` because the cast is explicit. However, it is somewhat true.

The explicit cast is precisely what enables the more nuanced function cast warning where it checks the function for type compatibility.  That is, it will check the types of the return and arguments and then warn in case of mismatch.

> However, the correct way to fix would be to have the METH_NOARGS case cast the function to the right prototype. There exists lots of existing code that *is* going to break too. 

AFAICT, there is no right prototype for the METH_NOARGS function; there used to be a PyCFunctionWithNoArguments but that seems to have fallen out of favour some time back.  The prototype that seems to be commonly in use (and in clinic as well, which is what I based my patches on) is the PyCFunction, which seems like a safe way to do things.

> Perhaps PyCFunction should declare no prototype, i.e. empty parentheses, for backwards compatibility:
> 
>     typedef PyObject *(*PyCFunction)();
> 
> and deprecate it; start using a new typedef for it - and then add proper casts in every place that call a function.

I have a patch in the works that makes it PyObject *(*)(PyObject *, PyObject *, ...)

which allows for two compulsory arguments (fits in with most cases, provided the METH_NOARGS patch is accepted) and then the rest depending on the type of the cast function.  The rest of the PyMethodDef functions are much simpler fixes this way.  It also seems like a more intuitive description of the callbacks.

Then there are getter and setter and other function pointers that need similar fixes to METH_NOARGS.
msg313890 - (view) Author: Siddhesh Poyarekar (siddhesh) * Date: 2018-03-15 15:55
I forgot to clarify that the function cast warning allows for variable argument casts as a wildcard, which is my basis for the PyObject *(*)(PyObject *, PyObject *, ...) fix proposal.
msg314795 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2018-04-02 03:52
Siddhesh, it looks like your fixes make the C function signatures match the signature expected in the PyMethodDef structure. If so, I suggest to remove the (PyCFunction) casts from those structure definitions as well. For instance, now that we have

  PyObject *Noddy_name(Noddy *self, PyObject *Py_UNUSED(ignored))

I suggest changing

  PyMethodDef Noddy_methods[] = {
      {"name", (PyCFunction)Noddy_name, METH_NOARGS, ...

to

  PyMethodDef Noddy_methods[] = {
      {"name", Noddy_name, METH_NOARGS, ...

I suspect the casts were only added to hide compiler warnings related to this bug.

If you are proposing to add an ellipsis (...) to the definition of PyCFunction, that seems misguided. I understand this is incompatible under standard C. Are you relying on a GCC extension perhaps? Python is used with other compilers too.
msg314962 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2018-04-05 00:13
Sorry, I realize there is a problem remaining with the pointer types for "Noddy_name" (Noddy vs PyObject pointers), so you can't remove the cast there. But my suggestion should still apply to other places, for instance the "error_out" method in Doc/howto/cporting.rst.
msg315123 - (view) Author: Siddhesh Poyarekar (siddhesh) * Date: 2018-04-09 10:41
Fair enough, I'll reduce my scope of changes for this patchset, especially since I'm unable to find enough time to work on the remaining changes I had thought of in the coming weeks.

I'll post an updated patch soonish.
msg315909 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-04-29 18:59
New changeset 55edd0c185ad2d895b5d73e47d67049bc156b654 by Serhiy Storchaka (Siddhesh Poyarekar) in branch 'master':
bpo-33012: Fix invalid function cast warnings with gcc 8 for METH_NOARGS. (GH-6030)
https://github.com/python/cpython/commit/55edd0c185ad2d895b5d73e47d67049bc156b654
msg315937 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2018-04-30 08:02
The commit 55edd0c185ad2d895b5d73e47d67049bc156b654 introduced a new warning:

gcc -pthread -c -Wno-unused-result -Wsign-compare -g -Og -Wall -Wstrict-prototypes -O0   -std=c99 -Wextra -Wno-unused-result -Wno-unused-parameter -Wno-missing-field-initializers -Werror=implicit-function-declaration   -I. -I./Include   -fPIC -DPy_BUILD_CORE -o Objects/longobject.o Objects/longobject.c
Objects/longobject.c:5359:5: warning: initialisation depuis un type pointeur incompatible [-Wincompatible-pointer-types]
     long_long,                  /*nb_int*/
     ^~~~~~~~~
Objects/longobject.c:5359:5: note: (près de l'initialisation de « long_as_number.nb_int »)
Objects/longobject.c:5376:5: warning: initialisation depuis un type pointeur incompatible [-Wincompatible-pointer-types]
     long_long,                  /* nb_index */
     ^~~~~~~~~
Objects/longobject.c:5376:5: note: (près de l'initialisation de « long_as_number.nb_index »)


It seems like a conversion to (unaryfunc) is needed when passing long_long as nb_int in long_as_number.
msg315938 - (view) Author: Siddhesh Poyarekar (siddhesh) * Date: 2018-04-30 08:07
Yeah, there are multiple such uses that need wrappers to actually fix for gcc8, which will be released this week.  I think I'll have more time for some more patches in this vein this weekend.
msg315948 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-04-30 12:35
New changeset 6405feecda6a5d5dd7a4240eb3054a2676ed29b1 by Serhiy Storchaka in branch 'master':
bpo-33012: Fix invalid function casts for long_long. (GH-6652)
https://github.com/python/cpython/commit/6405feecda6a5d5dd7a4240eb3054a2676ed29b1
msg316355 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-05-10 10:28
The following PRs fix warnings in casts to PyCFunction for method conventions different from METH_NOARGS, METH_O and
METH_VARARGS. PR 6748 does this for Argument Clinic generated code (all files except Tools/clinic/clinic.py are generated). PR 6749 does it for hand-written code. It also fixes few missed or new cases for METH_NOARGS.
msg316356 - (view) Author: Charalampos Stratakis (cstratak) * Date: 2018-05-10 10:31
Is it possible/feasible to fix that for the 3.7 and 3.6 branches as well?
msg316357 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-05-10 10:33
See also issue33454 about a real bug found thanks to these warnings.
msg316372 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-05-10 18:14
I think it safer to silence this kind of warnings in 3.7 and 3.6. PR 6757 does this.
msg317668 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-05-25 10:16
New changeset ef91ddeae79497fac25545dd68ee55a5a3c60e8d by Serhiy Storchaka in branch '3.7':
bpo-33012: Add -Wno-cast-function-type for gcc 8. (GH-6757)
https://github.com/python/cpython/commit/ef91ddeae79497fac25545dd68ee55a5a3c60e8d
msg317670 - (view) Author: miss-islington (miss-islington) Date: 2018-05-25 11:03
New changeset 20b797dafa149afcdd7b64e489eb8ea1386d8eb4 by Miss Islington (bot) in branch '3.6':
bpo-33012: Add -Wno-cast-function-type for gcc 8. (GH-6757)
https://github.com/python/cpython/commit/20b797dafa149afcdd7b64e489eb8ea1386d8eb4
msg317742 - (view) Author: Xavier de Gaye (xdegaye) * (Python triager) Date: 2018-05-26 15:16
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 */
 };
msg317747 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-05-26 17:25
Great idea!

But the problem is that additional flags can be used, e.g. METH_CLASS.
msg317794 - (view) Author: Xavier de Gaye (xdegaye) * (Python triager) Date: 2018-05-27 14:27
> But the problem is that additional flags can be used, e.g. METH_CLASS.

Right, https://docs.python.org/3/c-api/structures.html says: "The individual flags indicate either a calling convention or a binding convention". This may be overcome by introducing another macro with 2 arguments, the second argument being used to set the binding convention flag:

#define SET_METH_VARARGS_KEYWORDS_FLAG(func, flag) \
    (PyCFunction)(void *)(PyCFunctionWithKeywords)func, METH_VARARGS|METH_KEYWORDS|flag
#define SET_METH_VARARGS_KEYWORDS(func) SET_METH_VARARGS_KEYWORDS_FLAG(func, 0x0000)

The refactoring would be quite large though.
msg317823 - (view) Author: Antti Haapala (ztane) * Date: 2018-05-28 03:53
Well, there's only one problem with casting to void *: while converting the function pointer to another *is* standard-compliant, and GCC is being just hypersensitive here, casting a function pointer to void * isn't, though it is a common extension (http://port70.net/~nsz/c/c11/n1570.html#J.5.7).

Pedantically the correct way is to cast to a function pointer with no prototype (empty parentheses) and from that to the target type. See for example. See for example https://godbolt.org/g/FdPdUj
msg317825 - (view) Author: Siddhesh Poyarekar (siddhesh) * Date: 2018-05-28 06:31
> Pedantically the correct way is to cast to a function pointer with no prototype (empty parentheses) and from that to the target type. See for example. See for example https://godbolt.org/g/FdPdUj

This is one way that the gcc diagnostics explicitly allow in addition to variable arguments like so: https://godbolt.org/g/Dtb4fv
msg317826 - (view) Author: (yan12125) * Date: 2018-05-28 07:12
There are still quite a few similar warnings on git-master. Do they indicate the same problem or should I open new issues?

For example:

gcc -pthread -c -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes    -std=c99 -Wextra -Wno-unused-result -Wno-unused-parameter -Wno-missing-field-initializers -Werror=implicit-function-declaration   -I. -I./Include    -DPy_BUILD_CORE -o Objects/bytearrayobject.o Objects/bytearrayobject.c
In file included from Objects/bytearrayobject.c:96:
Objects/clinic/bytearrayobject.c.h:677:23: warning: cast between incompatible function types from ‘PyObject * (*)(PyByteArrayObject *, PyObject * const*, Py_ssize_t)’ {aka ‘struct _object * (*)(struct <anonymous> *, struct _object * const*, long int)’} to ‘PyObject * (*)(PyObject *, PyObject *)’ {aka ‘struct _object * (*)(struct _object *, struct _object *)’} [-Wcast-function-type]
     {"__reduce_ex__", (PyCFunction)bytearray_reduce_ex, METH_FASTCALL, bytearray_reduce_ex__doc__},
                       ^
Objects/bytearrayobject.c:2136:5: note: in expansion of macro ‘BYTEARRAY_REDUCE_EX_METHODDEF’
     BYTEARRAY_REDUCE_EX_METHODDEF
     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~

I'm using GCC 8.1.0 on Arch Linux.
msg317853 - (view) Author: Xavier de Gaye (xdegaye) * (Python triager) Date: 2018-05-28 14:23
> Pedantically the correct way is to cast to a function pointer with no prototype (empty parentheses)

Thanks for raising this point Antti. This is hinted at by the gcc 8 documentation on -Wcast-function-type: "The function type void (*) (void) is special and matches everything, which can be used to suppress this warning" [1]. One cannot use empty parentheses though as this raises -Wstrict-prototypes on Python builds with gcc and -Wcast-function-type with gcc 8.

So (void *) may be replaced with (void (*) (void)) in my previous code snippets and also in PR 6748 and PR 6749 (I just checked my code).

[1] https://gcc.gnu.org/onlinedocs/gcc-8.1.0/gcc/Warning-Options.html#Warning-Options
msg317854 - (view) Author: Xavier de Gaye (xdegaye) * (Python triager) Date: 2018-05-28 14:26
> There are still quite a few similar warnings on git-master. Do they indicate the same problem or should I open new issues?

This is the same issue, the warning line ends with [-Wcast-function-type].
msg318538 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-06-03 12:32
The problem with invalid function signatures was initially reported in old issue1648268.
msg326181 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2018-09-23 20:46
If I click the link for PR 6748, I see a page with "We went looking everywhere, but couldn’t find those commits."  Maybe the PR needs to be refreshed or closed and maybe reopened. PR 6749 looks normal.
msg327754 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2018-10-15 13:29
I marked bpo-34992 as a duplicate of this issue:

I use Fedora 28 and gcc (GCC) 8.1.1 20180712 (Red Hat 8.1.1-5)

and I get a lot of warnings, with the standard config of Python (./configure)

Objects/call.c: In function '_PyMethodDef_RawFastCallDict':
Objects/call.c:515:24: warning: cast between incompatible function types from 'PyCFunction' {aka 'struct _object * (*)(struct _object *, struct _object *)'} to 'PyObject * (*)(PyObject *, PyObject *, PyObject *)' {aka 'struct _object * (*)(struct _object *, struct _object *, struct _object *)'} [-Wcast-function-type]
             result = (*(PyCFunctionWithKeywords)meth) (self, argstuple, kwargs);
                        ^
Objects/call.c:530:20: warning: cast between incompatible function types from 'PyCFunction' {aka 'struct _object * (*)(struct _object *, struct _object *)'} to 'PyObject * (*)(PyObject *, PyObject * const*, Py_ssize_t)' {aka 'struct _object * (*)(struct _object *, struct _object * const*, long int)'} [-Wcast-function-type]
         result = (*(_PyCFunctionFast)meth) (self, args, nargs);
                    ^
Objects/call.c:538:49: warning: cast between incompatible function types from 'PyCFunction' {aka 'struct _object * (*)(struct _object *, struct _object *)'} to 'PyObject * (*)(PyObject *, PyObject * const*, Py_ssize_t,  PyObject *)' {aka 'struct _object * (*)(struct _object *, struct _object * const*, long int,  struct _object *)'} [-Wcast-function-type]
         _PyCFunctionFastWithKeywords fastmeth = (_PyCFunctionFastWithKeywords)meth;

....

+- 827 warnings
msg328410 - (view) Author: Michael Airey (resmord) Date: 2018-10-25 05:06
Try this - https://github.com/siddhesh/cpython/tree/func-cast
msg330498 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-11-27 09:27
New changeset 4a934d490fac779d8954a8292369c4506bab23fa by Serhiy Storchaka in branch 'master':
bpo-33012: Fix invalid function cast warnings with gcc 8 in Argument Clinic. (GH-6748)
https://github.com/python/cpython/commit/4a934d490fac779d8954a8292369c4506bab23fa
msg330505 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-11-27 11:05
New changeset 81524022d0c0df7a41f9b2b2df41e2ebe140e610 by Serhiy Storchaka in branch 'master':
bpo-33012: Fix signatures of METH_NOARGS funstions. (GH-10736)
https://github.com/python/cpython/commit/81524022d0c0df7a41f9b2b2df41e2ebe140e610
msg330506 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-11-27 11:27
New changeset 62be74290aca26d16f3f55ece7ff6dad14e60e8d by Serhiy Storchaka in branch 'master':
bpo-33012: Fix invalid function cast warnings with gcc 8. (GH-6749)
https://github.com/python/cpython/commit/62be74290aca26d16f3f55ece7ff6dad14e60e8d
msg330526 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2018-11-27 14:56
> New changeset 62be74290aca26d16f3f55ece7ff6dad14e60e8d by Serhiy Storchaka in branch 'master':
> bpo-33012: Fix invalid function cast warnings with gcc 8. (GH-6749)

I still get something 100 warnings with GCC 8.2.1 on Fedora 29. I wrote PR 10744 to add a macro to cast a function pointer.

I chose to add a macro, so it will be easier to spot where we cast function pointers and change the implementation (cast) if needed at a single place.
msg330545 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-11-27 18:27
New changeset ad8ac54aa3d2323bdb5feb5e858a922840358187 by Serhiy Storchaka in branch '3.7':
bpo-33012: Fix signatures of METH_NOARGS functions. (GH-10736) (GH-10748)
https://github.com/python/cpython/commit/ad8ac54aa3d2323bdb5feb5e858a922840358187
msg330549 - (view) Author: miss-islington (miss-islington) Date: 2018-11-27 18:51
New changeset d5c8bd8e4cc04873254f0bd38895a6479c23c8aa by Miss Islington (bot) in branch '3.6':
bpo-33012: Fix signatures of METH_NOARGS functions. (GH-10736) (GH-10748)
https://github.com/python/cpython/commit/d5c8bd8e4cc04873254f0bd38895a6479c23c8aa
msg330551 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-11-27 19:34
New changeset 1c607155c9e363489036ae6258b165a3fae75134 by Serhiy Storchaka in branch 'master':
bpo-33012: Fix more invalid function cast warnings with gcc 8. (GH-10751)
https://github.com/python/cpython/commit/1c607155c9e363489036ae6258b165a3fae75134
msg330552 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-11-27 19:45
Only one function cast warning is left, and it is a separate issue: issue33015.
msg330690 - (view) Author: miss-islington (miss-islington) Date: 2018-11-29 14:27
New changeset 1659c08d5d17357597f220c4d297b19e7a59737c by Miss Islington (bot) in branch '3.7':
bpo-33012: Fix more invalid function cast warnings with gcc 8. (GH-10751)
https://github.com/python/cpython/commit/1659c08d5d17357597f220c4d297b19e7a59737c
msg330694 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2018-11-29 14:49
New changeset 77000bbb104021b89368b9f7cab6f1417794e348 by Victor Stinner in branch '3.6':
bpo-33012: Fix more invalid function cast warnings with gcc 8. (GH-10751) (GH-10796)
https://github.com/python/cpython/commit/77000bbb104021b89368b9f7cab6f1417794e348
msg337197 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2019-03-05 14:58
I marked bpo-36197 as a duplicate of this issue:

"""
gcc -pthread -c -Wno-unused-result -Wsign-compare -DDYNAMIC_ANNOTATIONS_ENABLED=1 -g -Og -Wall    -std=c99 -Wextra -Wno-unused-result -Wno-unused-parameter -Wno-missing-field-initializers -Werror=implicit-function-declaration  -I./Include/internal  -I. -I./Include    -DPy_BUILD_CORE -o Objects/memoryobject.o Objects/memoryobject.c
Objects/memoryobject.c:3112:21: warning: cast between incompatible function types from 'PyObject * (*)(PyMemoryViewObject *, PyObject *, PyObject *)' {aka 'struct _object * (*)(struct <anonymous> *, struct _object *, struct _object *)'} to 'PyObject * (*)(PyObject *, PyObject *)' {aka 'struct _object * (*)(struct _object *, struct _object *)'} [-Wcast-function-type]
     {"tobytes",     (PyCFunction)memory_tobytes, METH_VARARGS|METH_KEYWORDS, memory_tobytes_doc},


I am preparing a small PR for this issue.
"""

=> PR 12179
msg337199 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2019-03-05 15:10
New changeset 359a2f3daba49fde0d3a07fb3c7a8b051c450d08 by Victor Stinner (Stéphane Wirtel) in branch 'master':
bpo-33012: Fix compilation warnings in memoryobject.c and _collectionsmodule.c (GH-12179)
https://github.com/python/cpython/commit/359a2f3daba49fde0d3a07fb3c7a8b051c450d08
msg341153 - (view) Author: Andreas Schneider (asn) * Date: 2019-04-30 14:53
Looking at:

https://github.com/python/cpython/commit/359a2f3daba49fde0d3a07fb3c7a8b051c450d08

This is not fixing the underlying issue but hiding it. The right fix would be to use a union for ml_meth providing members for the 3 different function. So the developer could assign them correctly and the compiler would warn if he would do something wrong. Casting to (void *) is just hiding the problem not fixing it!
msg341155 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2019-04-30 15:38
Yes, and this particular case was fixed in adfffc7343ce7ebc88ec734a803d3247ba8927fb.
msg341156 - (view) Author: Andreas Schneider (asn) * Date: 2019-04-30 15:45
And how do you deal with METH_VARARGS|METH_KEYWORDS functions which have 3 arguments?

PyObject* myfunc(PyObject *py_obj, PyObject *args, PyObject *kwargs)
msg364015 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2020-03-12 12:38
FYI I modified SystemError("bad call flags") error message to include the method name in bpo-39884.
History
Date User Action Args
2022-04-11 14:58:58adminsetgithub: 77193
2020-03-12 12:38:56vstinnersetmessages: + msg364015
2019-08-14 05:33:34ZackerySpytzsetpull_requests: + pull_request14986
2019-04-30 15:45:55asnsetmessages: + msg341156
2019-04-30 15:38:33serhiy.storchakasetmessages: + msg341155
2019-04-30 14:53:50asnsetnosy: + asn
messages: + msg341153
2019-03-05 15:10:56vstinnersetmessages: + msg337199
2019-03-05 14:58:48matrixisesetpull_requests: + pull_request12175
2019-03-05 14:58:30vstinnersetmessages: + msg337197
2019-03-05 14:57:52vstinnerlinkissue36197 superseder
2018-11-29 14:49:24vstinnersetmessages: + msg330694
2018-11-29 14:27:55miss-islingtonsetmessages: + msg330690
2018-11-29 14:22:54vstinnersetpull_requests: + pull_request10043
2018-11-29 14:10:05miss-islingtonsetpull_requests: + pull_request10042
2018-11-27 19:45:30serhiy.storchakasetstatus: open -> closed
resolution: fixed
messages: + msg330552

stage: patch review -> resolved
2018-11-27 19:34:36serhiy.storchakasetmessages: + msg330551
2018-11-27 18:51:11miss-islingtonsetmessages: + msg330549
2018-11-27 18:50:14serhiy.storchakasetpull_requests: + pull_request9999
2018-11-27 18:28:23miss-islingtonsetpull_requests: + pull_request9998
2018-11-27 18:27:50serhiy.storchakasetmessages: + msg330545
2018-11-27 17:44:28serhiy.storchakasetpull_requests: + pull_request9996
2018-11-27 14:56:59vstinnersetmessages: + msg330526
2018-11-27 14:55:02vstinnersetpull_requests: + pull_request9991
2018-11-27 11:27:34serhiy.storchakasetmessages: + msg330506
2018-11-27 11:05:06serhiy.storchakasetmessages: + msg330505
2018-11-27 10:34:05serhiy.storchakasetpull_requests: + pull_request9983
2018-11-27 09:27:40serhiy.storchakasetmessages: + msg330498
2018-10-25 05:06:25resmordsetnosy: + resmord
messages: + msg328410
2018-10-24 19:42:52gregory.p.smithsetnosy: + gregory.p.smith

versions: + Python 3.7
2018-10-15 13:29:52vstinnersetmessages: + msg327754
2018-10-15 13:29:22vstinnerlinkissue34992 superseder
2018-09-23 20:46:24terry.reedysetnosy: + terry.reedy
messages: + msg326181
2018-06-03 12:32:52serhiy.storchakasetmessages: + msg318538
2018-05-28 14:26:53xdegayesetmessages: + msg317854
2018-05-28 14:23:22xdegayesetmessages: + msg317853
2018-05-28 07:12:54yan12125setnosy: + yan12125
messages: + msg317826
2018-05-28 06:31:40siddheshsetmessages: + msg317825
2018-05-28 03:53:34ztanesetmessages: + msg317823
2018-05-27 14:27:52xdegayesetmessages: + msg317794
2018-05-26 17:25:31serhiy.storchakasetmessages: + msg317747
2018-05-26 15:16:28xdegayesetnosy: + xdegaye
messages: + msg317742
2018-05-25 11:08:06eitan.adlersetnosy: + eitan.adler
2018-05-25 11:03:04miss-islingtonsetnosy: + miss-islington
messages: + msg317670
2018-05-25 10:17:09miss-islingtonsetpull_requests: + pull_request6751
2018-05-25 10:16:54serhiy.storchakasetmessages: + msg317668
2018-05-10 18:14:01serhiy.storchakasetmessages: + msg316372
2018-05-10 18:12:20serhiy.storchakasetpull_requests: + pull_request6444
2018-05-10 12:45:34pmppsetnosy: + pmpp
2018-05-10 10:33:43serhiy.storchakasetmessages: + msg316357
2018-05-10 10:31:15cstrataksetmessages: + msg316356
2018-05-10 10:28:54serhiy.storchakasetmessages: + msg316355
2018-05-10 10:23:03serhiy.storchakasetpull_requests: + pull_request6437
2018-05-10 10:22:47serhiy.storchakasetpull_requests: + pull_request6436
2018-04-30 12:35:16serhiy.storchakasetmessages: + msg315948
2018-04-30 08:24:09serhiy.storchakasetpull_requests: + pull_request6348
2018-04-30 08:07:23siddheshsetmessages: + msg315938
2018-04-30 08:02:19vstinnersetnosy: + vstinner
messages: + msg315937
2018-04-29 18:59:35serhiy.storchakasetmessages: + msg315909
2018-04-09 10:41:33siddheshsetmessages: + msg315123
2018-04-05 00:13:46martin.pantersetmessages: + msg314962
2018-04-02 03:52:53martin.pantersetnosy: + martin.panter
messages: + msg314795
2018-03-15 15:55:27siddheshsetmessages: + msg313890
2018-03-15 15:54:03siddheshsetmessages: + msg313889
2018-03-15 15:27:14ztanesetmessages: + msg313886
2018-03-15 12:10:16serhiy.storchakasetdependencies: + Questionable code in OrderedDict definition
2018-03-15 12:07:38siddheshsetmessages: + msg313879
2018-03-15 11:33:22ztanesetnosy: + ztane
messages: + msg313875
2018-03-08 15:11:06siddheshsetkeywords: + patch
stage: patch review
pull_requests: + pull_request5792
2018-03-06 11:38:00cstrataksetnosy: + cstratak
messages: + msg313320
2018-03-06 11:32:34serhiy.storchakasetnosy: + serhiy.storchaka

messages: + msg313319
versions: + Python 3.8
2018-03-06 11:18:13siddheshcreate