-
Notifications
You must be signed in to change notification settings - Fork 2.2k
fix: type_caster_enum_type
for pointer types
#5776
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
Conversation
Add pointer overload to type_caster_enum_type::cast method to handle enum pointer casting. Fixes compilation error when returning pointers to enum types from bound functions. Experiment for validation: Temporarily undo the changes in include/pybind11/cast.h: ``` g++ -o pybind11/tests/test_native_enum.os -c -std=c++20 -fPIC -fvisibility=hidden -O0 -g -Wall -Wextra -Wconversion -Wcast-qual -Wdeprecated -Wundef -Wnon-virtual-dtor -Wunused-result -Werror -funsigned-char -Wpedantic -isystem /usr/include/python3.12 -isystem /usr/include/eigen3 -DPYBIND11_SMART_HOLDER_PADDING_ON -DPYBIND11_STRICT_ASSERTS_CLASS_HOLDER_VS_TYPE_CASTER_MIX -DPYBIND11_ENABLE_TYPE_CASTER_ODR_GUARD_IF_AVAILABLE -DPYBIND11_TEST_BOOST -Ipybind11/include -I/home/rgrossekunst/forked/pybind11/include -I/home/rgrossekunst/clone/pybind11/include /home/rgrossekunst/forked/pybind11/tests/test_native_enum.cpp In file included from /home/rgrossekunst/forked/pybind11/include/pybind11/native_enum.h:10, from /home/rgrossekunst/forked/pybind11/tests/test_native_enum.cpp:1: /home/rgrossekunst/forked/pybind11/include/pybind11/cast.h: In instantiation of ‘static pybind11::handle pybind11::detail::type_caster_enum_type<EnumType>::cast(SrcType&&, pybind11::return_value_policy, pybind11::handle) [with SrcType = const test_native_enum::color*; EnumType = test_native_enum::color]’: /home/rgrossekunst/forked/pybind11/include/pybind11/pybind11.h:429:40: required from ‘void pybind11::cpp_function::initialize(Func&&, Return (*)(Args ...), const Extra& ...) [with Func = test_submodule_native_enum(pybind11::module_&)::<lambda()>; Return = const test_native_enum::color*; Args = {}; Extra = {pybind11::name, pybind11::scope, pybind11::sibling}]’ /home/rgrossekunst/forked/pybind11/include/pybind11/pybind11.h:274:19: required from ‘pybind11::cpp_function::cpp_function(Func&&, const Extra& ...) [with Func = test_submodule_native_enum(pybind11::module_&)::<lambda()>; Extra = {pybind11::name, pybind11::scope, pybind11::sibling}; <template-parameter-1-3> = void]’ /home/rgrossekunst/forked/pybind11/include/pybind11/pybind11.h:1384:22: required from ‘pybind11::module_& pybind11::module_::def(const char*, Func&&, const Extra& ...) [with Func = test_submodule_native_enum(pybind11::module_&)::<lambda()>; Extra = {}]’ /home/rgrossekunst/forked/pybind11/tests/test_native_enum.cpp:139:10: required from here /home/rgrossekunst/forked/pybind11/include/pybind11/cast.h:70:32: error: invalid ‘static_cast’ from type ‘const test_native_enum::color*’ to type ‘pybind11::detail::type_caster_enum_type<test_native_enum::color>::Underlying’ {aka ‘unsigned int’} 70 | return native_enum(static_cast<Underlying>(src)).release(); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~ /home/rgrossekunst/forked/pybind11/include/pybind11/cast.h: In instantiation of ‘static pybind11::handle pybind11::detail::type_caster_enum_type<EnumType>::cast(SrcType&&, pybind11::return_value_policy, pybind11::handle) [with SrcType = test_native_enum::color*; EnumType = test_native_enum::color]’: /home/rgrossekunst/forked/pybind11/include/pybind11/pybind11.h:429:40: required from ‘void pybind11::cpp_function::initialize(Func&&, Return (*)(Args ...), const Extra& ...) [with Func = test_submodule_native_enum(pybind11::module_&)::<lambda()>; Return = test_native_enum::color*; Args = {}; Extra = {pybind11::name, pybind11::scope, pybind11::sibling}]’ /home/rgrossekunst/forked/pybind11/include/pybind11/pybind11.h:274:19: required from ‘pybind11::cpp_function::cpp_function(Func&&, const Extra& ...) [with Func = test_submodule_native_enum(pybind11::module_&)::<lambda()>; Extra = {pybind11::name, pybind11::scope, pybind11::sibling}; <template-parameter-1-3> = void]’ /home/rgrossekunst/forked/pybind11/include/pybind11/pybind11.h:1384:22: required from ‘pybind11::module_& pybind11::module_::def(const char*, Func&&, const Extra& ...) [with Func = test_submodule_native_enum(pybind11::module_&)::<lambda()>; Extra = {}]’ /home/rgrossekunst/forked/pybind11/tests/test_native_enum.cpp:143:10: required from here /home/rgrossekunst/forked/pybind11/include/pybind11/cast.h:70:32: error: invalid ‘static_cast’ from type ‘test_native_enum::color*’ to type ‘pybind11::detail::type_caster_enum_type<test_native_enum::color>::Underlying’ {aka ‘unsigned int’} ```
Tracking flakes: 🐍 (macos-13, 3.13t, -DCMAKE_CXX_STANDARD=11) / 🧪 test_gil_scoped.py:204: RuntimeError: DEADLOCK, most likely, exactly what this test is meant to detect. (This is a known instability.) 🐍 3.12 • macos-latest • x64 inplace C++14
@b-pass I know you're working on it already. Tagging you here JIC it helps as a clue. |
@henryiii There seems to be an unrelated problem with 🐍 (macos-13, 3.13t, -DCMAKE_CXX_STANDARD=11) / 🧪 that we need to solve separately. I'm seeing the same failure under #5777. I think this PR is good for merging. |
I don't understand what changed since yesterday, though. It's now reliably falling, it seems. Nothing noticeable has changed in the environment. Same runner version, Python version, etc - if it's logged, it's the same (diff'ed logs). |
Under #5777 it just passed again. Weird. I cannot imagine how this PR could be related. I'll try another rerun. |
@henryiii The troublesome test passed now. |
When is this going into a release? 3.0.0 is out and #5694 gotten more serious. |
I'd be in favor of a 3.0.1 soon. :) |
type_caster_enum_type
for pointer typestype_caster_enum_type
for pointer types
@henryiii wrote:
+1 I think it's a good time now. I won't be able to drive this for a while though, but I'll review release PRs asap. |
Description
Closes #5694
Add pointer overload to
type_caster_enum_type::cast
method to handle enum pointer casting. Fixes compilation error when returning pointers to enum types from bound functions.Experiment for validation:
Temporarily undo the changes in include/pybind11/cast.h:
Suggested changelog entry:
type_caster_enum_type
when casting pointer-to-enum types. Added pointer overload to handle dereferencing before enum conversion.