|
12 | 12 | namespace py = pybind11; |
13 | 13 |
|
14 | 14 | #define DOC(function) \ |
15 | | - (std::string("See documentation of underlying C++ API: :cpp:func:`cppcolormap::") + \ |
| 15 | + (std::string("See documentation of C++ API: :cpp:func:`cppcolormap::") + \ |
16 | 16 | std::string(function) + std::string("`")) \ |
17 | 17 | .c_str() |
18 | 18 |
|
19 | 19 | #define ENUM(function) \ |
20 | | - (std::string("See documentation of underlying C++ API: :cpp:enum:`cppcolormap::") + \ |
| 20 | + (std::string("See documentation of C++ API: :cpp:enum:`cppcolormap::") + \ |
21 | 21 | std::string(function) + std::string("`")) \ |
22 | 22 | .c_str() |
23 | 23 |
|
| 24 | +/** |
| 25 | +Overrides the `__name__` of a module. |
| 26 | +Classes defined by pybind11 use the `__name__` of the module as of the time they are defined, |
| 27 | +which affects the `__repr__` of the class type objects. |
| 28 | +*/ |
| 29 | +class ScopedModuleNameOverride { |
| 30 | +public: |
| 31 | + explicit ScopedModuleNameOverride(py::module m, std::string name) : module_(std::move(m)) |
| 32 | + { |
| 33 | + original_name_ = module_.attr("__name__"); |
| 34 | + module_.attr("__name__") = name; |
| 35 | + } |
| 36 | + ~ScopedModuleNameOverride() |
| 37 | + { |
| 38 | + module_.attr("__name__") = original_name_; |
| 39 | + } |
| 40 | + |
| 41 | +private: |
| 42 | + py::module module_; |
| 43 | + py::object original_name_; |
| 44 | +}; |
| 45 | + |
24 | 46 | PYBIND11_MODULE(_cppcolormap, m) |
25 | 47 | { |
26 | 48 |
|
| 49 | + // Ensure members to display as `cppcolormap.X` rather than `cppcolormap._cppcolormap.X` |
| 50 | + ScopedModuleNameOverride name_override(m, "cppcolormap"); |
| 51 | + |
27 | 52 | xt::import_numpy(); |
28 | 53 |
|
29 | 54 | m.doc() = "Library with colormaps"; |
@@ -271,6 +296,6 @@ PYBIND11_MODULE(_cppcolormap, m) |
271 | 296 | .value("perceptual", cppcolormap::metric::perceptual) |
272 | 297 | .export_values(); |
273 | 298 |
|
274 | | - m.def("match", &cppcolormap::match); |
| 299 | + m.def("match", &cppcolormap::match, DOC("match")); |
275 | 300 |
|
276 | 301 | } // PYBIND11_MODULE |
0 commit comments