@@ -88,7 +88,9 @@ class object_api : public pyobject_tag {
88
88
or `object` subclass causes a call to ``__setitem__``.
89
89
\endrst */
90
90
item_accessor operator [](handle key) const ;
91
- // / See above (the only difference is that they key is provided as a string literal)
91
+ // / See above (the only difference is that the key's reference is stolen)
92
+ item_accessor operator [](object &&key) const ;
93
+ // / See above (the only difference is that the key is provided as a string literal)
92
94
item_accessor operator [](const char *key) const ;
93
95
94
96
/* * \rst
@@ -98,7 +100,9 @@ class object_api : public pyobject_tag {
98
100
or `object` subclass causes a call to ``setattr``.
99
101
\endrst */
100
102
obj_attr_accessor attr (handle key) const ;
101
- // / See above (the only difference is that they key is provided as a string literal)
103
+ // / See above (the only difference is that the key's reference is stolen)
104
+ obj_attr_accessor attr (object &&key) const ;
105
+ // / See above (the only difference is that the key is provided as a string literal)
102
106
str_attr_accessor attr (const char *key) const ;
103
107
104
108
/* * \rst
@@ -863,7 +867,7 @@ class accessor : public object_api<accessor<Policy>> {
863
867
}
864
868
template <typename T>
865
869
void operator =(T &&value) & {
866
- get_cache () = reinterpret_borrow<object> (object_or_cast (std::forward<T>(value)));
870
+ get_cache () = ensure_object (object_or_cast (std::forward<T>(value)));
867
871
}
868
872
869
873
template <typename T = Policy>
@@ -891,6 +895,9 @@ class accessor : public object_api<accessor<Policy>> {
891
895
}
892
896
893
897
private:
898
+ static object ensure_object (object &&o) { return std::move (o); }
899
+ static object ensure_object (handle h) { return reinterpret_borrow<object>(h); }
900
+
894
901
object &get_cache () const {
895
902
if (!cache) {
896
903
cache = Policy::get (obj, key);
@@ -1890,7 +1897,10 @@ class tuple : public object {
1890
1897
size_t size () const { return (size_t ) PyTuple_Size (m_ptr); }
1891
1898
bool empty () const { return size () == 0 ; }
1892
1899
detail::tuple_accessor operator [](size_t index) const { return {*this , index}; }
1893
- detail::item_accessor operator [](handle h) const { return object::operator [](h); }
1900
+ template <typename T, detail::enable_if_t <detail::is_pyobject<T>::value, int > = 0 >
1901
+ detail::item_accessor operator [](T &&o) const {
1902
+ return object::operator [](std::forward<T>(o));
1903
+ }
1894
1904
detail::tuple_iterator begin () const { return {*this , 0 }; }
1895
1905
detail::tuple_iterator end () const { return {*this , PyTuple_GET_SIZE (m_ptr)}; }
1896
1906
};
@@ -1950,7 +1960,10 @@ class sequence : public object {
1950
1960
}
1951
1961
bool empty () const { return size () == 0 ; }
1952
1962
detail::sequence_accessor operator [](size_t index) const { return {*this , index}; }
1953
- detail::item_accessor operator [](handle h) const { return object::operator [](h); }
1963
+ template <typename T, detail::enable_if_t <detail::is_pyobject<T>::value, int > = 0 >
1964
+ detail::item_accessor operator [](T &&o) const {
1965
+ return object::operator [](std::forward<T>(o));
1966
+ }
1954
1967
detail::sequence_iterator begin () const { return {*this , 0 }; }
1955
1968
detail::sequence_iterator end () const { return {*this , PySequence_Size (m_ptr)}; }
1956
1969
};
@@ -1969,7 +1982,10 @@ class list : public object {
1969
1982
size_t size () const { return (size_t ) PyList_Size (m_ptr); }
1970
1983
bool empty () const { return size () == 0 ; }
1971
1984
detail::list_accessor operator [](size_t index) const { return {*this , index}; }
1972
- detail::item_accessor operator [](handle h) const { return object::operator [](h); }
1985
+ template <typename T, detail::enable_if_t <detail::is_pyobject<T>::value, int > = 0 >
1986
+ detail::item_accessor operator [](T &&o) const {
1987
+ return object::operator [](std::forward<T>(o));
1988
+ }
1973
1989
detail::list_iterator begin () const { return {*this , 0 }; }
1974
1990
detail::list_iterator end () const { return {*this , PyList_GET_SIZE (m_ptr)}; }
1975
1991
template <typename T>
@@ -2269,6 +2285,10 @@ item_accessor object_api<D>::operator[](handle key) const {
2269
2285
return {derived (), reinterpret_borrow<object>(key)};
2270
2286
}
2271
2287
template <typename D>
2288
+ item_accessor object_api<D>::operator [](object &&key) const {
2289
+ return {derived (), std::move (key)};
2290
+ }
2291
+ template <typename D>
2272
2292
item_accessor object_api<D>::operator [](const char *key) const {
2273
2293
return {derived (), pybind11::str (key)};
2274
2294
}
@@ -2277,6 +2297,10 @@ obj_attr_accessor object_api<D>::attr(handle key) const {
2277
2297
return {derived (), reinterpret_borrow<object>(key)};
2278
2298
}
2279
2299
template <typename D>
2300
+ obj_attr_accessor object_api<D>::attr(object &&key) const {
2301
+ return {derived (), std::move (key)};
2302
+ }
2303
+ template <typename D>
2280
2304
str_attr_accessor object_api<D>::attr(const char *key) const {
2281
2305
return {derived (), key};
2282
2306
}
0 commit comments