Skip to content

Commit 16c5b02

Browse files
vepadulanodpiparo
authored andcommitted
[cppyy] Disable warnings for invalid function casts
Some C++ functions defined in CPyCppyy are passed to CPython API, e.g. to build Python methods that use some C++ functionality. This is done by creating a instance of a PyMethodDef struct (https://docs.python.org/3/c-api/structures.html#c.PyMethodDef). One data member of this struct is of type PyCFunction (https://docs.python.org/3/c-api/structures.html#c.PyCFunction), a typedef of a function with signature ``` PyObject *PyCFunction(PyObject *self, PyObject *args); ``` In many cases, the C++ functions that are used as the data member of the PyMethodDef are not directly implemented as PyCFunction, thus need to be cast. This cast is often invalid, since many of such functions do not really respect the function signature prescribed by the API. This pattern is actually encouraged for CPython extension implementations by the official CPython docs (https://docs.python.org/3/extending/extending.html#keyword-parameters-for-extension-functions). As such, the compiler warnings that are generated becausee of the invalid function casts, for example ``` root/bindings/pyroot/cppyy/CPyCppyy/src/Pythonize.cxx:1949:50: warning: cast from 'PyObject *(*)(PyObject *)' (aka '_object *(*)(_object *)') to 'PyCFunction' (aka '_object *(*)(_object *, _object *)') converts to incompatible function type [-Wcast-function-type-mismatch] 1949 | Utility::AddToClass(pyclass, "__repr__", (PyCFunction)ComplexRepr, METH_NOARGS); ``` cannot be avoided and have to be suppressed. This is also done upstream, see wlav/CPyCppyy@33d4a6e
1 parent 0c72aea commit 16c5b02

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

bindings/pyroot/cppyy/CPyCppyy/CMakeLists.txt

+7-3
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,13 @@ if(NOT "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang" AND NOT MSVC)
7373
-Wno-unused-but-set-parameter)
7474
endif()
7575

76-
# Disables warnings coming from PyCFunction casts
77-
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" AND ${CMAKE_CXX_COMPILER_VERSION} VERSION_GREATER_EQUAL 8)
78-
target_compile_options(${libname} PRIVATE -Wno-cast-function-type)
76+
# Avoid warnings due to invalid function casts from C++ functions in CPyCppyy
77+
# to CPython API function typedefs (e.g. PyCFunction). This is a common pattern
78+
# in CPython extension implementations, explicitly encouraged by the official
79+
# CPython docs for C/C++ extensions. see
80+
# https://docs.python.org/3/extending/extending.html#keyword-parameters-for-extension-functions
81+
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
82+
target_compile_options(${libname} PRIVATE -Wno-cast-function-type -Wno-bad-function-cast)
7983
endif()
8084

8185
# Disables warnings in Python 3.8 caused by the temporary extra filed for tp_print compatibility

0 commit comments

Comments
 (0)