Skip to content
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

[py lcm] Adjust CreateDefaultValue to avoid unique_ptr #22463

Merged

Conversation

jwnimmer-tri
Copy link
Collaborator

@jwnimmer-tri jwnimmer-tri commented Jan 14, 2025

Towards #21968.


This change is Reviewable

@jwnimmer-tri jwnimmer-tri added priority: low release notes: none This pull request should not be mentioned in the release notes labels Jan 14, 2025
@jwnimmer-tri jwnimmer-tri force-pushed the pybind11-lcm-serializer branch from a0ea1a8 to bba32b5 Compare January 14, 2025 18:18
@jwnimmer-tri
Copy link
Collaborator Author

+@SeanCurtis-TRI for feature review please (to help keep you tuned in to the party happening on channel #21968 -- it's rockin!).

Copy link
Contributor

@SeanCurtis-TRI SeanCurtis-TRI left a comment

Choose a reason for hiding this comment

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

FTR "Avoiding unique_ptr" is not clearly apparent here. It seems that the base class has introduced the python variant which can be overridden in python under the old name. But the old name still exists and still returns a unique pointer. But even as I write that, I'm dissatisfied with that characterization. Could you elaborate?

Reviewed 1 of 1 files at r1, all commit messages.
Reviewable status: LGTM missing from assignee SeanCurtis-TRI(platform), needs at least two assigned reviewers

@jwnimmer-tri
Copy link
Collaborator Author

jwnimmer-tri commented Jan 15, 2025

The first argument to any PYBIND11_OVERLOAD_...(...) macro is the return type we should expect back from the Python interpreter when the user overrides the virtual function in Python. That spot is where unique_ptr is disallowed. Here we change the return type in that first argument to be py::object.

Maybe the other thing to know is that the quoted string "CreateDefaultValue" is the name of the Python method the macro will try to call -- so the name of method that the Python code should override is unchanged by this PR.

Copy link
Contributor

@SeanCurtis-TRI SeanCurtis-TRI left a comment

Choose a reason for hiding this comment

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

To be perfectly frank, I still don't understand the mechanism for python overriding. In C++, CreateDefaultValue() on the trampoline class is marked final. But CreateDefaultValuePython() isn't virtual. But presumably, neither of that stops a python class from overriding CreateDefaultValues(). So, I simply have no mental map of the code path when CreateDefaultValue() gets invoked by a python class with an overridden CreateDefaultValues(). I presume that no one in python ever does anything with the symbol CreateDefaultValuePython(). This corner of the code is completely opaque to me.

So, I'm giving you my stamp, but it's value may be iffy.

:LGTM:

Reviewable status: needs at least two assigned reviewers

This reduces our reliance RLG/pybind11 custom unique_ptr semantics.
@jwnimmer-tri jwnimmer-tri force-pushed the pybind11-lcm-serializer branch from bba32b5 to 6f676d3 Compare January 15, 2025 15:33
@jwnimmer-tri
Copy link
Collaborator Author

Here's another try. Probably it's better?

In C++, CreateDefaultValue() on the trampoline class is marked final.

Yes. There are no further C++ subclasses of PySerializerInterface.

The pybind11 docs here might help https://pybind11.readthedocs.io/en/stable/advanced/classes.html#overriding-virtual-functions-in-python but they don't really explain the how. The contract of the macro is that it finds out whether this is a Python object, and if so it scans the subclass's table of methods to look for one named "CreateDefaultValue", and if so it calls it, and then embeds a return result_of_the_call; inside the macro. Or, if there is no method, then because this is the PURE flavor of the macro, it raises an exception "cannot call a pure virtual method".

Copy link
Contributor

@SeanCurtis-TRI SeanCurtis-TRI left a comment

Choose a reason for hiding this comment

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

Reviewed 1 of 1 files at r2, all commit messages.
Reviewable status: needs at least two assigned reviewers

Copy link
Contributor

@SeanCurtis-TRI SeanCurtis-TRI left a comment

Choose a reason for hiding this comment

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

Thanks. The link helped a bit. The role between trampoline and actual class is made more distinct (if not completely clear).

Reviewable status: needs at least two assigned reviewers

@jwnimmer-tri
Copy link
Collaborator Author

Maybe +@rpoyner-tri for platform review? No rush.

Copy link
Contributor

@rpoyner-tri rpoyner-tri left a comment

Choose a reason for hiding this comment

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

:lgtm:

Reviewed 1 of 1 files at r2, all commit messages.
Reviewable status: :shipit: complete! all discussions resolved, LGTM from assignees rpoyner-tri(platform),SeanCurtis-TRI(platform)

@rpoyner-tri rpoyner-tri merged commit 6794c4d into RobotLocomotion:master Jan 16, 2025
10 checks passed
@jwnimmer-tri jwnimmer-tri deleted the pybind11-lcm-serializer branch January 16, 2025 17:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority: low release notes: none This pull request should not be mentioned in the release notes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants