From edf03ccd341e995fe122971c6c513641f6e1299c Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Wed, 31 Jul 2024 13:41:34 -0700 Subject: [PATCH] Changes extracted from pybind11k commit f8128f0b0d712c210d7d4470a50acc88dc4604ea (PR #30005) --- CMakeLists.txt | 2 ++ docs/classes.rst | 5 +++++ include/pybind11/pytypes.h | 6 +++++- tests/CMakeLists.txt | 1 + tests/extra_python_package/test_files.py | 2 ++ 5 files changed, 15 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 0964e2eef..82eca8923 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -153,6 +153,7 @@ set(PYBIND11_HEADERS include/pybind11/detail/dynamic_raw_ptr_cast_if_possible.h include/pybind11/detail/init.h include/pybind11/detail/internals.h + include/pybind11/detail/native_enum_data.h include/pybind11/detail/smart_holder_poc.h include/pybind11/detail/type_caster_base.h include/pybind11/detail/typeid.h @@ -175,6 +176,7 @@ set(PYBIND11_HEADERS include/pybind11/gil_safe_call_once.h include/pybind11/iostream.h include/pybind11/functional.h + include/pybind11/native_enum.h include/pybind11/numpy.h include/pybind11/operators.h include/pybind11/pybind11.h diff --git a/docs/classes.rst b/docs/classes.rst index 4f2167dac..1b165e8fa 100644 --- a/docs/classes.rst +++ b/docs/classes.rst @@ -553,3 +553,8 @@ The ``name`` property returns the name of the enum value as a unicode string. .. warning:: Contrary to Python customs, enum values from the wrappers should not be compared using ``is``, but with ``==`` (see `#1177 `_ for background). + +.. note:: + + ``py::native_enum`` was added as an alternative to ``py::enum_`` + with http://github.com/pybind/pybind11/pull/9999 diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index f26c307a8..cb333f7af 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -48,6 +48,9 @@ PYBIND11_NAMESPACE_BEGIN(detail) class args_proxy; bool isinstance_generic(handle obj, const std::type_info &tp); +template +bool isinstance_native_enum(handle obj, const std::type_info &tp); + // Accessor forward declarations template class accessor; @@ -845,7 +848,8 @@ bool isinstance(handle obj) { template ::value, int> = 0> bool isinstance(handle obj) { - return detail::isinstance_generic(obj, typeid(T)); + return detail::isinstance_native_enum(obj, typeid(T)) + || detail::isinstance_generic(obj, typeid(T)); } template <> diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 1007d6d82..1a3ef7149 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -162,6 +162,7 @@ set(PYBIND11_TEST_FILES test_methods_and_attributes test_modules test_multiple_inheritance + test_native_enum test_numpy_array test_numpy_dtypes test_numpy_vectorize diff --git a/tests/extra_python_package/test_files.py b/tests/extra_python_package/test_files.py index 5c9705371..e95ed4d36 100644 --- a/tests/extra_python_package/test_files.py +++ b/tests/extra_python_package/test_files.py @@ -39,6 +39,7 @@ main_headers = { "include/pybind11/gil.h", "include/pybind11/gil_safe_call_once.h", "include/pybind11/iostream.h", + "include/pybind11/native_enum.h", "include/pybind11/numpy.h", "include/pybind11/operators.h", "include/pybind11/options.h", @@ -59,6 +60,7 @@ detail_headers = { "include/pybind11/detail/dynamic_raw_ptr_cast_if_possible.h", "include/pybind11/detail/init.h", "include/pybind11/detail/internals.h", + "include/pybind11/detail/native_enum_data.h", "include/pybind11/detail/smart_holder_poc.h", "include/pybind11/detail/type_caster_base.h", "include/pybind11/detail/typeid.h",