diff --git a/.github/workflows/python312.yml b/.github/workflows/python312.yml new file mode 100644 index 0000000000..c97dbdb0d9 --- /dev/null +++ b/.github/workflows/python312.yml @@ -0,0 +1,131 @@ +name: Python312 + +on: + workflow_dispatch: + pull_request: + +concurrency: + group: python312-${{ github.ref }} + cancel-in-progress: false + +env: + PYTEST_TIMEOUT: 300 + +jobs: + standard: + name: "🐍 3.12 latest • ubuntu-latest • x64" + runs-on: ubuntu-latest + # if: "contains(github.event.pull_request.labels.*.name, 'python dev')" + + steps: + - name: Show env + run: env + + - uses: actions/checkout@v3 + + - name: Setup Python 3.12 + uses: actions/setup-python@v4 + with: + python-version: "3.12-dev" + + - name: Setup Boost + run: sudo apt-get install libboost-dev + + - name: Update CMake + uses: jwlawson/actions-setup-cmake@v1.13 + + - name: Run pip installs + run: | + python -m pip install --upgrade pip + python -m pip install --prefer-binary -r tests/requirements.txt + python -m pip install --prefer-binary numpy + # python -m pip install --prefer-binary scipy + + - name: Show platform info + run: python -m platform + + - name: Show CMake version + run: cmake --version + + # FIRST BUILD + - name: Configure C++11 + run: > + cmake -S . -B build11 + -DCMAKE_VERBOSE_MAKEFILE=ON + -DPYBIND11_WERROR=ON + -DDOWNLOAD_CATCH=ON + -DDOWNLOAD_EIGEN=ON + -DCMAKE_CXX_STANDARD=11 + -DCMAKE_BUILD_TYPE=Debug + + - name: Build C++11 + run: cmake --build build11 -j 2 + + - name: Python tests C++11 + run: cmake --build build11 --target pytest -j 2 + + - name: C++ tests C++11 + run: cmake --build build11 --target cpptest -j 2 + + - name: Interface test C++11 + run: cmake --build build11 --target test_cmake_build + + - name: Clean directory + run: git clean -fdx + + # SECOND BUILD + - name: Configure C++17 + run: > + cmake -S . -B build17 + -DCMAKE_VERBOSE_MAKEFILE=ON + -DPYBIND11_WERROR=ON + -DDOWNLOAD_CATCH=ON + -DDOWNLOAD_EIGEN=ON + -DCMAKE_CXX_STANDARD=17 + -DCMAKE_BUILD_TYPE=Debug + + - name: Build C++17 + run: cmake --build build17 -j 2 + + - name: Python tests C++17 + run: cmake --build build17 --target pytest + + - name: C++ tests C++17 + run: cmake --build build17 --target cpptest + + - name: Interface test C++17 + run: cmake --build build17 --target test_cmake_build + + - name: Clean directory + run: git clean -fdx + + # THIRD BUILD + - name: Configure C++17 max DPYBIND11_INTERNALS_VERSION + run: > + cmake -S . -B build17max + -DCMAKE_VERBOSE_MAKEFILE=ON + -DPYBIND11_WERROR=ON + -DDOWNLOAD_CATCH=ON + -DDOWNLOAD_EIGEN=ON + -DCMAKE_CXX_STANDARD=17 + -DCMAKE_BUILD_TYPE=Debug + -DPYBIND11_INTERNALS_VERSION=10000000 + + - name: Build C++17 max DPYBIND11_INTERNALS_VERSION + run: cmake --build build17max -j 2 + + - name: Python tests C++17 max DPYBIND11_INTERNALS_VERSION + run: cmake --build build17max --target pytest + + - name: C++ tests C++17 max DPYBIND11_INTERNALS_VERSION + run: cmake --build build17max --target cpptest + + - name: Interface test C++17 max DPYBIND11_INTERNALS_VERSION + run: cmake --build build17max --target test_cmake_build + + # Ensure the setup_helpers module can build packages using setuptools + - name: Setuptools helpers test + run: pytest tests/extra_setuptools + + - name: Clean directory + run: git clean -fdx diff --git a/CMakeLists.txt b/CMakeLists.txt index 0d93203881..ae85cf4e73 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -111,12 +111,16 @@ cmake_dependent_option(PYBIND11_FINDPYTHON "Force new FindPython" OFF # NB: when adding a header don't forget to also add it to setup.py set(PYBIND11_HEADERS + include/pybind11/detail/abi_platform_id.h include/pybind11/detail/class.h include/pybind11/detail/common.h + include/pybind11/detail/cross_extension_shared_state.h include/pybind11/detail/descr.h include/pybind11/detail/init.h include/pybind11/detail/internals.h + include/pybind11/detail/native_enum_data.h include/pybind11/detail/type_caster_base.h + include/pybind11/detail/type_map.h include/pybind11/detail/typeid.h include/pybind11/attr.h include/pybind11/buffer_info.h @@ -133,6 +137,7 @@ set(PYBIND11_HEADERS include/pybind11/gil.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/include/pybind11/cast.h b/include/pybind11/cast.h index 430c62f357..d3bf67190a 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -12,6 +12,7 @@ #include "detail/common.h" #include "detail/descr.h" +#include "detail/native_enum_data.h" #include "detail/type_caster_base.h" #include "detail/typeid.h" #include "pytypes.h" @@ -48,6 +49,101 @@ cast_op(make_caster &&caster) { template cast_op_type::type>(); } +template +class type_caster_enum_type { +private: + using Underlying = typename std::underlying_type::type; + +public: + static constexpr auto name = const_name(); + + template + static handle cast(SrcType &&src, return_value_policy, handle parent) { + auto const &natives = cross_extension_shared_states::native_enum_type_map::get(); + auto found = natives.find(std::type_index(typeid(EnumType))); + if (found != natives.end()) { + return handle(found->second)(static_cast(src)).release(); + } + return type_caster_base::cast( + std::forward(src), + // Fixes https://github.com/pybind/pybind11/pull/3643#issuecomment-1022987818: + return_value_policy::copy, + parent); + } + + bool load(handle src, bool convert) { + auto const &natives = cross_extension_shared_states::native_enum_type_map::get(); + auto found = natives.find(std::type_index(typeid(EnumType))); + if (found != natives.end()) { + if (!isinstance(src, found->second)) { + return false; + } + type_caster underlying_caster; + if (!underlying_caster.load(src.attr("value"), convert)) { + pybind11_fail("native_enum internal consistency failure."); + } + value = static_cast(static_cast(underlying_caster)); + return true; + } + if (!pybind11_enum_) { + pybind11_enum_.reset(new type_caster_base()); + } + return pybind11_enum_->load(src, convert); + } + + template + using cast_op_type = detail::cast_op_type; + + // NOLINTNEXTLINE(google-explicit-constructor) + operator EnumType *() { + if (!pybind11_enum_) { + return &value; + } + return pybind11_enum_->operator EnumType *(); + } + + // NOLINTNEXTLINE(google-explicit-constructor) + operator EnumType &() { + if (!pybind11_enum_) { + return value; + } + return pybind11_enum_->operator EnumType &(); + } + +private: + std::unique_ptr> pybind11_enum_; + EnumType value; +}; + +template +struct type_caster_enum_type_enabled : std::true_type {}; + +template +class type_caster::value + && type_caster_enum_type_enabled::value>> + : public type_caster_enum_type {}; + +template ::value, int> = 0> +bool isinstance_native_enum_impl(handle obj, const std::type_info &tp) { + auto const &natives = cross_extension_shared_states::native_enum_type_map::get(); + auto found = natives.find(tp); + if (found == natives.end()) { + return false; + } + return isinstance(obj, found->second); +} + +template ::value, int> = 0> +bool isinstance_native_enum_impl(handle, const std::type_info &) { + return false; +} + +template +bool isinstance_native_enum(handle obj, const std::type_info &tp) { + return isinstance_native_enum_impl>(obj, tp); +} + template class type_caster> { private: @@ -1037,8 +1133,19 @@ PYBIND11_NAMESPACE_END(detail) template ::value, int> = 0> T cast(const handle &handle) { using namespace detail; - static_assert(!cast_is_temporary_value_reference::value, + constexpr bool is_enum_cast + = std::is_base_of>, make_caster>::value; + static_assert(!cast_is_temporary_value_reference::value || is_enum_cast, "Unable to cast type to reference: value is local to type caster"); +#ifndef NDEBUG + if (is_enum_cast) { + if (cross_extension_shared_states::native_enum_type_map::get().count( + std::type_index(typeid(intrinsic_t))) + != 0) { + pybind11_fail("Unable to cast native enum type to reference"); + } + } +#endif return cast_op(load_type(handle)); } diff --git a/include/pybind11/detail/abi_platform_id.h b/include/pybind11/detail/abi_platform_id.h new file mode 100644 index 0000000000..913ba4d20f --- /dev/null +++ b/include/pybind11/detail/abi_platform_id.h @@ -0,0 +1,93 @@ +// Copyright (c) 2022 The pybind Community. +// All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +#pragma once + +#include "common.h" + +/// On MSVC, debug and release builds are not ABI-compatible! +#if defined(_MSC_VER) && defined(_DEBUG) +# define PYBIND11_BUILD_TYPE "_debug" +#else +# define PYBIND11_BUILD_TYPE "" +#endif + +/// Let's assume that different compilers are ABI-incompatible. +/// A user can manually set this string if they know their +/// compiler is compatible. +#ifndef PYBIND11_COMPILER_TYPE +# if defined(_MSC_VER) +# define PYBIND11_COMPILER_TYPE "_msvc" +# elif defined(__INTEL_COMPILER) +# define PYBIND11_COMPILER_TYPE "_icc" +# elif defined(__clang__) +# define PYBIND11_COMPILER_TYPE "_clang" +# elif defined(__PGI) +# define PYBIND11_COMPILER_TYPE "_pgi" +# elif defined(__MINGW32__) +# define PYBIND11_COMPILER_TYPE "_mingw" +# elif defined(__CYGWIN__) +# define PYBIND11_COMPILER_TYPE "_gcc_cygwin" +# elif defined(__GNUC__) +# define PYBIND11_COMPILER_TYPE "_gcc" +# else +# define PYBIND11_COMPILER_TYPE "_unknown" +# endif +#endif + +/// Also standard libs +#ifndef PYBIND11_STDLIB +# if defined(_LIBCPP_VERSION) +# define PYBIND11_STDLIB "_libcpp" +# elif defined(__GLIBCXX__) || defined(__GLIBCPP__) +# define PYBIND11_STDLIB "_libstdcpp" +# else +# define PYBIND11_STDLIB "" +# endif +#endif + +/// On Linux/OSX, changes in __GXX_ABI_VERSION__ indicate ABI incompatibility. +#ifndef PYBIND11_BUILD_ABI +# if defined(__GXX_ABI_VERSION) +# define PYBIND11_BUILD_ABI "_cxxabi" PYBIND11_TOSTRING(__GXX_ABI_VERSION) +# else +# define PYBIND11_BUILD_ABI "" +# endif +#endif + +#ifndef PYBIND11_INTERNALS_KIND +# if defined(WITH_THREAD) +# define PYBIND11_INTERNALS_KIND "" +# else +# define PYBIND11_INTERNALS_KIND "_without_thread" +# endif +#endif + +/* NOTE - ATTENTION - WARNING - EXTREME CAUTION + Changing this will break compatibility with `PYBIND11_INTERNALS_VERSION 4` + See pybind11/detail/type_map.h for more information. + */ +#define PYBIND11_PLATFORM_ABI_ID_V4 \ + PYBIND11_INTERNALS_KIND PYBIND11_COMPILER_TYPE PYBIND11_STDLIB PYBIND11_BUILD_ABI \ + PYBIND11_BUILD_TYPE + +/// LEGACY "ABI-breaking" APPROACH, ORIGINAL COMMENT +/// ------------------------------------------------ +/// Tracks the `internals` and `type_info` ABI version independent of the main library version. +/// +/// Some portions of the code use an ABI that is conditional depending on this +/// version number. That allows ABI-breaking changes to be "pre-implemented". +/// Once the default version number is incremented, the conditional logic that +/// no longer applies can be removed. Additionally, users that need not +/// maintain ABI compatibility can increase the version number in order to take +/// advantage of any functionality/efficiency improvements that depend on the +/// newer ABI. +/// +/// WARNING: If you choose to manually increase the ABI version, note that +/// pybind11 may not be tested as thoroughly with a non-default ABI version, and +/// further ABI-incompatible changes may be made before the ABI is officially +/// changed to the new version. +#ifndef PYBIND11_INTERNALS_VERSION +# define PYBIND11_INTERNALS_VERSION 4 +#endif diff --git a/include/pybind11/detail/cross_extension_shared_state.h b/include/pybind11/detail/cross_extension_shared_state.h new file mode 100644 index 0000000000..150df0b052 --- /dev/null +++ b/include/pybind11/detail/cross_extension_shared_state.h @@ -0,0 +1,142 @@ +// Copyright (c) 2022 The pybind Community. +// All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +#pragma once + +#include "common.h" + +#if defined(WITH_THREAD) && defined(PYBIND11_SIMPLE_GIL_MANAGEMENT) +# include "../gil.h" +#endif + +#include "../pytypes.h" +#include "abi_platform_id.h" + +#include + +PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_BEGIN(detail) + +inline object get_python_state_dict() { + object state_dict; +#if (PYBIND11_INTERNALS_VERSION <= 4 && PY_VERSION_HEX < 0x030C0000) \ + || PY_VERSION_HEX < 0x03080000 || defined(PYPY_VERSION) + state_dict = reinterpret_borrow(PyEval_GetBuiltins()); +#else +# if PY_VERSION_HEX < 0x03090000 + PyInterpreterState *istate = _PyInterpreterState_Get(); +# else + PyInterpreterState *istate = PyInterpreterState_Get(); +# endif + if (istate) { + state_dict = reinterpret_borrow(PyInterpreterState_GetDict(istate)); + } +#endif + if (!state_dict) { + raise_from(PyExc_SystemError, "pybind11::detail::get_python_state_dict() FAILED"); + } + return state_dict; +} + +#if defined(WITH_THREAD) +# if defined(PYBIND11_SIMPLE_GIL_MANAGEMENT) +using gil_scoped_acquire_simple = gil_scoped_acquire; +# else +// Cannot use py::gil_scoped_acquire here since that constructor calls get_internals. +struct gil_scoped_acquire_simple { + gil_scoped_acquire_simple() : state(PyGILState_Ensure()) {} + gil_scoped_acquire_simple(const gil_scoped_acquire_simple &) = delete; + gil_scoped_acquire_simple &operator=(const gil_scoped_acquire_simple &) = delete; + ~gil_scoped_acquire_simple() { PyGILState_Release(state); } + const PyGILState_STATE state; +}; +# endif +#endif + +/* NOTE: struct cross_extension_shared_state is in + namespace pybind11::detail + but all types using this struct are meant to live in + namespace pybind11::cross_extension_shared_states + to make them easy to discover and reason about. + */ +template +struct cross_extension_shared_state { + static constexpr const char *abi_id() { return AdapterType::abi_id(); } + + using payload_type = typename AdapterType::payload_type; + + static payload_type **&payload_pp() { + // The reason for the double-indirection is documented here: + // https://github.com/pybind/pybind11/pull/1092 + static payload_type **pp; + return pp; + } + + static payload_type *get_existing() { + if (payload_pp() && *payload_pp()) { + return *payload_pp(); + } + + gil_scoped_acquire_simple gil; + error_scope err_scope; + + str abi_id_str(AdapterType::abi_id()); + dict state_dict = get_python_state_dict(); + if (!state_dict.contains(abi_id_str)) { + return nullptr; + } + + void *raw_ptr = PyCapsule_GetPointer(state_dict[abi_id_str].ptr(), AdapterType::abi_id()); + if (raw_ptr == nullptr) { + raise_from(PyExc_SystemError, + ("pybind11::detail::cross_extension_shared_state::get_existing():" + " Retrieve payload_type** from capsule FAILED for ABI ID \"" + + std::string(AdapterType::abi_id()) + "\"") + .c_str()); + } + payload_pp() = static_cast(raw_ptr); + return *payload_pp(); + } + + static payload_type &get() { + payload_type *existing = get_existing(); + if (existing != nullptr) { + return *existing; + } + if (payload_pp() == nullptr) { + payload_pp() = new payload_type *(); + } + *payload_pp() = new payload_type(); + get_python_state_dict()[AdapterType::abi_id()] + = capsule(payload_pp(), AdapterType::abi_id()); + return **payload_pp(); + } + + struct scoped_clear { + // To be called BEFORE Py_Finalize(). + scoped_clear() { + payload_type *existing = get_existing(); + if (existing != nullptr) { + AdapterType::payload_clear(*existing); + arm_dtor = true; + } + } + + // To be called AFTER Py_Finalize(). + ~scoped_clear() { + if (arm_dtor) { + delete *payload_pp(); + *payload_pp() = nullptr; + } + } + + scoped_clear(const scoped_clear &) = delete; + scoped_clear &operator=(const scoped_clear &) = delete; + + bool arm_dtor = false; + }; +}; + +PYBIND11_NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) diff --git a/include/pybind11/detail/internals.h b/include/pybind11/detail/internals.h index 6fd61098c4..4aaf138960 100644 --- a/include/pybind11/detail/internals.h +++ b/include/pybind11/detail/internals.h @@ -16,26 +16,21 @@ #endif #include "../pytypes.h" +#include "abi_platform_id.h" +#include "cross_extension_shared_state.h" +#include "type_map.h" #include - -/// Tracks the `internals` and `type_info` ABI version independent of the main library version. -/// -/// Some portions of the code use an ABI that is conditional depending on this -/// version number. That allows ABI-breaking changes to be "pre-implemented". -/// Once the default version number is incremented, the conditional logic that -/// no longer applies can be removed. Additionally, users that need not -/// maintain ABI compatibility can increase the version number in order to take -/// advantage of any functionality/efficiency improvements that depend on the -/// newer ABI. -/// -/// WARNING: If you choose to manually increase the ABI version, note that -/// pybind11 may not be tested as thoroughly with a non-default ABI version, and -/// further ABI-incompatible changes may be made before the ABI is officially -/// changed to the new version. -#ifndef PYBIND11_INTERNALS_VERSION -# define PYBIND11_INTERNALS_VERSION 4 -#endif +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) @@ -108,42 +103,6 @@ inline void tls_replace_value(PYBIND11_TLS_KEY_REF key, void *value) { # define PYBIND11_TLS_FREE(key) (void) key #endif -// Python loads modules by default with dlopen with the RTLD_LOCAL flag; under libc++ and possibly -// other STLs, this means `typeid(A)` from one module won't equal `typeid(A)` from another module -// even when `A` is the same, non-hidden-visibility type (e.g. from a common include). Under -// libstdc++, this doesn't happen: equality and the type_index hash are based on the type name, -// which works. If not under a known-good stl, provide our own name-based hash and equality -// functions that use the type name. -#if defined(__GLIBCXX__) -inline bool same_type(const std::type_info &lhs, const std::type_info &rhs) { return lhs == rhs; } -using type_hash = std::hash; -using type_equal_to = std::equal_to; -#else -inline bool same_type(const std::type_info &lhs, const std::type_info &rhs) { - return lhs.name() == rhs.name() || std::strcmp(lhs.name(), rhs.name()) == 0; -} - -struct type_hash { - size_t operator()(const std::type_index &t) const { - size_t hash = 5381; - const char *ptr = t.name(); - while (auto c = static_cast(*ptr++)) { - hash = (hash * 33) ^ c; - } - return hash; - } -}; - -struct type_equal_to { - bool operator()(const std::type_index &lhs, const std::type_index &rhs) const { - return lhs.name() == rhs.name() || std::strcmp(lhs.name(), rhs.name()) == 0; - } -}; -#endif - -template -using type_map = std::unordered_map; - struct override_hash { inline size_t operator()(const std::pair &v) const { size_t value = std::hash()(v.first); @@ -239,73 +198,13 @@ struct type_info { bool module_local : 1; }; -/// On MSVC, debug and release builds are not ABI-compatible! -#if defined(_MSC_VER) && defined(_DEBUG) -# define PYBIND11_BUILD_TYPE "_debug" -#else -# define PYBIND11_BUILD_TYPE "" -#endif - -/// Let's assume that different compilers are ABI-incompatible. -/// A user can manually set this string if they know their -/// compiler is compatible. -#ifndef PYBIND11_COMPILER_TYPE -# if defined(_MSC_VER) -# define PYBIND11_COMPILER_TYPE "_msvc" -# elif defined(__INTEL_COMPILER) -# define PYBIND11_COMPILER_TYPE "_icc" -# elif defined(__clang__) -# define PYBIND11_COMPILER_TYPE "_clang" -# elif defined(__PGI) -# define PYBIND11_COMPILER_TYPE "_pgi" -# elif defined(__MINGW32__) -# define PYBIND11_COMPILER_TYPE "_mingw" -# elif defined(__CYGWIN__) -# define PYBIND11_COMPILER_TYPE "_gcc_cygwin" -# elif defined(__GNUC__) -# define PYBIND11_COMPILER_TYPE "_gcc" -# else -# define PYBIND11_COMPILER_TYPE "_unknown" -# endif -#endif - -/// Also standard libs -#ifndef PYBIND11_STDLIB -# if defined(_LIBCPP_VERSION) -# define PYBIND11_STDLIB "_libcpp" -# elif defined(__GLIBCXX__) || defined(__GLIBCPP__) -# define PYBIND11_STDLIB "_libstdcpp" -# else -# define PYBIND11_STDLIB "" -# endif -#endif - -/// On Linux/OSX, changes in __GXX_ABI_VERSION__ indicate ABI incompatibility. -#ifndef PYBIND11_BUILD_ABI -# if defined(__GXX_ABI_VERSION) -# define PYBIND11_BUILD_ABI "_cxxabi" PYBIND11_TOSTRING(__GXX_ABI_VERSION) -# else -# define PYBIND11_BUILD_ABI "" -# endif -#endif - -#ifndef PYBIND11_INTERNALS_KIND -# if defined(WITH_THREAD) -# define PYBIND11_INTERNALS_KIND "" -# else -# define PYBIND11_INTERNALS_KIND "_without_thread" -# endif -#endif - #define PYBIND11_INTERNALS_ID \ "__pybind11_internals_v" PYBIND11_TOSTRING(PYBIND11_INTERNALS_VERSION) \ - PYBIND11_INTERNALS_KIND PYBIND11_COMPILER_TYPE PYBIND11_STDLIB PYBIND11_BUILD_ABI \ - PYBIND11_BUILD_TYPE "__" + PYBIND11_PLATFORM_ABI_ID_V4 "__" #define PYBIND11_MODULE_LOCAL_ID \ "__pybind11_module_local_v" PYBIND11_TOSTRING(PYBIND11_INTERNALS_VERSION) \ - PYBIND11_INTERNALS_KIND PYBIND11_COMPILER_TYPE PYBIND11_STDLIB PYBIND11_BUILD_ABI \ - PYBIND11_BUILD_TYPE "__" + PYBIND11_PLATFORM_ABI_ID_V4 "__" /// Each module locally stores a pointer to the `internals` data. The data /// itself is shared among modules with the same `PYBIND11_INTERNALS_ID`. @@ -423,33 +322,30 @@ inline void translate_local_exception(std::exception_ptr p) { /// Return a reference to the current `internals` data PYBIND11_NOINLINE internals &get_internals() { - auto **&internals_pp = get_internals_pp(); + internals **&internals_pp = get_internals_pp(); if (internals_pp && *internals_pp) { return **internals_pp; } -#if defined(WITH_THREAD) -# if defined(PYBIND11_SIMPLE_GIL_MANAGEMENT) - gil_scoped_acquire gil; -# else - // Ensure that the GIL is held since we will need to make Python calls. - // Cannot use py::gil_scoped_acquire here since that constructor calls get_internals. - struct gil_scoped_acquire_local { - gil_scoped_acquire_local() : state(PyGILState_Ensure()) {} - gil_scoped_acquire_local(const gil_scoped_acquire_local &) = delete; - gil_scoped_acquire_local &operator=(const gil_scoped_acquire_local &) = delete; - ~gil_scoped_acquire_local() { PyGILState_Release(state); } - const PyGILState_STATE state; - } gil; -# endif -#endif + gil_scoped_acquire_simple gil; error_scope err_scope; - PYBIND11_STR_TYPE id(PYBIND11_INTERNALS_ID); - auto builtins = handle(PyEval_GetBuiltins()); - if (builtins.contains(id) && isinstance(builtins[id])) { - internals_pp = static_cast(capsule(builtins[id])); + constexpr const char *id_cstr = PYBIND11_INTERNALS_ID; + str id(id_cstr); + dict state_dict = get_python_state_dict(); + + if (state_dict.contains(id_cstr)) { + void *raw_ptr = PyCapsule_GetPointer(state_dict[id].ptr(), id_cstr); + if (raw_ptr == nullptr) { + raise_from( + PyExc_SystemError, + "pybind11::detail::get_internals(): Retrieve internals** from capsule FAILED"); + } + internals_pp = static_cast(raw_ptr); + } + + if (internals_pp && *internals_pp) { // We loaded builtins through python's builtins, which means that our `error_already_set` // and `builtin_exception` may be different local classes than the ones set up in the // initial exception translator, below, so add another for our local exception classes. @@ -485,7 +381,7 @@ PYBIND11_NOINLINE internals &get_internals() { # endif internals_ptr->istate = tstate->interp; #endif - builtins[id] = capsule(internals_pp); + state_dict[id] = capsule(internals_pp, id_cstr); internals_ptr->registered_exception_translators.push_front(&translate_exception); internals_ptr->static_property_type = make_static_property_type(); internals_ptr->default_metaclass = make_default_metaclass(); diff --git a/include/pybind11/detail/native_enum_data.h b/include/pybind11/detail/native_enum_data.h new file mode 100644 index 0000000000..e639f545b0 --- /dev/null +++ b/include/pybind11/detail/native_enum_data.h @@ -0,0 +1,87 @@ +// Copyright (c) 2022 The pybind Community. +// All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +#pragma once + +#include "../pytypes.h" +#include "abi_platform_id.h" +#include "common.h" +#include "cross_extension_shared_state.h" +#include "type_map.h" + +#include +#include + +PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_BEGIN(detail) + +class native_enum_data { +public: + native_enum_data(const char *enum_name, + const std::type_index &enum_type_index, + bool use_int_enum) + : correct_use_check{false}, enum_name_encoded{enum_name}, enum_type_index{enum_type_index}, + use_int_enum{use_int_enum}, export_values_flag{false}, enum_name{enum_name} {} + + native_enum_data(const native_enum_data &) = delete; + native_enum_data &operator=(const native_enum_data &) = delete; + + void disarm_correct_use_check() const { correct_use_check = false; } + void arm_correct_use_check() const { correct_use_check = true; } + + // This is a separate public function only to enable easy unit testing. + std::string was_not_added_error_message() const { + return "`native_enum` was not added to any module." + " Use e.g. `m += native_enum<...>(\"" + + enum_name_encoded + "\")` to fix."; + } + +#if !defined(NDEBUG) + // This dtor cannot easily be unit tested because it terminates the process. + ~native_enum_data() { + if (correct_use_check) { + pybind11_fail(was_not_added_error_message()); + } + } +#endif + +private: + mutable bool correct_use_check; + +public: + std::string enum_name_encoded; + std::type_index enum_type_index; + bool use_int_enum; + bool export_values_flag; + str enum_name; + list members; + list docs; +}; + +PYBIND11_NAMESPACE_END(detail) + +PYBIND11_NAMESPACE_BEGIN(cross_extension_shared_states) + +struct native_enum_type_map_v1_adapter { + static constexpr const char *abi_id() { + return "__pybind11_native_enum_type_map_v1" PYBIND11_PLATFORM_ABI_ID_V4 "__"; + } + + using payload_type = detail::type_map; + + static void payload_clear(payload_type &payload) { + for (auto it : payload) { + Py_DECREF(it.second); + } + payload.clear(); + } +}; + +using native_enum_type_map_v1 + = detail::cross_extension_shared_state; +using native_enum_type_map = native_enum_type_map_v1; + +PYBIND11_NAMESPACE_END(cross_extension_shared_states) + +PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) diff --git a/include/pybind11/detail/type_map.h b/include/pybind11/detail/type_map.h new file mode 100644 index 0000000000..e899f3eadc --- /dev/null +++ b/include/pybind11/detail/type_map.h @@ -0,0 +1,72 @@ +// Copyright (c) 2022 The pybind Community. +// All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +#pragma once + +#include "common.h" + +#include +#include +#include +#include + +/* NOTE - ATTENTION - WARNING - EXTREME CAUTION + + Almost any changes here will break compatibility with `PYBIND11_INTERNALS_VERSION 4` + + Recommendation: + To not break compatibility with many existing PyPI wheels (https://pypi.org/), + DO NOT MAKE CHANGES HERE + until Python 3.11 (at least) has reached EOL. Then remove this file entirely. + + To evolve this code, start with a copy of this file and move the code to + namespace pybind11::cross_extension_shared_states + with new, versioned names, e.g. type_map_v2. + */ + +PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_BEGIN(detail) + +// Python loads modules by default with dlopen with the RTLD_LOCAL flag; under libc++ and possibly +// other STLs, this means `typeid(A)` from one module won't equal `typeid(A)` from another module +// even when `A` is the same, non-hidden-visibility type (e.g. from a common include). Under +// libstdc++, this doesn't happen: equality and the type_index hash are based on the type name, +// which works. If not under a known-good stl, provide our own name-based hash and equality +// functions that use the type name. +#if defined(__GLIBCXX__) + +inline bool same_type(const std::type_info &lhs, const std::type_info &rhs) { return lhs == rhs; } +using type_hash = std::hash; +using type_equal_to = std::equal_to; + +#else + +inline bool same_type(const std::type_info &lhs, const std::type_info &rhs) { + return lhs.name() == rhs.name() || std::strcmp(lhs.name(), rhs.name()) == 0; +} + +struct type_hash { + size_t operator()(const std::type_index &t) const { + size_t hash = 5381; + const char *ptr = t.name(); + while (auto c = static_cast(*ptr++)) { + hash = (hash * 33) ^ c; + } + return hash; + } +}; + +struct type_equal_to { + bool operator()(const std::type_index &lhs, const std::type_index &rhs) const { + return lhs.name() == rhs.name() || std::strcmp(lhs.name(), rhs.name()) == 0; + } +}; + +#endif + +template +using type_map = std::unordered_map; + +PYBIND11_NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) diff --git a/include/pybind11/embed.h b/include/pybind11/embed.h index 0ac609e0f1..2138fce9ce 100644 --- a/include/pybind11/embed.h +++ b/include/pybind11/embed.h @@ -213,6 +213,8 @@ inline void initialize_interpreter(bool init_signal_handlers = true, \endrst */ inline void finalize_interpreter() { + cross_extension_shared_states::native_enum_type_map::scoped_clear native_enum_type_map_clear; + handle builtins(PyEval_GetBuiltins()); const char *id = PYBIND11_INTERNALS_ID; diff --git a/include/pybind11/native_enum.h b/include/pybind11/native_enum.h new file mode 100644 index 0000000000..13c15ab905 --- /dev/null +++ b/include/pybind11/native_enum.h @@ -0,0 +1,64 @@ +// Copyright (c) 2022 The pybind Community. +// All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +#pragma once + +#include "detail/common.h" +#include "detail/native_enum_data.h" +#include "detail/type_caster_base.h" +#include "cast.h" + +#include +#include +#include + +PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) + +/// Conversions between Python's native (stdlib) enum types and C++ enums. +template +class native_enum : public detail::native_enum_data { +public: + using Underlying = typename std::underlying_type::type; + + explicit native_enum(const char *name) + : detail::native_enum_data(name, + std::type_index(typeid(Type)), + std::numeric_limits::is_integer + && !std::is_same::value + && !detail::is_std_char_type::value) { + if (detail::get_local_type_info(typeid(Type)) != nullptr + || detail::get_global_type_info(typeid(Type)) != nullptr) { + pybind11_fail( + "pybind11::native_enum<...>(\"" + enum_name_encoded + + "\") is already registered as a `pybind11::enum_` or `pybind11::class_`!"); + } + if (cross_extension_shared_states::native_enum_type_map::get().count(enum_type_index)) { + pybind11_fail("pybind11::native_enum<...>(\"" + enum_name_encoded + + "\") is already registered!"); + } + arm_correct_use_check(); + } + + /// Export enumeration entries into the parent scope + native_enum &export_values() { + export_values_flag = true; + return *this; + } + + /// Add an enumeration entry + native_enum &value(char const *name, Type value, const char *doc = nullptr) { + disarm_correct_use_check(); + members.append(make_tuple(name, static_cast(value))); + if (doc) { + docs.append(make_tuple(name, doc)); + } + arm_correct_use_check(); + return *this; + } + + native_enum(const native_enum &) = delete; + native_enum &operator=(const native_enum &) = delete; +}; + +PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index 76d6eadcae..8f4f7269af 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -12,6 +12,7 @@ #include "detail/class.h" #include "detail/init.h" +#include "detail/native_enum_data.h" #include "attr.h" #include "gil.h" #include "options.h" @@ -1268,6 +1269,36 @@ class module_ : public object { // For Python 2, reinterpret_borrow was correct. return reinterpret_borrow(m); } + + module_ &operator+=(const detail::native_enum_data &data) { + data.disarm_correct_use_check(); + if (hasattr(*this, data.enum_name)) { + pybind11_fail("pybind11::native_enum<...>(\"" + data.enum_name_encoded + + "\"): an object with that name is already defined"); + } + auto enum_module = import("enum"); + auto py_enum_type = enum_module.attr(data.use_int_enum ? "IntEnum" : "Enum"); + auto py_enum = py_enum_type(data.enum_name, data.members); + py_enum.attr("__module__") = *this; + this->attr(data.enum_name) = py_enum; + if (data.export_values_flag) { + for (auto member : data.members) { + auto member_name = member[int_(0)]; + if (hasattr(*this, member_name)) { + pybind11_fail("pybind11::native_enum<...>(\"" + data.enum_name_encoded + + "\").value(\"" + member_name.cast() + + "\"): an object with that name is already defined"); + } + this->attr(member_name) = py_enum[member_name]; + } + } + for (auto doc : data.docs) { + py_enum[doc[int_(0)]].attr("__doc__") = doc[int_(1)]; + } + cross_extension_shared_states::native_enum_type_map::get()[data.enum_type_index] + = py_enum.release().ptr(); + return *this; + } }; // When inside a namespace (or anywhere as long as it's not the first item on a line), @@ -2172,6 +2203,15 @@ class enum_ : public class_ { template enum_(const handle &scope, const char *name, const Extra &...extra) : class_(scope, name, extra...), m_base(*this, scope) { + { + if (cross_extension_shared_states::native_enum_type_map::get().count( + std::type_index(typeid(Type))) + != 0) { + pybind11_fail("pybind11::enum_ \"" + std::string(name) + + "\" is already registered as a pybind11::native_enum!"); + } + } + constexpr bool is_arithmetic = detail::any_of...>::value; constexpr bool is_convertible = std::is_convertible::value; m_base.init(is_arithmetic, is_convertible); diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index 80b49ec397..1023358bdb 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -46,6 +46,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; @@ -736,7 +739,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 491f215cef..1e92d8b353 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -142,6 +142,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/conftest.py b/tests/conftest.py index f5ddb9f129..639b57f474 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -9,11 +9,17 @@ import gc import re import textwrap +import traceback import pytest # Early diagnostic for failed imports -import pybind11_tests +try: + import pybind11_tests +except Exception: + # pytest does not show the traceback without this. + traceback.print_exc() + raise _long_marker = re.compile(r"([0-9])L") _hexadecimal = re.compile(r"0x[0-9a-fA-F]+") diff --git a/tests/extra_python_package/test_files.py b/tests/extra_python_package/test_files.py index 9a9bb1556a..a9b09cd613 100644 --- a/tests/extra_python_package/test_files.py +++ b/tests/extra_python_package/test_files.py @@ -36,6 +36,7 @@ "include/pybind11/functional.h", "include/pybind11/gil.h", "include/pybind11/iostream.h", + "include/pybind11/native_enum.h", "include/pybind11/numpy.h", "include/pybind11/operators.h", "include/pybind11/options.h", @@ -46,12 +47,16 @@ } detail_headers = { + "include/pybind11/detail/abi_platform_id.h", "include/pybind11/detail/class.h", "include/pybind11/detail/common.h", + "include/pybind11/detail/cross_extension_shared_state.h", "include/pybind11/detail/descr.h", "include/pybind11/detail/init.h", "include/pybind11/detail/internals.h", + "include/pybind11/detail/native_enum_data.h", "include/pybind11/detail/type_caster_base.h", + "include/pybind11/detail/type_map.h", "include/pybind11/detail/typeid.h", } diff --git a/tests/test_embed/test_interpreter.cpp b/tests/test_embed/test_interpreter.cpp index 44dcd1fdb0..e49f9adfc8 100644 --- a/tests/test_embed/test_interpreter.cpp +++ b/tests/test_embed/test_interpreter.cpp @@ -168,9 +168,8 @@ TEST_CASE("There can be only one interpreter") { py::initialize_interpreter(); } -bool has_pybind11_internals_builtin() { - auto builtins = py::handle(PyEval_GetBuiltins()); - return builtins.contains(PYBIND11_INTERNALS_ID); +bool has_pybind11_internals_capsule() { + return py::detail::get_python_state_dict().contains(PYBIND11_INTERNALS_ID); }; bool has_pybind11_internals_static() { @@ -181,7 +180,7 @@ bool has_pybind11_internals_static() { TEST_CASE("Restart the interpreter") { // Verify pre-restart state. REQUIRE(py::module_::import("widget_module").attr("add")(1, 2).cast() == 3); - REQUIRE(has_pybind11_internals_builtin()); + REQUIRE(has_pybind11_internals_capsule()); REQUIRE(has_pybind11_internals_static()); REQUIRE(py::module_::import("external_module").attr("A")(123).attr("value").cast() == 123); @@ -198,10 +197,10 @@ TEST_CASE("Restart the interpreter") { REQUIRE(Py_IsInitialized() == 1); // Internals are deleted after a restart. - REQUIRE_FALSE(has_pybind11_internals_builtin()); + REQUIRE_FALSE(has_pybind11_internals_capsule()); REQUIRE_FALSE(has_pybind11_internals_static()); pybind11::detail::get_internals(); - REQUIRE(has_pybind11_internals_builtin()); + REQUIRE(has_pybind11_internals_capsule()); REQUIRE(has_pybind11_internals_static()); REQUIRE(reinterpret_cast(*py::detail::get_internals_pp()) == py::module_::import("external_module").attr("internals_at")().cast()); @@ -216,13 +215,13 @@ TEST_CASE("Restart the interpreter") { py::detail::get_internals(); *static_cast(ran) = true; }); - REQUIRE_FALSE(has_pybind11_internals_builtin()); + REQUIRE_FALSE(has_pybind11_internals_capsule()); REQUIRE_FALSE(has_pybind11_internals_static()); REQUIRE_FALSE(ran); py::finalize_interpreter(); REQUIRE(ran); py::initialize_interpreter(); - REQUIRE_FALSE(has_pybind11_internals_builtin()); + REQUIRE_FALSE(has_pybind11_internals_capsule()); REQUIRE_FALSE(has_pybind11_internals_static()); // C++ modules can be reloaded. @@ -244,7 +243,7 @@ TEST_CASE("Subinterpreter") { REQUIRE(m.attr("add")(1, 2).cast() == 3); } - REQUIRE(has_pybind11_internals_builtin()); + REQUIRE(has_pybind11_internals_capsule()); REQUIRE(has_pybind11_internals_static()); /// Create and switch to a subinterpreter. @@ -254,7 +253,7 @@ TEST_CASE("Subinterpreter") { // Subinterpreters get their own copy of builtins. detail::get_internals() still // works by returning from the static variable, i.e. all interpreters share a single // global pybind11::internals; - REQUIRE_FALSE(has_pybind11_internals_builtin()); + REQUIRE_FALSE(has_pybind11_internals_capsule()); REQUIRE(has_pybind11_internals_static()); // Modules tags should be gone. diff --git a/tests/test_enum.cpp b/tests/test_enum.cpp index 2597b275ef..4ec0af7245 100644 --- a/tests/test_enum.cpp +++ b/tests/test_enum.cpp @@ -130,4 +130,20 @@ TEST_SUBMODULE(enums, m) { py::enum_(m, "ScopedBoolEnum") .value("FALSE", ScopedBoolEnum::FALSE) .value("TRUE", ScopedBoolEnum::TRUE); + +#if defined(__MINGW32__) + m.attr("obj_cast_UnscopedEnum_ptr") = "MinGW: dangling pointer to an unnamed temporary may be " + "used [-Werror=dangling-pointer=]"; +#else + m.def("obj_cast_UnscopedEnum_ptr", [](const py::object &obj) { + // https://github.com/OpenImageIO/oiio/blob/30ea4ebdfab11aec291befbaff446f2a7d24835b/src/python/py_oiio.h#L300 + if (py::isinstance(obj)) { + if (*obj.cast() == UnscopedEnum::ETwo) { + return 2; + } + return 1; + } + return 0; + }); +#endif } diff --git a/tests/test_enum.py b/tests/test_enum.py index f14a72398f..64c898b2d1 100644 --- a/tests/test_enum.py +++ b/tests/test_enum.py @@ -262,3 +262,12 @@ def test_docstring_signatures(): for attr in enum_type.__dict__.values(): # Issue #2623/PR #2637: Add argument names to enum_ methods assert "arg0" not in (attr.__doc__ or "") + + +@pytest.mark.skipif( + isinstance(m.obj_cast_UnscopedEnum_ptr, str), reason=m.obj_cast_UnscopedEnum_ptr +) +def test_obj_cast_unscoped_enum_ptr(): + assert m.obj_cast_UnscopedEnum_ptr(m.UnscopedEnum.ETwo) == 2 + assert m.obj_cast_UnscopedEnum_ptr(m.UnscopedEnum.EOne) == 1 + assert m.obj_cast_UnscopedEnum_ptr(None) == 0 diff --git a/tests/test_native_enum.cpp b/tests/test_native_enum.cpp new file mode 100644 index 0000000000..d634f54db1 --- /dev/null +++ b/tests/test_native_enum.cpp @@ -0,0 +1,169 @@ +#include + +#include "pybind11_tests.h" + +#include + +namespace test_native_enum { + +// https://en.cppreference.com/w/cpp/language/enum + +// enum that takes 16 bits +enum smallenum : std::int16_t { a, b, c }; + +// color may be red (value 0), yellow (value 1), green (value 20), or blue (value 21) +enum color { red, yellow, green = 20, blue }; + +// altitude may be altitude::high or altitude::low +enum class altitude : char { + high = 'h', + low = 'l', // trailing comma only allowed after CWG518 +}; + +enum class export_values { exv0, exv1 }; + +enum class member_doc { mem0, mem1, mem2 }; + +// https://github.com/protocolbuffers/protobuf/blob/d70b5c5156858132decfdbae0a1103e6a5cb1345/src/google/protobuf/generated_enum_util.h#L52-L53 +template +struct is_proto_enum : std::false_type {}; + +enum some_proto_enum : int { Zero, One }; + +template <> +struct is_proto_enum : std::true_type {}; + +} // namespace test_native_enum + +PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) +PYBIND11_NAMESPACE_BEGIN(detail) + +template +struct type_caster_enum_type_enabled< + ProtoEnumType, + detail::enable_if_t::value>> : std::false_type { +}; + +// https://github.com/pybind/pybind11_protobuf/blob/a50899c2eb604fc5f25deeb8901eff6231b8b3c0/pybind11_protobuf/enum_type_caster.h#L101-L105 +template +struct type_caster::value>> { + static handle + cast(const ProtoEnumType & /*src*/, return_value_policy /*policy*/, handle /*parent*/) { + return py::none(); + } + + bool load(handle /*src*/, bool /*convert*/) { + value = static_cast(0); + return true; + } + + PYBIND11_TYPE_CASTER(ProtoEnumType, const_name()); +}; + +PYBIND11_NAMESPACE_END(detail) +PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) + +TEST_SUBMODULE(native_enum, m) { + using namespace test_native_enum; + + m.attr("native_enum_type_map_abi_id_c_str") + = py::cross_extension_shared_states::native_enum_type_map::abi_id(); + + m += py::native_enum("smallenum") + .value("a", smallenum::a) + .value("b", smallenum::b) + .value("c", smallenum::c); + + m += py::native_enum("color") + .value("red", color::red) + .value("yellow", color::yellow) + .value("green", color::green) + .value("blue", color::blue); + + m += py::native_enum("altitude") + .value("high", altitude::high) + .value("low", altitude::low); + + m += py::native_enum("export_values") + .value("exv0", export_values::exv0) + .value("exv1", export_values::exv1) + .export_values(); + + m += py::native_enum("member_doc") + .value("mem0", member_doc::mem0, "docA") + .value("mem1", member_doc::mem1) + .value("mem2", member_doc::mem2, "docC"); + + m.def("isinstance_color", [](const py::object &obj) { return py::isinstance(obj); }); + + m.def("pass_color", [](color e) { return static_cast(e); }); + m.def("return_color", [](int i) { return static_cast(i); }); + + m.def("pass_some_proto_enum", [](some_proto_enum) { return py::none(); }); + m.def("return_some_proto_enum", []() { return some_proto_enum::Zero; }); + +#if defined(__MINGW32__) + m.attr("obj_cast_color_ptr") = "MinGW: dangling pointer to an unnamed temporary may be used " + "[-Werror=dangling-pointer=]"; +#elif defined(NDEBUG) + m.attr("obj_cast_color_ptr") = "NDEBUG disables cast safety check"; +#else + m.def("obj_cast_color_ptr", [](const py::object &obj) { obj.cast(); }); +#endif + + m.def("native_enum_data_was_not_added_error_message", [](const char *enum_name) { + py::detail::native_enum_data data(enum_name, std::type_index(typeid(void)), false); + data.disarm_correct_use_check(); + return data.was_not_added_error_message(); + }); + + m.def("native_enum_ctor_malformed_utf8", [](const char *malformed_utf8) { + enum fake { x }; + py::native_enum{malformed_utf8}; + }); + + m.def("native_enum_value_malformed_utf8", [](const char *malformed_utf8) { + enum fake { x }; + py::native_enum("fake").value(malformed_utf8, fake::x); + }); + + m.def("double_registration_native_enum", [](py::module_ m) { + enum fake { x }; + m += py::native_enum("fake_double_registration_native_enum").value("x", fake::x); + py::native_enum("fake_double_registration_native_enum"); + }); + + m.def("native_enum_name_clash", [](py::module_ m) { + enum fake { x }; + m += py::native_enum("fake_native_enum_name_clash").value("x", fake::x); + }); + + m.def("native_enum_value_name_clash", [](py::module_ m) { + enum fake { x }; + m += py::native_enum("fake_native_enum_value_name_clash") + .value("fake_native_enum_value_name_clash_x", fake::x) + .export_values(); + }); + + m.def("double_registration_enum_before_native_enum", [](const py::module_ &m) { + enum fake { x }; + py::enum_(m, "fake_enum_first").value("x", fake::x); + py::native_enum("fake_enum_first").value("x", fake::x); + }); + + m.def("double_registration_native_enum_before_enum", [](py::module_ m) { + enum fake { x }; + m += py::native_enum("fake_native_enum_first").value("x", fake::x); + py::enum_(m, "name_must_be_different_to_reach_desired_code_path"); + }); + +#if defined(PYBIND11_NEGATE_THIS_CONDITION_FOR_LOCAL_TESTING) && !defined(NDEBUG) + m.def("native_enum_correct_use_failure", []() { + enum fake { x }; + py::native_enum("fake_native_enum_correct_use_failure").value("x", fake::x); + }); +#else + m.attr("native_enum_correct_use_failure") = "For local testing only: terminates process"; +#endif +} diff --git a/tests/test_native_enum.py b/tests/test_native_enum.py new file mode 100644 index 0000000000..a0ce8dba7e --- /dev/null +++ b/tests/test_native_enum.py @@ -0,0 +1,198 @@ +import enum +import re + +import pytest + +from pybind11_tests import native_enum as m + + +def test_abi_id(): + assert re.match( + "__pybind11_native_enum_type_map_v1_.*__$", m.native_enum_type_map_abi_id_c_str + ) + + +SMALLENUM_MEMBERS = ( + ("a", 0), + ("b", 1), + ("c", 2), +) + +COLOR_MEMBERS = ( + ("red", 0), + ("yellow", 1), + ("green", 20), + ("blue", 21), +) + +ALTITUDE_MEMBERS = ( + ("high", "h"), + ("low", "l"), +) + +EXPORT_VALUES_MEMBERS = ( + ("exv0", 0), + ("exv1", 1), +) + +MEMBER_DOC_MEMBERS = ( + ("mem0", 0), + ("mem1", 1), + ("mem2", 2), +) + + +@pytest.mark.parametrize( + "enum_type", (m.smallenum, m.color, m.altitude, m.export_values, m.member_doc) +) +def test_enum_type(enum_type): + assert isinstance(enum_type, enum.EnumMeta) + + +@pytest.mark.parametrize( + "enum_type,members", + ( + (m.smallenum, SMALLENUM_MEMBERS), + (m.color, COLOR_MEMBERS), + (m.altitude, ALTITUDE_MEMBERS), + (m.export_values, EXPORT_VALUES_MEMBERS), + (m.member_doc, MEMBER_DOC_MEMBERS), + ), +) +def test_enum_members(enum_type, members): + for name, value in members: + assert enum_type[name].value == value + + +def test_export_values(): + assert m.exv0 is m.export_values.exv0 + assert m.exv1 is m.export_values.exv1 + + +def test_member_doc(): + pure_native = enum.IntEnum("pure_native", (("mem", 0),)) + assert m.member_doc.mem0.__doc__ == "docA" + assert m.member_doc.mem1.__doc__ == pure_native.mem.__doc__ + assert m.member_doc.mem2.__doc__ == "docC" + + +def test_pybind11_isinstance_color(): + for name, _ in COLOR_MEMBERS: + assert m.isinstance_color(m.color[name]) + assert not m.isinstance_color(m.color) + for name, _ in SMALLENUM_MEMBERS: + assert not m.isinstance_color(m.smallenum[name]) + assert not m.isinstance_color(m.smallenum) + assert not m.isinstance_color(None) + + +def test_pass_color_success(): + for name, value in COLOR_MEMBERS: + assert m.pass_color(m.color[name]) == value + + +def test_pass_color_fail(): + with pytest.raises(TypeError) as excinfo: + m.pass_color(None) + assert "test_native_enum::color" in str(excinfo.value) + + +def test_return_color_success(): + for name, value in COLOR_MEMBERS: + assert m.return_color(value) == m.color[name] + + +def test_return_color_fail(): + with pytest.raises(ValueError) as excinfo_direct: + m.color(2) + with pytest.raises(ValueError) as excinfo_cast: + m.return_color(2) + assert str(excinfo_cast.value) == str(excinfo_direct.value) + + +def test_type_caster_enum_type_enabled_false(): + # This is really only a "does it compile" test. + assert m.pass_some_proto_enum(None) is None + assert m.return_some_proto_enum() is None + + +@pytest.mark.skipif(isinstance(m.obj_cast_color_ptr, str), reason=m.obj_cast_color_ptr) +def test_obj_cast_color_ptr(): + with pytest.raises(RuntimeError) as excinfo: + m.obj_cast_color_ptr(m.color.red) + assert str(excinfo.value) == "Unable to cast native enum type to reference" + + +def test_native_enum_data_was_not_added_error_message(): + msg = m.native_enum_data_was_not_added_error_message("Fake") + assert msg == ( + "`native_enum` was not added to any module." + ' Use e.g. `m += native_enum<...>("Fake")` to fix.' + ) + + +@pytest.mark.parametrize( + "func", (m.native_enum_ctor_malformed_utf8, m.native_enum_value_malformed_utf8) +) +def test_native_enum_malformed_utf8(func): + malformed_utf8 = b"\x80" + with pytest.raises(UnicodeDecodeError): + func(malformed_utf8) + + +def test_double_registration_native_enum(): + with pytest.raises(RuntimeError) as excinfo: + m.double_registration_native_enum(m) + assert ( + str(excinfo.value) + == 'pybind11::native_enum<...>("fake_double_registration_native_enum") is already registered!' + ) + + +def test_native_enum_name_clash(): + m.fake_native_enum_name_clash = None + with pytest.raises(RuntimeError) as excinfo: + m.native_enum_name_clash(m) + assert ( + str(excinfo.value) + == 'pybind11::native_enum<...>("fake_native_enum_name_clash"):' + " an object with that name is already defined" + ) + + +def test_native_enum_value_name_clash(): + m.fake_native_enum_value_name_clash_x = None + with pytest.raises(RuntimeError) as excinfo: + m.native_enum_value_name_clash(m) + assert ( + str(excinfo.value) + == 'pybind11::native_enum<...>("fake_native_enum_value_name_clash")' + '.value("fake_native_enum_value_name_clash_x"):' + " an object with that name is already defined" + ) + + +def test_double_registration_enum_before_native_enum(): + with pytest.raises(RuntimeError) as excinfo: + m.double_registration_enum_before_native_enum(m) + assert ( + str(excinfo.value) + == 'pybind11::native_enum<...>("fake_enum_first") is already registered' + " as a `pybind11::enum_` or `pybind11::class_`!" + ) + + +def test_double_registration_native_enum_before_enum(): + with pytest.raises(RuntimeError) as excinfo: + m.double_registration_native_enum_before_enum(m) + assert ( + str(excinfo.value) + == 'pybind11::enum_ "name_must_be_different_to_reach_desired_code_path"' + " is already registered as a pybind11::native_enum!" + ) + + +def test_native_enum_correct_use_failure(): + if not isinstance(m.native_enum_correct_use_failure, str): + m.native_enum_correct_use_failure() + pytest.fail("Process termination expected.")