Skip to content
Open
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
38 changes: 25 additions & 13 deletions include/pybind11/pytypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,25 @@ template <>
struct is_pyobject<const PyObject> : std::false_type {};
#endif

// py::int_ converts through long long or unsigned long long. Exclude wider integers
// along with bool and character types that `cast` converts to Python strings.
template <typename T>
using is_integer_key = all_of<std::is_integral<T>,
bool_constant<(sizeof(T) <= sizeof(unsigned long long))>,
none_of<std::is_same<T, bool>, is_std_char_type<T>>>;
// `cast` converts integers through long long or unsigned long long, so reject wider ones.
template <typename U>
struct is_wide_integer
: all_of<std::is_integral<U>, bool_constant<(sizeof(U) > sizeof(unsigned long long))>> {};
#ifdef __SIZEOF_INT128__
// In strict -std=c++NN mode, some standard libraries do not report __int128 as integral.
__extension__ using int128_alias = __int128;
__extension__ using uint128_alias = unsigned __int128;
template <>
struct is_wide_integer<int128_alias> : std::true_type {};
template <>
struct is_wide_integer<uint128_alias> : std::true_type {};
#endif

template <typename T, typename U = remove_cvref_t<T>>
using is_cast_key = none_of<is_pyobject<U>,
std::is_convertible<T, handle>,
std::is_convertible<T, const char *>,
is_wide_integer<U>>;

/** \rst
A mixin class which adds common functions to `handle`, `object` and various accessors.
Expand Down Expand Up @@ -120,10 +133,9 @@ class object_api : public pyobject_tag {
item_accessor operator[](object &&key) const;
/// See above (the only difference is that the key is provided as a string literal)
item_accessor operator[](const char *key) const;
/// See above (the key is a C++ integer type no wider than unsigned long long).
/// Excludes bool and character types that cast to Python strings.
template <typename T, enable_if_t<is_integer_key<T>::value, int> = 0>
item_accessor operator[](T key) const;
/// See above (the key is any other C++ type; it is converted with `pybind11::cast`)
template <typename T, enable_if_t<is_cast_key<T>::value, int> = 0>
item_accessor operator[](T &&key) const;

/** \rst
Return an internal functor to access the object's attributes. Casting the
Expand Down Expand Up @@ -2604,9 +2616,9 @@ item_accessor object_api<D>::operator[](const char *key) const {
return {derived(), pybind11::str(key)};
}
template <typename D>
template <typename T, enable_if_t<is_integer_key<T>::value, int>>
item_accessor object_api<D>::operator[](T key) const {
return {derived(), pybind11::int_(key)};
template <typename T, enable_if_t<is_cast_key<T>::value, int>>
item_accessor object_api<D>::operator[](T &&key) const {
return {derived(), object_or_cast(std::forward<T>(key))};
}
template <typename D>
obj_attr_accessor object_api<D>::attr(handle key) const {
Expand Down
12 changes: 8 additions & 4 deletions tests/test_pytypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -463,18 +463,22 @@ TEST_SUBMODULE(pytypes, m) {
return d;
});

// Issue #1309: integer keys
// Issue #1309: any castable C++ type is a valid key
#ifdef __SIZEOF_INT128__
__extension__ using wide_integer = unsigned __int128;
static_assert(!py::detail::is_integer_key<wide_integer>::value,
"Wide integer keys would be truncated by py::int_");
static_assert(!py::detail::is_cast_key<wide_integer>::value,
"Wide integer keys would be truncated");
#endif
m.def("accessor_int_key", [](const py::object &o) {
m.def("accessor_key", [](const py::object &o) {
py::dict d;
d[1] = "int";
d[0] = "zero";
d[-1] = "negative";
d[std::size_t{2}] = "size_t";
d[2.5] = "float";
d[std::string("s")] = "string";
d['c'] = "char";
d[std::make_pair(1, 2)] = "pair";
d["read"] = py::object(d[1]);
d["nested"] = py::object(o[1][2]);
d["via_accessor"] = py::object(o.attr("__getitem__")(1)[0]);
Expand Down
6 changes: 5 additions & 1 deletion tests/test_pytypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -465,11 +465,15 @@ def func(self, x, *args):
assert d["implicit_list"] == [1, 2, 3]
assert all(x in TestObject.__dict__ for x in d["implicit_dict"])

assert m.accessor_int_key(((1, 2, 3), (4, 5, 6))) == {
assert m.accessor_key(((1, 2, 3), (4, 5, 6))) == {
1: "int",
0: "zero",
-1: "negative",
2: "size_t",
2.5: "float",
"s": "string",
"c": "char",
(1, 2): "pair",
"read": "int",
"nested": 6,
"via_accessor": 4,
Expand Down
Loading