-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
How to interpret/expose a C++ raw memory as a numpy array *view* in Python? #2271
Comments
I had not been able to notice or reply to the discussion on Gitter in the 1 hour and a quarter before you posted this issue... But as replied there, now, the suggestion I made long time ago and said it might "maybe" work, doesn't: The solution is probably to just Alternatively, create any valid object (i.e., take As for the feature request: you can always make a PR, if you want, but there's a lot of memory management subtleties where Python and C++ mismatch. So that's probably why there isn't a feature like this. |
Another relevant link found by @bstaletic: https://gitter.im/pybind/Lobby?at=5a341ed6540c78242dcca87d |
A solution was obtained with the help of @YannickJadoul and @bstaletic, which is posted below for others' reference: py::array_t<int> get_indices(Group& g) {
return py::array_t<int>{5, g.indices, py::cast(g)};
} An alternative of I will leave this issue open for some while in case someone proposes a better solution 😊. |
I'm not sure there is a better solution? If your problem is solved, could you close it? There's already far too many open issues in this project :-) |
A |
Actually, it seems that the lifetime management here is okay, as you've declared it a readonly property, so |
That's a good point, indeed. I had not considered the combination of that mechanism with the So that would mean that even when not having |
One would have to do some tests to confirm, but I think one can have safety and Pythonic semantics here. |
No, you're right. I see no obvious reason why it wouldn't work.
You already can do so now, ofc. But this could make it easier, indeed. |
Hi, @molpopgen and @YannickJadoul , if I was not wrong, it seems the aforementioned Test 1: use
|
@molpopgen, @ShuhuaGao Right. From looking at the code, it seems the Sorry for the confusion; I hadn't thought of/considered that :-/ |
Unless I am mistaken, I think this is the only resource that gives a hint on how to return a numpy array, with a view on data of a C++ object, where the NumPy array keeps the C++ object alive. Just to summarize, this should be the example, where the returned numpy array does not copy the data, and keeps the Group C++ object alive:
At least my testing shows this to be the case, and I thought I would make this more explicit, so others might find this as well. |
This is a question or more likey a feature request.
Issue description
Supposing I have a block of memory in C++ (say, which may be an std::vector or a C array), how can I expose it as a numpy array to Python? More specifically, the numpy array should be a view rather than a copy of the memory, just like
numpy.frombuffer
orEigen::Map
.The discussion on gitter did not yield a solution. This issue is related to #1042
Reproducible example code
According to the suggestion by @YannickJadoul on gitter, I have tried the empty
py::capsule
, but it did not work. A minimal example is below.Test it in Python
It would be great if a similar
frombuffer
method is available forpy::array
andpy::array_t
(without copying and without ownership), though I am not sure whether it is too difficult to do so.The text was updated successfully, but these errors were encountered: