FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Conversions between Python's native (stdlib) enum types and C++ enums. by rwgk · Pull Request #30005 · google/pybind11clif · GitHub

Conversions between Python's native (stdlib) enum types and C++ enums. - #30005

Merged
rwgk merged 25 commits into
google:mainfrom
rwgk:native_enum_pywrapcc
Mar 16, 2023
Merged

rwgk merged 25 commits into
google:mainfrom
rwgk:native_enum_pywrapcc

Conversation

rwgk commented Mar 9, 2023
edited
Loading

Copy link
Copy Markdown
Contributor

Description

Alternative to py::enum_, binding C++ enum types to Python's native (stdlib) enum types. For example:

enum class altitude : char {
    high = 'h',
    low = 'l'
};

PYBIND11_MODULE(native_enum_demo, m) {
    m += py::native_enum<altitude>("altitude")
             .value("high", altitude::high)
             .value("low", altitude::low);
}

Note that the py::native_enum design is different from the established py::enum_ design:

    py::enum_<altitude>(m, "altitude")
             .value("high", altitude::high)
             .value("low", altitude::low);

The py::native_enum design is a mostly a trick:

  • Python's native enums cannot be built incrementally, but all name/values need to be passed at once.
  • To achieve this, py::native_enum is merely a buffer for collecting the name/value pairs. The m += operation triggers harvesting those from the buffer to build the arguments for constructing a native Python enum type.

Sidesteps issues discovered under pybind/pybind11#2744.

This PR fixes pybind/pybind11#3643 (comment), more-or-less as a side-effect.

This PR was transferred from pybind/pybind11#4349, which in turn was transferred from pybind/pybind11#4329.

Suggested changelog entry:

rwgk added 11 commits November 19, 2022 13:39
The patch .rej below are resolved, but THIS STATE DOES NOT BUILD:

```
clang++ -o pybind11/tests/test_constants_and_functions.os -c -std=c++17 -fPIC -fvisibility=hidden -O0 -g -Wall -Wextra -Wconversion -Wcast-qual -Wdeprecated -Wundef -Wnon-virtual-dtor -Wunused-result -Werror -isystem /usr/include/python3.10 -isystem /usr/include/eigen3 -DPYBIND11_STRICT_ASSERTS_CLASS_HOLDER_VS_TYPE_CASTER_MIX -DPYBIND11_ENABLE_TYPE_CASTER_ODR_GUARD_IF_AVAILABLE -DPYBIND11_TEST_BOOST -Ipybind11/include -I/usr/local/google/home/rwgk/forked/pybind11/include -I/usr/local/google/home/rwgk/clone/pybind11/include /usr/local/google/home/rwgk/forked/pybind11/tests/test_constants_and_functions.cpp
In file included from /usr/local/google/home/rwgk/forked/pybind11/tests/test_constants_and_functions.cpp:11:
In file included from /usr/local/google/home/rwgk/forked/pybind11/tests/pybind11_tests.h:3:
In file included from /usr/local/google/home/rwgk/forked/pybind11/include/pybind11/eval.h:14:
/usr/local/google/home/rwgk/forked/pybind11/include/pybind11/pybind11.h:1781:9: error: static_assert failed due to requirement '!holder_is_smart_holder == type_caster_type_is_type_caster_base_subtype' "py::class_ holder vs type_caster mismatch: missing PYBIND11_TYPE_CASTER_BASE_HOLDER(T, ...) or collision with custom py::detail::type_caster<T>?"
        static_assert(!holder_is_smart_holder == type_caster_type_is_type_caster_base_subtype,
        ^             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/local/google/home/rwgk/forked/pybind11/include/pybind11/pybind11.h:2460:11: note: in instantiation of function template specialization 'pybind11::class_<MyEnum>::class_<>' requested here
        : class_<Type>(scope, name, extra...), m_base(*this, scope) {
          ^
/usr/local/google/home/rwgk/forked/pybind11/tests/test_constants_and_functions.cpp:106:5: note: in instantiation of function template specialization 'pybind11::enum_<MyEnum>::enum_<>' requested here
    py::enum_<MyEnum>(m, "MyEnum")
    ^
1 error generated.
```

________

```
rwgk.c.googlers.com:~/forked/pybind11 $ patch -p 1 < ~/native_enum_git_diff_master_2022-11-19+131942.patch
patching file .github/workflows/python312.yml
patching file CMakeLists.txt
Hunk google#1 FAILED at 111.
Hunk google#2 succeeded at 138 (offset 5 lines).
1 out of 2 hunks FAILED -- saving rejects to file CMakeLists.txt.rej
patching file include/pybind11/cast.h
Hunk google#1 FAILED at 12.
Hunk google#2 succeeded at 78 (offset 30 lines).
Hunk google#3 succeeded at 1173 (offset 41 lines).
1 out of 3 hunks FAILED -- saving rejects to file include/pybind11/cast.h.rej
patching file include/pybind11/detail/abi_platform_id.h
patching file include/pybind11/detail/cross_extension_shared_state.h
patching file include/pybind11/detail/internals.h
Hunk google#1 FAILED at 16.
Hunk google#2 succeeded at 109 (offset 1 line).
Hunk google#3 FAILED at 203.
Hunk google#4 succeeded at 398 (offset 11 lines).
Hunk google#5 succeeded at 457 (offset 11 lines).
2 out of 5 hunks FAILED -- saving rejects to file include/pybind11/detail/internals.h.rej
patching file include/pybind11/detail/native_enum_data.h
patching file include/pybind11/detail/type_map.h
patching file include/pybind11/embed.h
patching file include/pybind11/native_enum.h
patching file include/pybind11/pybind11.h
Hunk google#1 FAILED at 12.
Hunk google#2 succeeded at 1269 (offset 1 line).
Hunk google#3 succeeded at 2457 (offset 255 lines).
1 out of 3 hunks FAILED -- saving rejects to file include/pybind11/pybind11.h.rej
patching file include/pybind11/pytypes.h
patching file tests/CMakeLists.txt
Hunk google#1 succeeded at 160 (offset 18 lines).
patching file tests/conftest.py
patching file tests/extra_python_package/test_files.py
Hunk google#2 FAILED at 47.
1 out of 2 hunks FAILED -- saving rejects to file tests/extra_python_package/test_files.py.rej
patching file tests/test_embed/test_interpreter.cpp
patching file tests/test_enum.cpp
patching file tests/test_enum.py
patching file tests/test_native_enum.cpp
patching file tests/test_native_enum.py
```
rwgk added the gha_wf_python312dev_enable Enable python312dev.yml label Mar 9, 2023
rwgk requested a review from wangxf123456 March 16, 2023 00:16
rwgk marked this pull request as ready for review March 16, 2023 00:16

rwgk commented Mar 16, 2023

Copy link
Copy Markdown
Contributor Author

For completeness: We reviewed this PR together in a meeting, spending a good 40 minutes looking at it.

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gha_wf_python312dev_enable Enable python312dev.yml

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants


Back | FazBrowse Home | New Git URL