Skip to content
Open
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
20 changes: 5 additions & 15 deletions include/pybind11/pytypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,8 @@ class object_api : public pyobject_tag {
subclass causes a corresponding call to ``__getitem__``. Assigning a `handle`
or `object` subclass causes a call to ``__setitem__``.
\endrst */
item_accessor operator[](handle key) const;
/// See above (the only difference is that the key's reference is stolen)
item_accessor operator[](object &&key) const;
/// See above (the only difference is that the key is provided as a string literal)
item_accessor operator[](const char *key) const;
template <typename T>
item_accessor operator[](T &&key) const;

/** \rst
Return an internal functor to access the object's attributes. Casting the
Expand Down Expand Up @@ -2270,16 +2267,9 @@ iterator object_api<D>::end() const {
return iterator::sentinel();
}
template <typename D>
item_accessor object_api<D>::operator[](handle key) const {
return {derived(), reinterpret_borrow<object>(key)};
}
template <typename D>
item_accessor object_api<D>::operator[](object &&key) const {
return {derived(), std::move(key)};
}
template <typename D>
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...

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.

}
template <typename D>
obj_attr_accessor object_api<D>::attr(handle key) const {
Expand Down
4 changes: 4 additions & 0 deletions tests/test_pytypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ TEST_SUBMODULE(pytypes, m) {
m.def("tuple_ssize_t", []() { return py::tuple{(py::ssize_t) 0}; });
m.def("tuple_size_t", []() { return py::tuple{(py::size_t) 0}; });
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...

});

// test_simple_namespace
m.def("get_simple_namespace", []() {
Expand Down
1 change: 1 addition & 0 deletions tests/test_pytypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ def test_tuple():
assert m.tuple_ssize_t() == ()
assert m.tuple_size_t() == ()
assert m.get_tuple() == (42, None, "spam")
# assert m.access_tuple_with_int_index() == (2)


def test_simple_namespace():
Expand Down