-
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
pybind11 cheatsheet documentation #1201
Comments
Thanks a lot! I really struggled with "C++ → py::object", which as mentioned here, is undocumented. It should at least be in the Reference section of the docs, if not in the type conversion section. |
It's actually documented here, albeit a bit briefly. |
The more intricate bits of ref counting, casting, etc., came up on Gitter here. If I have the time, I may try to incorporate this and a few other things in a PR for the docs (adding it to the currently terse docs). |
Slowly circling back, but relates #2362 -- documenting pytypes a bit more, perhaps with examples |
Agreed that the docs on these concepts should be expanded! Lots of these things can probably be added to https://pybind11.readthedocs.io/en/stable/advanced/pycpp/object.html? I would argue to try and fit them into the existing way the docs are structured and laid out, though:
|
I have found the pybind11 documentation to be not so great for quickly trying to figure out "how the heck do I get from type A to type B". So I wrote my own notes. I'm interesting in getting these back into the official docs, but I am not sure what is the most appropriate form.
Here goes.
The important types:
py::handle
- equivalent toPyObject*
(no automatic refcounting)py::object
- does automatic refcounting. Subclass of py::handle.Conversions
PyObject* ↔ py::handle
This is a trivial conversion. These two types are basically totally equivalent.
py::handle (or subclass) → py::object (or subclass)
The syntax here works for both py::object, and any of its subclasses (e.g., py::str, etc)
http://pybind11.readthedocs.io/en/stable/reference.html#_CPPv26object
All of these are unchecked. Do an isinstance check beforehand if necessary. (Note that if you try to do anything with the py::object subclass, pybind11 will probably throw an exception at that point, so you probably aren't completely hosed. But casts to py::foo things are not checked at the point you do the cast!)
Common gotcha: you MUST pass a template argument to py::cast if you're casting from a PyObject*, it will silently do the wrong thing if you don't.
py::object (or superclass) → py::handle
Stealing is pretty useful if you're trying to invoke a C API function that steals its argument.
NB: a “copy” from object into handle doesn't make sense, because handle doesn't know how to manage its own memory.
NB2: these conversions are implemented by the implicit copy-constructor on
py::handle
,static casting the inputpy::object
as aconst py::handle&
(in case you're like me and tried to find where in the code this conversion was implemented).py::object → C++
C++ → py::object
How do I...
The text was updated successfully, but these errors were encountered: