Replies: 1 comment 2 replies
-
It's not obvious how a Python |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hey !
I'm working with managed container. My type expose a
capsule
, a pure C++ type that hold a pointer alongside a reference count.Creating a
nanobind::capsule
from this and giving it to ananobind::ndarray
is straightforward. This setup allows the python type to safely utilize the buffer while keeping it alive. However, doing the opposite present some challenges.First, once created,
nanobind
doesn't exposendarray_handle
struct preventing me to access the owner member of this type. A potential solution could be to add an accessor function:A more intricate issue arises when trying to retrieve the capsule from a Python type. When calling
ndarray_import
,owner
is always set tonullptr
, even if a capsule exists. Nevertheless, the capsule may be found (underbase.obj
when usingnumpy
, not sure for the other frameworks though). The remaining issue is that to access the capsule, I need to know the internalnb_ndarray
type which exposendarray_handle *th
. One solution could be:ndarray_handle *get_ndrray_handle(handle h);
This function would attempt to cast
h
tonb_ndarray
and then return theth
member.Alternatively, we could perform this in
ndarray_import
and setowner
if found.Doing that would allow me the check is the
capsule
is mine and if so, just increment the ref count. Or just copy thendarray
into my owncapsule
to keep it alive.What do you think of this ? Thank you !
Beta Was this translation helpful? Give feedback.
All reactions