@@ -85,7 +85,9 @@ class object_api : public pyobject_tag {
85
85
or `object` subclass causes a call to ``__setitem__``.
86
86
\endrst */
87
87
item_accessor operator [](handle key) const ;
88
- // / See above (the only difference is that they key is provided as a string literal)
88
+ // / See above (the only difference is that the key's reference is stolen)
89
+ item_accessor operator [](object &&key) const ;
90
+ // / See above (the only difference is that the key is provided as a string literal)
89
91
item_accessor operator [](const char *key) const ;
90
92
91
93
/* * \rst
@@ -95,7 +97,9 @@ class object_api : public pyobject_tag {
95
97
or `object` subclass causes a call to ``setattr``.
96
98
\endrst */
97
99
obj_attr_accessor attr (handle key) const ;
98
- // / See above (the only difference is that they key is provided as a string literal)
100
+ // / See above (the only difference is that the key's reference is stolen)
101
+ obj_attr_accessor attr (object &&key) const ;
102
+ // / See above (the only difference is that the key is provided as a string literal)
99
103
str_attr_accessor attr (const char *key) const ;
100
104
101
105
/* * \rst
@@ -684,7 +688,7 @@ class accessor : public object_api<accessor<Policy>> {
684
688
}
685
689
template <typename T>
686
690
void operator =(T &&value) & {
687
- get_cache () = reinterpret_borrow<object> (object_or_cast (std::forward<T>(value)));
691
+ get_cache () = ensure_object (object_or_cast (std::forward<T>(value)));
688
692
}
689
693
690
694
template <typename T = Policy>
@@ -712,6 +716,9 @@ class accessor : public object_api<accessor<Policy>> {
712
716
}
713
717
714
718
private:
719
+ static object ensure_object (object &&o) { return std::move (o); }
720
+ static object ensure_object (handle h) { return reinterpret_borrow<object>(h); }
721
+
715
722
object &get_cache () const {
716
723
if (!cache) {
717
724
cache = Policy::get (obj, key);
@@ -1711,7 +1718,10 @@ class tuple : public object {
1711
1718
size_t size () const { return (size_t ) PyTuple_Size (m_ptr); }
1712
1719
bool empty () const { return size () == 0 ; }
1713
1720
detail::tuple_accessor operator [](size_t index) const { return {*this , index }; }
1714
- detail::item_accessor operator [](handle h) const { return object::operator [](h); }
1721
+ template <typename T, detail::enable_if_t <detail::is_pyobject<T>::value, int > = 0 >
1722
+ detail::item_accessor operator [](T &&o) const {
1723
+ return object::operator [](std::forward<T>(o));
1724
+ }
1715
1725
detail::tuple_iterator begin () const { return {*this , 0 }; }
1716
1726
detail::tuple_iterator end () const { return {*this , PyTuple_GET_SIZE (m_ptr)}; }
1717
1727
};
@@ -1771,7 +1781,10 @@ class sequence : public object {
1771
1781
}
1772
1782
bool empty () const { return size () == 0 ; }
1773
1783
detail::sequence_accessor operator [](size_t index) const { return {*this , index }; }
1774
- detail::item_accessor operator [](handle h) const { return object::operator [](h); }
1784
+ template <typename T, detail::enable_if_t <detail::is_pyobject<T>::value, int > = 0 >
1785
+ detail::item_accessor operator [](T &&o) const {
1786
+ return object::operator [](std::forward<T>(o));
1787
+ }
1775
1788
detail::sequence_iterator begin () const { return {*this , 0 }; }
1776
1789
detail::sequence_iterator end () const { return {*this , PySequence_Size (m_ptr)}; }
1777
1790
};
@@ -1790,7 +1803,10 @@ class list : public object {
1790
1803
size_t size () const { return (size_t ) PyList_Size (m_ptr); }
1791
1804
bool empty () const { return size () == 0 ; }
1792
1805
detail::list_accessor operator [](size_t index) const { return {*this , index }; }
1793
- detail::item_accessor operator [](handle h) const { return object::operator [](h); }
1806
+ template <typename T, detail::enable_if_t <detail::is_pyobject<T>::value, int > = 0 >
1807
+ detail::item_accessor operator [](T &&o) const {
1808
+ return object::operator [](std::forward<T>(o));
1809
+ }
1794
1810
detail::list_iterator begin () const { return {*this , 0 }; }
1795
1811
detail::list_iterator end () const { return {*this , PyList_GET_SIZE (m_ptr)}; }
1796
1812
template <typename T>
@@ -2090,6 +2106,10 @@ item_accessor object_api<D>::operator[](handle key) const {
2090
2106
return {derived (), reinterpret_borrow<object>(key)};
2091
2107
}
2092
2108
template <typename D>
2109
+ item_accessor object_api<D>::operator [](object &&key) const {
2110
+ return {derived (), std::move (key)};
2111
+ }
2112
+ template <typename D>
2093
2113
item_accessor object_api<D>::operator [](const char *key) const {
2094
2114
return {derived (), pybind11::str (key)};
2095
2115
}
@@ -2098,6 +2118,10 @@ obj_attr_accessor object_api<D>::attr(handle key) const {
2098
2118
return {derived (), reinterpret_borrow<object>(key)};
2099
2119
}
2100
2120
template <typename D>
2121
+ obj_attr_accessor object_api<D>::attr(object &&key) const {
2122
+ return {derived (), std::move (key)};
2123
+ }
2124
+ template <typename D>
2101
2125
str_attr_accessor object_api<D>::attr(const char *key) const {
2102
2126
return {derived (), key};
2103
2127
}
0 commit comments