-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Convert py::object Numpy array to py::array #1310
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
Comments
The built-in python wrappers like py::array_t<double> arr = np.attr("ones")(3); |
No. When the interpreter goes out of scope there won't actually be a Python object left and so there would be no way back into the object. It is possible, however, to have your data survive a interpreter shutdown and restart by having numpy operate on your own memory: pass a pointer to allocated memory (of at least the required size) into the |
Great, thanks @jagerman for the explanation! Would it make sense to add this kind of tiny example to the docs? I can make a PR. |
Sure; it would fit as a new section in It would also be worth mentioning that the implicit conversion does a type-check and possibly a conversion at runtime, raising an exception if the object type isn't right and conversion isn't possible. (For example, if you tried: |
FWIW, in my experience, it seems that NumPy is not robust against restarting an interpreter. From how the C API is imported, I don't think there's any trigger for C extensions to recognize when a new instance of NumPy is loaded, so it may have stale API entries (e.g. for |
Good to know! I'm now working on a project that makes use of |
I'd have closed this one, but:
So instead, this is just a ping. |
Thanks for the ping! I made a PR. |
I'm playing around with embedding the interpreter, and I'd like to access a Numpy array stored in a
py::object
using pybind11's array handling functionality, and possibly hand it off to other code.Here's a minimal example:
Here probably my ignorance of C++ shows, but how is this conversion done? Is there functionality for that in pybind11?
A follow-up on that question would then be: Can I stop the interpreter from deleting the object when the interpreter goes out of scope? If not, or if it requires too much hacking, I'd be okay with a copy.
If there's an elegant solution, I think it would be a nice addition to the docs on embedding the interpreter.
The text was updated successfully, but these errors were encountered: