|
34 | 34 | /// further ABI-incompatible changes may be made before the ABI is officially
|
35 | 35 | /// changed to the new version.
|
36 | 36 | #ifndef PYBIND11_INTERNALS_VERSION
|
37 |
| -# define PYBIND11_INTERNALS_VERSION 5 |
| 37 | +# define PYBIND11_INTERNALS_VERSION 4 |
38 | 38 | #endif
|
39 | 39 |
|
40 | 40 | PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
|
@@ -209,9 +209,6 @@ struct internals {
|
209 | 209 | PYBIND11_TLS_FREE(tstate);
|
210 | 210 | }
|
211 | 211 | #endif
|
212 |
| -#if PYBIND11_INTERNALS_VERSION > 4 |
213 |
| - type_map<PyObject *> native_enum_types; |
214 |
| -#endif |
215 | 212 | };
|
216 | 213 |
|
217 | 214 | /// Additional type information which does not fit into the PyTypeObject.
|
@@ -300,15 +297,16 @@ struct type_info {
|
300 | 297 | # endif
|
301 | 298 | #endif
|
302 | 299 |
|
| 300 | +#define PYBIND11_ABI_ID \ |
| 301 | + PYBIND11_INTERNALS_KIND PYBIND11_COMPILER_TYPE PYBIND11_STDLIB PYBIND11_BUILD_ABI \ |
| 302 | + PYBIND11_BUILD_TYPE |
| 303 | + |
303 | 304 | #define PYBIND11_INTERNALS_ID \
|
304 |
| - "__pybind11_internals_v" PYBIND11_TOSTRING(PYBIND11_INTERNALS_VERSION) \ |
305 |
| - PYBIND11_INTERNALS_KIND PYBIND11_COMPILER_TYPE PYBIND11_STDLIB PYBIND11_BUILD_ABI \ |
306 |
| - PYBIND11_BUILD_TYPE "__" |
| 305 | + "__pybind11_internals_v" PYBIND11_TOSTRING(PYBIND11_INTERNALS_VERSION) PYBIND11_ABI_ID "__" |
307 | 306 |
|
308 | 307 | #define PYBIND11_MODULE_LOCAL_ID \
|
309 |
| - "__pybind11_module_local_v" PYBIND11_TOSTRING(PYBIND11_INTERNALS_VERSION) \ |
310 |
| - PYBIND11_INTERNALS_KIND PYBIND11_COMPILER_TYPE PYBIND11_STDLIB PYBIND11_BUILD_ABI \ |
311 |
| - PYBIND11_BUILD_TYPE "__" |
| 308 | + "__pybind11_module_local_v" PYBIND11_TOSTRING(PYBIND11_INTERNALS_VERSION) PYBIND11_ABI_ID "_" \ |
| 309 | + "_" |
312 | 310 |
|
313 | 311 | /// Each module locally stores a pointer to the `internals` data. The data
|
314 | 312 | /// itself is shared among modules with the same `PYBIND11_INTERNALS_ID`.
|
@@ -445,28 +443,29 @@ inline object get_python_state_dict() {
|
445 | 443 | return state_dict;
|
446 | 444 | }
|
447 | 445 |
|
| 446 | +#if defined(WITH_THREAD) |
| 447 | +# if defined(PYBIND11_SIMPLE_GIL_MANAGEMENT) |
| 448 | +using gil_scoped_acquire_simple = gil_scoped_acquire; |
| 449 | +# else |
| 450 | +// Cannot use py::gil_scoped_acquire here since that constructor calls get_internals. |
| 451 | +struct gil_scoped_acquire_simple { |
| 452 | + gil_scoped_acquire_simple() : state(PyGILState_Ensure()) {} |
| 453 | + gil_scoped_acquire_simple(const gil_scoped_acquire_simple &) = delete; |
| 454 | + gil_scoped_acquire_simple &operator=(const gil_scoped_acquire_simple &) = delete; |
| 455 | + ~gil_scoped_acquire_simple() { PyGILState_Release(state); } |
| 456 | + const PyGILState_STATE state; |
| 457 | +}; |
| 458 | +# endif |
| 459 | +#endif |
| 460 | + |
448 | 461 | /// Return a reference to the current `internals` data
|
449 | 462 | PYBIND11_NOINLINE internals &get_internals() {
|
450 | 463 | internals **&internals_pp = get_internals_pp();
|
451 | 464 | if (internals_pp && *internals_pp) {
|
452 | 465 | return **internals_pp;
|
453 | 466 | }
|
454 | 467 |
|
455 |
| -#if defined(WITH_THREAD) |
456 |
| -# if defined(PYBIND11_SIMPLE_GIL_MANAGEMENT) |
457 |
| - gil_scoped_acquire gil; |
458 |
| -# else |
459 |
| - // Ensure that the GIL is held since we will need to make Python calls. |
460 |
| - // Cannot use py::gil_scoped_acquire here since that constructor calls get_internals. |
461 |
| - struct gil_scoped_acquire_local { |
462 |
| - gil_scoped_acquire_local() : state(PyGILState_Ensure()) {} |
463 |
| - gil_scoped_acquire_local(const gil_scoped_acquire_local &) = delete; |
464 |
| - gil_scoped_acquire_local &operator=(const gil_scoped_acquire_local &) = delete; |
465 |
| - ~gil_scoped_acquire_local() { PyGILState_Release(state); } |
466 |
| - const PyGILState_STATE state; |
467 |
| - } gil; |
468 |
| -# endif |
469 |
| -#endif |
| 468 | + gil_scoped_acquire_simple gil; |
470 | 469 | error_scope err_scope;
|
471 | 470 |
|
472 | 471 | constexpr const char *id_cstr = PYBIND11_INTERNALS_ID;
|
@@ -646,4 +645,55 @@ T &get_or_create_shared_data(const std::string &name) {
|
646 | 645 | return *ptr;
|
647 | 646 | }
|
648 | 647 |
|
| 648 | +PYBIND11_NAMESPACE_BEGIN(detail) |
| 649 | + |
| 650 | +#define PYBIND11_NATIVE_ENUM_TYPE_MAP_ABI_ID \ |
| 651 | + "__pybind11_native_enum_type_map_v1" PYBIND11_ABI_ID "__" |
| 652 | + |
| 653 | +using native_enum_type_map = type_map<PyObject *>; |
| 654 | + |
| 655 | +inline native_enum_type_map **&get_native_enum_types_pp() { |
| 656 | + static native_enum_type_map **native_enum_types_pp = nullptr; |
| 657 | + return native_enum_types_pp; |
| 658 | +} |
| 659 | + |
| 660 | +PYBIND11_NOINLINE native_enum_type_map &get_native_enum_type_map() { |
| 661 | + native_enum_type_map **&native_enum_type_map_pp = get_native_enum_types_pp(); |
| 662 | + if (native_enum_type_map_pp && *native_enum_type_map_pp) { |
| 663 | + return **native_enum_type_map_pp; |
| 664 | + } |
| 665 | + |
| 666 | + gil_scoped_acquire_simple gil; |
| 667 | + error_scope err_scope; |
| 668 | + |
| 669 | + constexpr const char *id_cstr = PYBIND11_NATIVE_ENUM_TYPE_MAP_ABI_ID; |
| 670 | + str id(id_cstr); |
| 671 | + |
| 672 | + dict state_dict = get_python_state_dict(); |
| 673 | + |
| 674 | + if (state_dict.contains(id_cstr)) { |
| 675 | + void *raw_ptr = PyCapsule_GetPointer(state_dict[id].ptr(), id_cstr); |
| 676 | + if (raw_ptr == nullptr) { |
| 677 | + raise_from(PyExc_SystemError, |
| 678 | + "pybind11::detail::get_native_enum_type_map(): Retrieve " |
| 679 | + "native_enum_type_map** from capsule FAILED"); |
| 680 | + } |
| 681 | + native_enum_type_map_pp = static_cast<native_enum_type_map **>(raw_ptr); |
| 682 | + } |
| 683 | + |
| 684 | + if (native_enum_type_map_pp && *native_enum_type_map_pp) { |
| 685 | + return **native_enum_type_map_pp; |
| 686 | + } |
| 687 | + |
| 688 | + if (!native_enum_type_map_pp) { |
| 689 | + native_enum_type_map_pp = new native_enum_type_map *(); |
| 690 | + } |
| 691 | + auto *&native_enum_type_map_ptr = *native_enum_type_map_pp; |
| 692 | + native_enum_type_map_ptr = new native_enum_type_map(); |
| 693 | + state_dict[id] = capsule(native_enum_type_map_pp, id_cstr); |
| 694 | + return **native_enum_type_map_pp; |
| 695 | +} |
| 696 | + |
| 697 | +PYBIND11_NAMESPACE_END(detail) |
| 698 | + |
649 | 699 | PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)
|
0 commit comments