Skip to content
Merged
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
16 changes: 16 additions & 0 deletions include/pybind11/pytypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@ 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>>>;

/** \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 +120,10 @@ 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;

/** \rst
Return an internal functor to access the object's attributes. Casting the
Expand Down Expand Up @@ -2576,6 +2587,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
19 changes: 19 additions & 0 deletions tests/test_pytypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,25 @@ TEST_SUBMODULE(pytypes, m) {
return d;
});

// Issue #1309: integer keys
#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_");
#endif
m.def("accessor_int_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["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 @@ -443,6 +443,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
Loading