-
Hi, I have some legacy codes that use pybind11 and reply on operations on A problem I hit is that the I still can get the dictionary object in C++ codes, but it seems like a dirty hack: #include "nanobind/nanobind.h"
#include "nanobind/stl/optional.h"
namespace nb = nanobind;
using namespace nb::literals;
struct A{};
static PyObject **nb_dict_ptr(PyObject *self) {
PyTypeObject *tp = Py_TYPE(self);
return (PyObject **) ((uint8_t *) self + tp->tp_dictoffset);
}
NB_MODULE(test_nanobind_bad_cast, m) {
nb::class_<A>(m, "A", nb::dynamic_attr())
.def("__init__", [](A *holder) {
new (holder) A{};
});
m.def("_get_dict", [](nanobind::handle self) {
return nanobind::handle(*nb_dict_ptr(self.ptr()));
});
} |
Beta Was this translation helpful? Give feedback.
Answered by
wjakob
Aug 22, 2023
Replies: 1 comment 1 reply
-
IIRC the dictionaries are created on demand when you first write to them (this behavior may depend on the Python version) |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
zhqrbitee
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
IIRC the dictionaries are created on demand when you first write to them (this behavior may depend on the Python version)