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
17 changes: 17 additions & 0 deletions include/pybind11/pytypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@ 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>>>;

#ifdef PYBIND11_HAS_STRING_VIEW
template <typename T>
using is_string_key = any_of<std::is_same<T, std::string>, std::is_same<T, std::string_view>>;
#else
template <typename T>
using is_string_key = std::is_same<T, std::string>;
#endif

/** \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 @@ -124,6 +132,10 @@ class object_api : public pyobject_tag {
/// 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 a ``std::string`` or ``std::string_view``). A template, so that
/// types with an implicit conversion to these do not make the other overloads ambiguous.
template <typename T, enable_if_t<is_string_key<T>::value, int> = 0>
item_accessor operator[](const T &key) const;

/** \rst
Return an internal functor to access the object's attributes. Casting the
Expand Down Expand Up @@ -2609,6 +2621,11 @@ item_accessor object_api<D>::operator[](T key) const {
return {derived(), pybind11::int_(key)};
}
template <typename D>
template <typename T, enable_if_t<is_string_key<T>::value, int>>
item_accessor object_api<D>::operator[](const T &key) const {
return {derived(), pybind11::str(key)};
}
template <typename D>
obj_attr_accessor object_api<D>::attr(handle key) const {
return {derived(), reinterpret_borrow<object>(key)};
}
Expand Down
23 changes: 23 additions & 0 deletions tests/test_pytypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,29 @@ TEST_SUBMODULE(pytypes, m) {
return d;
});

m.def("accessor_string_key", [](const py::object &o) {
py::dict d;
const std::string key = "const";
d[key] = "lvalue";
d[std::string("temp")] = "rvalue";
d[std::string("nul\0key", 7)] = "embedded";
d["read"] = py::object(d[key]);
d["nested"] = py::object(o[key][std::string("inner")]);
return d;
});

#ifdef PYBIND11_HAS_STRING_VIEW
m.def("accessor_string_view_key", [](const py::object &o) {
py::dict d;
const std::string_view key = "view";
d[key] = "lvalue";
d[std::string_view("nul\0key", 7)] = "embedded";
d["read"] = py::object(d[key]);
d["nested"] = py::object(o[key][std::string_view("inner")]);
return d;
});
#endif

m.def("tuple_accessor", [](const py::tuple &existing_t) {
try {
existing_t[0] = 1;
Expand Down
16 changes: 16 additions & 0 deletions tests/test_pytypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,22 @@ def func(self, x, *args):
"list_object_key": 2,
}

assert m.accessor_string_key({"const": {"inner": 7}}) == {
"const": "lvalue",
"temp": "rvalue",
"nul\0key": "embedded",
"read": "lvalue",
"nested": 7,
}

if hasattr(m, "accessor_string_view_key"):
assert m.accessor_string_view_key({"view": {"inner": 7}}) == {
"view": "lvalue",
"nul\0key": "embedded",
"read": "lvalue",
"nested": 7,
}

assert m.tuple_accessor(()) == (0, 1, 2)

d = m.accessor_assignment()
Expand Down
Loading