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

[291] Nanobind for QueryAnswer and DASNode #299

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

Pedrobc89
Copy link
Collaborator

closes #291

@Pedrobc89 Pedrobc89 self-assigned this Mar 12, 2025
@Pedrobc89 Pedrobc89 requested a review from angeloprobst March 12, 2025 00:55
@Pedrobc89 Pedrobc89 changed the base branch from master to pedro/292/move-star-node March 12, 2025 00:57
Base automatically changed from pedro/292/move-star-node to master March 13, 2025 12:53
@Pedrobc89 Pedrobc89 marked this pull request as ready for review March 13, 2025 12:53
Comment on lines +33 to +35
//TODO: nanobind is failing to bind the handles field
// error: invalid conversion from 'const char* const*' to 'char'
// .def_ro("handles", &HandlesAnswer::handles)
Copy link
Collaborator

@angeloprobst angeloprobst Mar 13, 2025

Choose a reason for hiding this comment

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

@pedro-costa-techlabs const char* (or any other type of pointer) in nanobind is always challenging and should be avoided.

This particular case is even more complicated because handles is an array of pointers, so the binding implementation must translate it to a format that nanobind knows how to deal with, as follows:

#include <nanobind/stl/vector.h>
...
    .def_ro(
        "handles",
        [](const HandlesAnswer& self) -> const vector<string> {
            vector<string> handles;
            for(size_t i = 0; i < self.handles_size; i++) {
                handles.emplace_back(self.handles[i]);
            }
            return handles;
        })

nb::class_<HandlesAnswer, QueryAnswer>(m, "HandlesAnswer")
.def(nb::init<>())
.def(nb::init<double>(), "importance"_a)
.def(nb::init<const char*, double>(), "handle"_a, "importance"_a)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this could also require a special treatment for const char* handle:

    .def(
        "__init__",
        [](HandlesAnswer& self, const string& handle, double importance) {
            new (&self) HandlesAnswer(handle.c_str(), importance);
        },
        "handle"_a,
        "importance"_a)


// RemoteIterator.h
nb::class_<RemoteIterator<HandlesAnswer>>(m, "RemoteIterator")
.def(nb::init<string>(), "local_id"_a)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
.def(nb::init<string>(), "local_id"_a)
.def(nb::init<const string&>(), "local_id"_a)

Please revisit the other functions/constructors to ensure that the binding is expecting/passing in the right types, including aspect modifiers like const, &, etc.

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.

Expose QueryEngine to Python via nanobind
3 participants