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

[core/python] Add a way to take ownership when converting to python #670

Open
duburcqa opened this issue Dec 8, 2023 · 0 comments
Open
Labels
core enhancement New feature or request P1 Mid priority issue python

Comments

@duburcqa
Copy link
Owner

duburcqa commented Dec 8, 2023

The point is to avoid copy when wrapping temporaries, which typically happens the case when parsing log files here and wrapping some methods. Consider using boost::python::numpy instead raw C-Python API calls for constructing numpy object. It may be easier to expose a new take_ownership attribute for numpy converters. Otherwise, set ownership manually by doing:

void destroyManagedCObject(void * capsule) {
    free(PyCapsule_GetPointer(capsule, NULL));
};

PyObject * obj = PyCapsule_New(buffer, NULL, static_cast<PyCapsule_Destructor>(&destroyManagedCObject));
PyArray_SetBaseObject(static_cast<PyArrayObject *>(array), obj)

Note that taking ownership it not going to be helpful if memory has been allocated on the stack and not on the heap. It should be fine for dynamic size Eigen objects but not for fixed size. Fortunately, It is easy to detect this edge-case at compile-time.

See also for reference:

@github-project-automation github-project-automation bot moved this to To do in Jiminy 1.9 Dec 8, 2023
@duburcqa duburcqa added core python P0 Highest priority issue labels Dec 8, 2023
@duburcqa duburcqa changed the title [core/python] Refactor numpy converters to stop using low-level C-Python API. [core/python] Add a way to take ownership when converting to python Dec 8, 2023
@duburcqa duburcqa added P1 Mid priority issue and removed P0 Highest priority issue labels Dec 9, 2023
@duburcqa duburcqa added the enhancement New feature or request label Mar 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core enhancement New feature or request P1 Mid priority issue python
Projects
Status: To do
Development

No branches or pull requests

1 participant