Skip to content

Instantly share code, notes, and snippets.

@ezyang
Created September 21, 2017 21:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ezyang/3edfe5f0f035d4f18c01d76c33ae6bd0 to your computer and use it in GitHub Desktop.
Save ezyang/3edfe5f0f035d4f18c01d76c33ae6bd0 to your computer and use it in GitHub Desktop.
pybind11 cast to py::object subclass is lazy
#include <pybind11/pybind11.h>
#include <iostream>
namespace py = pybind11;
int main() {
Py_Initialize();
auto gil = PyGILState_Ensure();
PyObject *l = PyFloat_FromDouble(3.14);
//PyObject *l = PyBytes_FromString("foo");
auto n = py::cast<py::bytes>(l);
std::cerr << "No problem yet...\n";
std::cerr << static_cast<std::string>(n);
PyGILState_Release(gil);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment