diff --git a/include/pybind11/internal/builtins.h b/include/pybind11/internal/builtins.h index 34b8c462..52f65218 100644 --- a/include/pybind11/internal/builtins.h +++ b/include/pybind11/internal/builtins.h @@ -17,7 +17,9 @@ inline object eval(std::string_view code, handle globals = none(), handle locals return object::from_ret(); } else { handle eval = py_getbuiltin(py_name("eval")); - return eval(str(code), globals.is_none() ? dict() : globals, locals.is_none() ? dict() : locals); + return eval(str(code), + globals.is_none() ? dict() : globals, + locals.is_none() ? dict() : locals); } } @@ -75,7 +77,8 @@ inline bool isinstance(handle obj, type type) { return py_isinstance(obj.ptr(), inline bool python_error::match(type type) const { return isinstance(m_exception.ptr(), type); } template -constexpr inline bool is_pyobject_v = std::is_base_of_v> || std::is_same_v; +constexpr inline bool is_pyobject_v = + std::is_base_of_v> || std::is_same_v; template inline type type::of() { @@ -86,8 +89,8 @@ inline type type::of() { return type(T::type_or_check()); } } else { - auto it = m_type_map->find(typeid(T)); - if(it != m_type_map->end()) { + auto it = m_type_map.find(typeid(T)); + if(it != m_type_map.end()) { return type(it->second); } else { // if not found, raise error @@ -104,8 +107,8 @@ inline bool type::isinstance(handle obj) { if constexpr(is_pyobject_v) { // for every python object wrapper type, there must be a `type_or_check` method. // for some types, it returns the underlying type in pkpy, e.g., `int_` -> `tp_int`. - // for other types that may not have a corresponding type in pkpy, it returns a check function. - // e.g., `iterable` -> `[](handle h){ return hasattr(h, "iter"); }`. + // for other types that may not have a corresponding type in pkpy, it returns a check + // function. e.g., `iterable` -> `[](handle h){ return hasattr(h, "iter"); }`. auto type_or_check = T::type_or_check(); if constexpr(is_check_v) { return type_or_check(obj); @@ -117,7 +120,9 @@ inline bool type::isinstance(handle obj) { } } -inline bool issubclass(type derived, type base) { return py_issubclass(derived.index(), base.index()); } +inline bool issubclass(type derived, type base) { + return py_issubclass(derived.index(), base.index()); +} template inline bool isinstance(handle obj) { @@ -131,17 +136,23 @@ template struct type_caster; template -object cast(T&& value, return_value_policy policy = return_value_policy::automatic_reference, handle parent = {}) { +object cast(T&& value, + return_value_policy policy = return_value_policy::automatic_reference, + handle parent = {}) { // decay_t can resolve c-array type, but remove_cv_ref_t can't. using underlying_type = std::decay_t; - if constexpr(std::is_convertible_v && !is_pyobject_v) { + if constexpr(std::is_convertible_v && + !is_pyobject_v) { return object(std::forward(value), object::realloc_t{}); } else if constexpr(is_unique_pointer_v) { using pointer = typename underlying_type::pointer; - return type_caster::cast(value.release(), return_value_policy::take_ownership, parent); + return type_caster::cast(value.release(), + return_value_policy::take_ownership, + parent); } else { - static_assert(!is_multiple_pointer_v, "multiple pointer is not supported."); + static_assert(!is_multiple_pointer_v, + "multiple pointer is not supported."); static_assert(!std::is_void_v>, "void* is not supported, consider using py::capsule."); @@ -163,7 +174,8 @@ object cast(T&& value, return_value_policy policy = return_value_policy::automat template T cast(handle obj, bool convert = true) { using caster_t = type_caster; - constexpr auto is_dangling_v = (std::is_reference_v || is_pointer_v) && caster_t::is_temporary_v; + constexpr auto is_dangling_v = + (std::is_reference_v || is_pointer_v) && caster_t::is_temporary_v; static_assert(!is_dangling_v, "dangling reference or pointer is not allowed."); assert(obj.ptr() != nullptr); diff --git a/include/pybind11/internal/class.h b/include/pybind11/internal/class.h index edd72924..ed05f988 100644 --- a/include/pybind11/internal/class.h +++ b/include/pybind11/internal/class.h @@ -25,7 +25,7 @@ public: static_cast(data)->~instance(); })), m_scope(scope) { - m_type_map->try_emplace(typeid(T), this->index()); + m_type_map.try_emplace(typeid(T), this->index()); auto& info = type_info::of(); info.name = name; diff --git a/include/pybind11/internal/kernel.h b/include/pybind11/internal/kernel.h index 03dde561..3b240f93 100644 --- a/include/pybind11/internal/kernel.h +++ b/include/pybind11/internal/kernel.h @@ -128,6 +128,4 @@ private: void (*init)(T&) = nullptr; }; -inline std::unordered_map* m_type_map = nullptr; - } // namespace pkbind diff --git a/include/pybind11/internal/types.h b/include/pybind11/internal/types.h index d37a5ebc..2ef49f25 100644 --- a/include/pybind11/internal/types.h +++ b/include/pybind11/internal/types.h @@ -29,6 +29,8 @@ public: using object ::object; using object ::operator=; + inline static std::unordered_map m_type_map; + // note: type is global instance, so we use ref_t. explicit type(py_Type type) : object(py_tpobject(type), ref_t{}) {} diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index fe5565de..5bb56ef4 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -19,8 +19,6 @@ inline void initialize(int object_pool_size = 1024) { // initialize object pool. object_pool::initialize(object_pool_size); - m_type_map = new std::unordered_map(); - action::initialize(); initialized = true; } @@ -28,10 +26,9 @@ inline void initialize(int object_pool_size = 1024) { /// finalize the vm. inline void finalize(bool test = false) { if(!initialized) { return; } - delete m_type_map; - m_type_map = nullptr; object_pool::finalize(); if(test) { + type::m_type_map.clear(); capsule::tp_capsule.reset(); cpp_function::tp_function_record.reset(); py_resetvm();