Skip to content

Change PyCFunction cast in function_record_pyobject.h to sidestep unhelpful compiler warnings. #5608

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 10, 2025

Conversation

rwgk
Copy link
Collaborator

@rwgk rwgk commented Apr 9, 2025

Description

With a little help from ChatGPT: https://chatgpt.com/share/67f60e22-8964-8008-aca6-1e13d24e019e

Although rwgk wants/needs to take full responsibility for the void * cast trick/hack:

Casting to void * before casting to PyCFunction is a more portable and easier way to silence compiler warnings.

Using C++-style casts is mostly a matter of style. Probably, C-style casts would work, too.

Suggested changelog entry:

rwgk added 2 commits April 8, 2025 23:04
/__w/pybind11/pybind11/include/pybind11/detail/function_record_pyobject.h:41:39: error: do not cast 'PyObject *(PyObject *, PyObject *, PyObject *)' (aka '_object *(_object *, _object *, _object *)') to 'PyCFunction' (aka '_object *(*)(_object *, _object *)') through 'void *' [bugprone-casting-through-void,-warnings-as-errors]
   41 |         reinterpret_cast<PyCFunction>(reinterpret_cast<void *>(reduce_ex_impl)),
      |                                       ^
@rwgk rwgk marked this pull request as ready for review April 9, 2025 06:57
@rwgk rwgk requested a review from henryiii April 9, 2025 06:58
Copy link
Collaborator

@henryiii henryiii left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just changing a C-style cast to a C++-style cast, right? It fixes the warning for me.

@rwgk
Copy link
Collaborator Author

rwgk commented Apr 10, 2025

This is just changing a C-style cast to a C++-style cast, right? It fixes the warning for me.

Thanks for confirming!

I added this to the PR description:

Casting to void * before casting to PyCFunction is a more portable and easier way to silence compiler warnings.

Using C++-style casts is mostly a matter of style. Probably, C-style casts would work, too.

@rwgk rwgk merged commit 1bd1d1c into pybind:master Apr 10, 2025
75 checks passed
@rwgk rwgk deleted the function_record_pyobject_cast branch April 10, 2025 19:10
@github-actions github-actions bot added the needs changelog Possibly needs a changelog entry label Apr 10, 2025
@rwgk rwgk removed the needs changelog Possibly needs a changelog entry label Apr 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants