-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expose enum_ entries as new "__members__" attribute #666
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
Conversation
I didn't look at this in great detail, but a couple comments:
|
I eventually chose |
include/pybind11/pybind11.h
Outdated
@@ -1407,12 +1407,13 @@ template <typename Type> class enum_ : public class_<Type> { | |||
!std::is_same<detail::first_of_t<arithmetic_tag, void, Extra...>, | |||
void>::value; | |||
|
|||
auto entries = new std::unordered_map<Scalar, const char *>(); | |||
auto entries = new pybind11::dict(); |
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.
use of new
here is problematic (this will never be cleaned up.) Better to use standard python objects and rely on reference counting.
include/pybind11/pybind11.h
Outdated
} | ||
|
||
/// Export enumeration entries into the parent scope | ||
enum_ &export_values() { | ||
#if !defined(PYPY_VERSION) |
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.
neat! That's a nice simplification.
I'm a proponent of keeping it as close to the PEP 435 enums as possible, i.e. with |
We always support 2.7 :) |
Lastest push fixes conflict issue and use
gives:
Looking into that and the |
Btw, the def __iter__(cls):
return (cls._member_map_[name] for name in cls._member_names_) We could probably do the equivalent with Also I think we should follow their lead and make @property
def __members__(cls):
return cls._member_map_.copy() |
Latest push should address the 'new' issue, readonly property, copy on access. Looked briefly at the |
travis-ci is having some technical problems tonight, so you'll probably have to wait a bit to get a successful build. |
include/pybind11/pybind11.h
Outdated
return std::string(name) + "." + kv.first.PREFIX_MEMBER_TEMPLATE cast<std::string>(); | ||
} | ||
return std::string(name) + ".???"; | ||
#undef PREFIX_MEMBER_TEMPLATE |
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.
I think you can simply this whole block (and get rid of the ugly MSVC workaround macro) as:
def("__repr__", [name, m_entries_ptr](Type value) -> str {
for (const auto &kv : reinterpret_borrow<dict>(m_entries_ptr)) {
if (pybind11::cast<Type>(kv.second) == value)
return str("{}.{}").format(name, kv.first);
}
return str("{}.???").format(name);
});
Re: iterator interface: I don't think it is worthwhile. The main issue is that the iterator interface has to be added on the metatype, not the type itself (because iteration is on Aside from the |
Oh derp, you're totally right, I just Ctrl-F'd the |
@jagerman - squashed your |
Assuming the WIP build succeeds, no need to squash (I can do that when merging). |
Ok - note ther typo in docs. |
Ah, well that needs a commit to fix it. |
shake and bake |
Merged, thanks! |
for (const auto &kv : reinterpret_borrow<dict>(m_entries_ptr)) | ||
m[kv.first] = kv.second; | ||
return m; | ||
}, return_value_policy::copy); | ||
def("__init__", [](Type& value, Scalar i) { value = (Type)i; }); | ||
def("__init__", [](Type& value, Scalar i) { new (&value) Type((Type) i); }); |
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.
unrelated to this PR but highlighted by the diff, lines 1145 / 1146 look odd.
Can you clarify "odd"? |
Oh, you mean two identical constructors declared. Yes, that does look strange. |
Brings convenience of being able to list the entries making an Enum. Side effect simplifies the .export_values() implementation.