Skip to content

Support arbitrary type in item accessor's subscript operator - #4004

Open
woodychow wants to merge 5 commits into
pybind:masterfrom
woodychow:item_accessor_T_2
Open

woodychow wants to merge 5 commits into
pybind:masterfrom
woodychow:item_accessor_T_2

Conversation

@woodychow

Copy link
Copy Markdown

Resolves #1309

PR made at cielavenir's request.

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 {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additionally, this PR should include tests to demonstrate the new behavior.

@cielavenir cielavenir Jun 15, 2022 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

interestingly, cielavenir@c59a10d works locally on both pypy2 and pypy3.

[edit] actually we need both single dimension case and multidimension cases...

Comment thread include/pybind11/pytypes.h Resolved
Comment thread tests/test_pytypes.cpp
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];

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

int is implicitly converted to size_t first, I believe so it uses the tuple_accessor, not the item_accessor.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay so it looks this test case itself is bugged and makes pypy complain.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@woodychow Would you please add a dict case that actually tests the broken behavior? Like the dict example given in the issue?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Skylion007 for this particular case py::tuple could be used but what about multi-dimensional tuple/list?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually we need both single dimension case and multidimension cases...

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dict test cielavenir@fd82f0a

somehow this one works fine, why tuple is broken like this...

@Skylion007

Skylion007 commented Jun 14, 2022 •

Copy link
Copy Markdown
Collaborator

@woodychow Seems this change breaks some tests in pypy for some reason.

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>()};

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Skylion007 sorry to bother you but is this "syntactic sugar" of @woodychow 's original code or does your code have improvement?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cielavenir It prevents extra reference counting if it's already a pybind11 object. We have tests or this which run in debug mode.

@cielavenir

cielavenir commented Jun 15, 2022 •

Copy link
Copy Markdown
Contributor

[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 ).

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implicit conversion of ints (and char const*) to handles?

3 participants