Repository navigation
Conversation
| item_accessor object_api<D>::operator[](const char *key) const { | ||
| return {derived(), pybind11::str(key)}; | ||
| template <typename T> | ||
| item_accessor object_api<D>::operator[](T &&key) const { |
There was a problem hiding this comment.
This actually will break quite a few things and cause some perf issues with a needless temporary. I don't quite get the motivation here either. What is wrong with the current overloads?
There was a problem hiding this comment.
Additionally, this PR should include tests to demonstrate the new behavior.
There was a problem hiding this comment.
interestingly, cielavenir@c59a10d works locally on both pypy2 and pypy3.
[edit] actually we need both single dimension case and multidimension cases...
| m.def("get_tuple", []() { return py::make_tuple(42, py::none(), "spam"); }); | ||
| m.def("access_tuple_with_int_index", []() { | ||
| py::object tpl = py::make_tuple(1, 2); | ||
| return tpl[1]; |
There was a problem hiding this comment.
int is implicitly converted to size_t first, I believe so it uses the tuple_accessor, not the item_accessor.
There was a problem hiding this comment.
Okay so it looks this test case itself is bugged and makes pypy complain.
There was a problem hiding this comment.
@woodychow Would you please add a dict case that actually tests the broken behavior? Like the dict example given in the issue?
There was a problem hiding this comment.
@Skylion007 for this particular case py::tuple could be used but what about multi-dimensional tuple/list?
There was a problem hiding this comment.
this test would be more proper here
m.def("access_tuple_with_int_index", []() {
py::object tpl = py::make_tuple(py::make_tuple(1, 2), py::make_tuple(3, 4));
return tpl[0][1];
});
this tpl[0][1] cannot be compiled on stock pybind11 even if tpl is declared as py::tuple.
There was a problem hiding this comment.
actually we need both single dimension case and multidimension cases...
There was a problem hiding this comment.
dict test cielavenir@fd82f0a
somehow this one works fine, why tuple is broken like this...
|
@woodychow Seems this change breaks some tests in pypy for some reason. |
for more information, see https://pre-commit.ci
| return {derived(), pybind11::str(key)}; | ||
| template <typename T> | ||
| item_accessor object_api<D>::operator[](T &&key) const { | ||
| return {derived(), detail::object_or_cast(std::forward<T>(key)).template cast<object>()}; |
There was a problem hiding this comment.
I dug around and found it that going from handle to object this way automatically does reinterpret_borrow so it's just some syntactic sugar in that regard.
There was a problem hiding this comment.
@Skylion007 sorry to bother you but is this "syntactic sugar" of @woodychow 's original code or does your code have improvement?
There was a problem hiding this comment.
@cielavenir It prevents extra reference counting if it's already a pybind11 object. We have tests or this which run in debug mode.
[edit] for py2 compatible version, https://github.com/cielavenir/pybind11/actions/workflows/ci.yml runs because the branch name starts from "v", and it seems roughly working ( https://github.com/cielavenir/pybind11/actions/runs/2500434793 ). |
Resolves #1309
PR made at cielavenir's request.