Skip to content
Draft
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
10 changes: 0 additions & 10 deletions include/pybind11/cast.h
Original file line number Diff line number Diff line change
Expand Up @@ -217,16 +217,6 @@ public:
template <typename T_> \
using cast_op_type = ::pybind11::detail::movable_cast_op_type<T_>

template <typename CharT>
using is_std_char_type = any_of<std::is_same<CharT, char>, /* std::string */
#if defined(PYBIND11_HAS_U8STRING)
std::is_same<CharT, char8_t>, /* std::u8string */
#endif
std::is_same<CharT, char16_t>, /* std::u16string */
std::is_same<CharT, char32_t>, /* std::u32string */
std::is_same<CharT, wchar_t> /* std::wstring */
>;

template <typename T>
struct type_caster<T, enable_if_t<std::is_arithmetic<T>::value && !is_std_char_type<T>::value>> {
using _py_type_0 = conditional_t<sizeof(T) <= sizeof(long), long, long long>;
Expand Down
10 changes: 10 additions & 0 deletions include/pybind11/detail/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -1043,6 +1043,16 @@ using any_of = std::disjunction<Ts...>;
template <class... Ts>
using none_of = negation<any_of<Ts...>>;

template <typename CharT>
using is_std_char_type = any_of<std::is_same<CharT, char>, /* std::string */
#if defined(PYBIND11_HAS_U8STRING)
std::is_same<CharT, char8_t>, /* std::u8string */
#endif
std::is_same<CharT, char16_t>, /* std::u16string */
std::is_same<CharT, char32_t>, /* std::u32string */
std::is_same<CharT, wchar_t> /* std::wstring */
>;

template <class T, template <class> class... Predicates>
using satisfies_all_of = all_of<Predicates<T>...>;
template <class T, template <class> class... Predicates>
Expand Down
13 changes: 13 additions & 0 deletions include/pybind11/pytypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ template <>
struct is_pyobject<const PyObject> : std::false_type {};
#endif

// bool and character types are integral, but `cast` does not convert them to a Python int.
template <typename T>
using is_integer_key
= all_of<std::is_integral<T>, none_of<std::is_same<T, bool>, is_std_char_type<T>>>;

/** \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``.
Expand Down Expand Up @@ -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 <typename T, enable_if_t<is_integer_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 @@ -2546,6 +2554,11 @@ 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 D>
obj_attr_accessor object_api<D>::attr(handle key) const {
return {derived(), reinterpret_borrow<object>(key)};
}
Expand Down
14 changes: 14 additions & 0 deletions tests/test_pytypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
11 changes: 11 additions & 0 deletions tests/test_pytypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down