Uh oh!
There was an error while loading. Please reload this page.
Expose enum_ entries as new "__members__" attribute - #666
Conversation
jagerman
commented
Feb 14, 2017
I didn't look at this in great detail, but a couple comments:
|
I eventually chose |
| void>::value; | ||
| auto entries = new std::unordered_map<Scalar, const char *>(); | ||
| auto entries = new pybind11::dict(); |
There was a problem hiding this comment.
use of new here is problematic (this will never be cleaned up.) Better to use standard python objects and rely on reference counting.
| /// Export enumeration entries into the parent scope | ||
| enum_ &export_values() { | ||
| #if !defined(PYPY_VERSION) |
I'm a proponent of keeping it as close to the PEP 435 enums as possible, i.e. with |
wjakob
commented
Feb 27, 2017
We always support 2.7 :) |
Lastest push fixes conflict issue and use gives: Looking into that and the |
llchan
commented
Feb 27, 2017
llchan
commented
Feb 27, 2017
Btw, the def__iter__(cls):
return (cls._member_map_[name] fornameincls._member_names_)We could probably do the equivalent with Also I think we should follow their lead and make @propertydef__members__(cls):
returncls._member_map_.copy() |
Latest push should address the 'new' issue, readonly property, copy on access. Looked briefly at the |
jagerman
commented
Mar 2, 2017
travis-ci is having some technical problems tonight, so you'll probably have to wait a bit to get a successful build. |
| 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.
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 (constauto &kv : reinterpret_borrow<dict>(m_entries_ptr)) {
if (pybind11::cast<Type>(kv.second) == value)
returnstr("{}.{}").format(name, kv.first);
}
returnstr("{}.???").format(name);
});jagerman
commented
Mar 3, 2017
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 |
llchan
commented
Mar 3, 2017
Oh derp, you're totally right, I just Ctrl-F'd the |
@jagerman - squashed your |
jagerman
commented
Mar 3, 2017
Assuming the WIP build succeeds, no need to squash (I can do that when merging). |
mdcb
commented
Mar 3, 2017
Ok - note ther typo in docs. |
jagerman
commented
Mar 3, 2017
Ah, well that needs a commit to fix it. |
mdcb
commented
Mar 3, 2017
shake and bake |
jagerman
commented
Mar 3, 2017
Merged, thanks! |
| 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.
unrelated to this PR but highlighted by the diff, lines 1145 / 1146 look odd.
jagerman
commented
Mar 4, 2017
Can you clarify "odd"? |
jagerman
commented
Mar 4, 2017
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.