pybind11 icon indicating copy to clipboard operation
pybind11 copied to clipboard

Binding a class/enum in multiple modules fails for statically linked code despite specifying module_local()

Open rfhaque opened this issue 5 years ago • 1 comments

In our code, an enumeration needs to be wrapped in multiple modules. The entire code is statically linked into a single executable. Despite specifying py::module_local() for the enumeration binding, the code still fails with the following error: ImportError: generic_type: type "type" is already registered!

The error seems to be due to a function static variable in the function registered_local_types_cpp() defined in include/pybind11/detail/internals.h

Is is possible to make the local type_info map actually unique for each module? The same problem would manifest when binding an STL container in multiple modules.

The following code demonstrates the issue.

File: example1.cc

#include "pybind11/pybind11.h" enum Color { RED, BLACK }; namespace py = pybind11; PYBIND11_MODULE(example1, m) { py::enum_<Color>(m, "Color", py::module_local()) .value("RED", Color::RED).export_values(); }

File: example2.cc

#include "pybind11/pybind11.h" enum Color { RED, BLACK }; namespace py = pybind11; PYBIND11_MODULE(example2, m) { py::enum_<Color>(m, "Color", py::module_local()) .value("BLACK", Color::BLACK).export_values(); }

File: main.cc

#include <Python.h> extern "C" { void initexample1(); void initexample2(); } int main(int argc, char *argv[]) { Py_Initialize(); PyImport_AppendInittab("example1", initexample1); PyImport_AppendInittab("example2", initexample2); Py_Main(argc, argv); }

rfhaque avatar May 18 '19 06:05 rfhaque

Is this functionality still not available?

SooluThomas avatar Oct 23 '23 12:10 SooluThomas