Skip to content

Add enum value to enum repr #2126

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

Merged
merged 1 commit into from
Sep 19, 2020
Merged

Conversation

auscompgeek
Copy link
Contributor

@auscompgeek auscompgeek commented Mar 1, 2020

This changes enum reprs to look like <Enum.name: value> similarly to
the Python enum module.

This keeps the str of enums as Enum.name, like the Python enum module.

Ref: #2332

Copy link
Collaborator

@YannickJadoul YannickJadoul left a comment

Choose a reason for hiding this comment

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

Looks good to me! :-)

@@ -1435,29 +1445,22 @@ struct enum_base {
auto static_property = handle((PyObject *) get_internals().static_property_type);

m_base.attr("__repr__") = cpp_function(
[](handle arg) -> str {
[](object arg) -> str {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why handle to object? You don't need reference counting here, do you?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Argh, good catch. I should've look more closely at the details rather than eyeballing the structural changes and shrugging "OK"...

Copy link
Collaborator

Choose a reason for hiding this comment

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

OK, so I just tried this out, and the reason seems to be because there's no int_ conversion from handle (you can reinterpret_steal/reinterpret_borrow, but those will not do the int(...) conversion).

Copy link
Collaborator

Choose a reason for hiding this comment

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

See e.g. __getstate__ and __hash__, also doing that. I don't immediately see a better solution, then? (though if feels there ought to be one, changing the interface of the Python types; but that's a different discussion/PR)

Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe pass the object by reference, to avoid reference counting?

Copy link
Collaborator

Choose a reason for hiding this comment

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

@bstaletic Hmmm, interesting.

Thinking about it a bit futher: will it, though? I believe (though the cast, make_caster, cast_op, PYBIND11_TYPE_CASTER, ... code is quite convoluted) that the object will be created and moved from the caster anyway, if you take it by value. So it shouldn't matter?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I vote to keep it this way, merge this PR, and create an issue/discussion on why you can't call the int_ conversion on a handle (like you can for str).

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm okay with that.

@YannickJadoul
Copy link
Collaborator

I've taken the liberty to rebase your PR onto the latest master, hopefully resolving any conflicts, @auscompgeek. I hope that's OK with you?

@YannickJadoul
Copy link
Collaborator

Successfully rebased, it seems :-) Does @bstaletic still want to say something (as you were involved in the discussion?)

Otherwise, this should be ready, @henryiii?

This changes enum reprs to look like `<Enum.name: value>` similarly to
the Python enum module.

This keeps the str of enums as `Enum.name`, like the Python enum module.
@YannickJadoul
Copy link
Collaborator

YannickJadoul commented Sep 19, 2020

Another rebase, after #2510. If CI turns green, I'll merge.

@YannickJadoul YannickJadoul merged commit 5e6ec49 into pybind:master Sep 19, 2020
@YannickJadoul
Copy link
Collaborator

Thanks, @auscompgeek!

henryiii added a commit to henryiii/pybind11 that referenced this pull request Sep 22, 2020
Fixed in pybind#2510 but reintroduced on one line by pybind#2126
YannickJadoul pushed a commit that referenced this pull request Sep 22, 2020
Fixed in #2510 but reintroduced on one line by #2126
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.

4 participants