Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix: ensure different KeysView/ValuesView type names for int16_t/int32_t or float/double. Fix #4529 #4972

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 2 additions & 12 deletions include/pybind11/stl_bind.h
Original file line number Diff line number Diff line change
Expand Up @@ -718,18 +718,8 @@ class_<Map, holder_type> bind_map(handle scope, const std::string &name, Args &&
}

Class_ cl(scope, name.c_str(), pybind11::module_local(local), std::forward<Args>(args)...);
static constexpr auto key_type_descr = detail::make_caster<KeyType>::name;
static constexpr auto mapped_type_descr = detail::make_caster<MappedType>::name;
std::string key_type_name(key_type_descr.text), mapped_type_name(mapped_type_descr.text);

// If key type isn't properly wrapped, fall back to C++ names
if (key_type_name == "%") {
key_type_name = detail::type_info_description(typeid(KeyType));
}
// Similarly for value type:
if (mapped_type_name == "%") {
mapped_type_name = detail::type_info_description(typeid(MappedType));
}
std::string key_type_name(detail::type_info_description(typeid(KeyType)));
std::string mapped_type_name(detail::type_info_description(typeid(MappedType)));

// Wrap KeysView[KeyType] if it wasn't already wrapped
if (!detail::get_type_info(typeid(KeysView))) {
Expand Down
13 changes: 13 additions & 0 deletions tests/test_stl_binders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,19 @@ TEST_SUBMODULE(stl_binders, m) {
py::bind_map<std::unordered_map<std::string, double const>>(m,
"UnorderedMapStringDoubleConst");

// test_map_view_types
py::bind_map<std::map<int, double>>(m, "MapIntDouble");
py::bind_map<std::unordered_map<int, double>>(m, "UnorderedMapIntDouble");

py::bind_map<std::map<int, double const>>(m, "MapIntDoubleConst");
py::bind_map<std::unordered_map<int, double const>>(m, "UnorderedMapIntDoubleConst");

py::bind_map<std::map<int, float>>(m, "MapIntFloat");
py::bind_map<std::unordered_map<int, float>>(m, "UnorderedMapIntFloat");

py::bind_map<std::map<int64_t, double>>(m, "MapInt64Double");
py::bind_map<std::map<uint64_t, double>>(m, "MapUInt64Double");

py::class_<E_nc>(m, "ENC").def(py::init<int>()).def_readwrite("value", &E_nc::value);

// test_noncopyable_containers
Expand Down
84 changes: 61 additions & 23 deletions tests/test_stl_binders.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,29 +312,67 @@ def test_map_delitem():


def test_map_view_types():
map_string_double = m.MapStringDouble()
unordered_map_string_double = m.UnorderedMapStringDouble()
map_string_double_const = m.MapStringDoubleConst()
unordered_map_string_double_const = m.UnorderedMapStringDoubleConst()

assert map_string_double.keys().__class__.__name__ == "KeysView[str]"
assert map_string_double.values().__class__.__name__ == "ValuesView[float]"
assert map_string_double.items().__class__.__name__ == "ItemsView[str, float]"

keys_type = type(map_string_double.keys())
assert type(unordered_map_string_double.keys()) is keys_type
assert type(map_string_double_const.keys()) is keys_type
assert type(unordered_map_string_double_const.keys()) is keys_type

values_type = type(map_string_double.values())
assert type(unordered_map_string_double.values()) is values_type
assert type(map_string_double_const.values()) is values_type
assert type(unordered_map_string_double_const.values()) is values_type

items_type = type(map_string_double.items())
assert type(unordered_map_string_double.items()) is items_type
assert type(map_string_double_const.items()) is items_type
assert type(unordered_map_string_double_const.items()) is items_type
map_int_double = m.MapIntDouble()
unordered_map_int_double = m.UnorderedMapIntDouble()
map_int_double_const = m.MapIntDoubleConst()
unordered_map_int_double_const = m.UnorderedMapIntDoubleConst()
map_int_float = m.MapIntFloat()
unordered_map_int_float = m.UnorderedMapIntFloat()
map_int64_double = m.MapInt64Double()
map_uint64_double = m.MapUInt64Double()

assert map_int_double.keys().__class__.__name__ == "KeysView[int]"
assert map_int_double.values().__class__.__name__ == "ValuesView[double]"
assert map_int_double.items().__class__.__name__ == "ItemsView[int, double]"

assert map_int_float.keys().__class__.__name__ == "KeysView[int]"
assert map_int_float.values().__class__.__name__ == "ValuesView[float]"
assert map_int_float.items().__class__.__name__ == "ItemsView[int, float]"

assert map_int64_double.keys().__class__.__name__ in [
"KeysView[long]",
"KeysView[long long]",
"KeysView[__int64]",
]
assert map_int64_double.values().__class__.__name__ == "ValuesView[double]"
assert map_int64_double.items().__class__.__name__ in [
"ItemsView[long, double]",
"ItemsView[long long, double]",
"ItemsView[__int64, double]",
]

assert map_uint64_double.keys().__class__.__name__ in [
"KeysView[unsigned long]",
"KeysView[unsigned long long]",
"KeysView[unsigned __int64]",
]
assert map_uint64_double.values().__class__.__name__ == "ValuesView[double]"
assert map_uint64_double.items().__class__.__name__ in [
"ItemsView[unsigned long, double]",
"ItemsView[unsigned long long, double]",
"ItemsView[unsigned __int64, double]",
]

keys_type = type(map_int_double.keys())
assert type(unordered_map_int_double.keys()) is keys_type
assert type(map_int_float.keys()) is keys_type
assert type(unordered_map_int_float.keys()) is keys_type
assert type(map_int_double_const.keys()) is keys_type
assert type(unordered_map_int_double_const.keys()) is keys_type

values_type_double = type(map_int_double.values())
assert type(unordered_map_int_double.values()) is values_type_double
assert type(map_int_double_const.values()) is values_type_double
assert type(unordered_map_int_double_const.values()) is values_type_double

values_type_float = type(map_int_float.values())
assert values_type_float is not values_type_double
assert type(unordered_map_int_float.values()) is values_type_float

items_type = type(map_int_double.items())
assert type(unordered_map_int_double.items()) is items_type
assert type(map_int_double_const.items()) is items_type
assert type(unordered_map_int_double_const.items()) is items_type


def test_recursive_vector():
Expand Down