Repository navigation
Support arbitrary type in item accessor's subscript operator #4004
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
3a74bc4
c3a313b
9ac9c07
fdc20a8
95ed8a6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
|
@@ -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 { | ||
| return {derived(), detail::object_or_cast(std::forward<T>(key)).template cast<object>()}; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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]; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this test would be more proper here this
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. actually we need both single dimension case and multidimension cases...
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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", []() { | ||
|
|
||
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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...