diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index 79e743d9bb..c60f8fb3ec 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -217,16 +217,6 @@ public: template \ using cast_op_type = ::pybind11::detail::movable_cast_op_type -template -using is_std_char_type = any_of, /* std::string */ -#if defined(PYBIND11_HAS_U8STRING) - std::is_same, /* std::u8string */ -#endif - std::is_same, /* std::u16string */ - std::is_same, /* std::u32string */ - std::is_same /* std::wstring */ - >; - template struct type_caster::value && !is_std_char_type::value>> { using _py_type_0 = conditional_t; diff --git a/include/pybind11/detail/common.h b/include/pybind11/detail/common.h index b35147add7..782462af11 100644 --- a/include/pybind11/detail/common.h +++ b/include/pybind11/detail/common.h @@ -1043,6 +1043,16 @@ using any_of = std::disjunction; template using none_of = negation>; +template +using is_std_char_type = any_of, /* std::string */ +#if defined(PYBIND11_HAS_U8STRING) + std::is_same, /* std::u8string */ +#endif + std::is_same, /* std::u16string */ + std::is_same, /* std::u32string */ + std::is_same /* std::wstring */ + >; + template class... Predicates> using satisfies_all_of = all_of...>; template class... Predicates> diff --git a/include/pybind11/pytypes.h b/include/pybind11/pytypes.h index adf512cc23..48ea92e6f2 100644 --- a/include/pybind11/pytypes.h +++ b/include/pybind11/pytypes.h @@ -83,6 +83,11 @@ template <> struct is_pyobject : std::false_type {}; #endif +// bool and character types are integral, but `cast` does not convert them to a Python int. +template +using is_integer_key + = all_of, none_of, is_std_char_type>>; + /** \rst A mixin class which adds common functions to `handle`, `object` and various accessors. The only requirement for `Derived` is to implement ``PyObject *Derived::ptr() const``. @@ -113,6 +118,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; bool and character types are excluded) + template ::value, int> = 0> + item_accessor operator[](T key) const; /** \rst Return an internal functor to access the object's attributes. Casting the @@ -2546,6 +2554,11 @@ item_accessor object_api::operator[](const char *key) const { return {derived(), pybind11::str(key)}; } template +template ::value, int>> +item_accessor object_api::operator[](T key) const { + return {derived(), pybind11::int_(key)}; +} +template obj_attr_accessor object_api::attr(handle key) const { return {derived(), reinterpret_borrow(key)}; } diff --git a/tests/test_pytypes.cpp b/tests/test_pytypes.cpp index afa344536f..2287559c0d 100644 --- a/tests/test_pytypes.cpp +++ b/tests/test_pytypes.cpp @@ -448,6 +448,20 @@ TEST_SUBMODULE(pytypes, m) { return d; }); + // Issue #1309: integer keys + m.def("accessor_int_key", [](const py::object &o) { + py::object d = py::dict(); + d[1] = "int"; + d[0] = "zero"; + d[-1] = "negative"; + d[std::size_t{2}] = "size_t"; + d["read"] = py::object(d[1]); + d["nested"] = py::object(o[1][2]); + d["via_accessor"] = py::object(o.attr("__getitem__")(1)[0]); + d["list_object_key"] = py::object(py::list(o)[py::int_(0)][1]); + return d; + }); + m.def("tuple_accessor", [](const py::tuple &existing_t) { try { existing_t[0] = 1; diff --git a/tests/test_pytypes.py b/tests/test_pytypes.py index 7e419b98e0..1e614b6627 100644 --- a/tests/test_pytypes.py +++ b/tests/test_pytypes.py @@ -434,6 +434,17 @@ 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))) == { + 1: "int", + 0: "zero", + -1: "negative", + 2: "size_t", + "read": "int", + "nested": 6, + "via_accessor": 4, + "list_object_key": 2, + } + assert m.tuple_accessor(()) == (0, 1, 2) d = m.accessor_assignment()