|
2 | 2 | #include <string> |
3 | 3 | #include <vector> |
4 | 4 |
|
5 | | -#include <libpy/abi.h> |
6 | 5 | #include <libpy/autoclass.h> |
| 6 | +#include <libpy/automodule.h> |
7 | 7 | #include <libpy/exception.h> |
8 | 8 |
|
9 | 9 | namespace libpy_tutorial { |
@@ -67,52 +67,23 @@ struct LIBPY_NO_EXPORT to_object<libpy_tutorial::vec3d> |
67 | 67 | } // namespace py::dispatch |
68 | 68 |
|
69 | 69 | namespace libpy_tutorial { |
70 | | -namespace { |
71 | | -PyModuleDef module = { |
72 | | - PyModuleDef_HEAD_INIT, |
73 | | - "libpy_tutorial.classes", |
74 | | - nullptr, |
75 | | - -1, |
76 | | - nullptr, |
77 | | - nullptr, |
78 | | - nullptr, |
79 | | - nullptr, |
80 | | - nullptr, |
81 | | -}; |
82 | | - |
83 | | -PyMODINIT_FUNC PyInit_classes() { |
84 | | - if (py::abi::ensure_compatible_libpy_abi()) { |
85 | | - return nullptr; |
86 | | - } |
87 | | - import_array(); |
88 | | - auto m = py::owned_ref(PyModule_Create(&module)); |
89 | | - if (!m) { |
90 | | - return nullptr; |
91 | | - } |
92 | | - try { |
93 | | - auto type = |
94 | | - py::autoclass<vec3d>("Vec3d") |
95 | | - .doc("An efficient 3-vector.") // add a class docstring |
96 | | - .new_<double, double, double>() //__new__ takes parameters |
97 | | - // bind the named methods to Python |
98 | | - .def<&vec3d::x>("x") |
99 | | - .def<&vec3d::y>("y") |
100 | | - .def<&vec3d::z>("z") |
101 | | - .def<&vec3d::magnitude>("magnitude") |
102 | | - .str() // set `operator<<(std::ostream&, vec3d) to `str(x)` in Python |
103 | | - .repr<repr>() // set `repr` to be the result of `repr(x)` in Python |
104 | | - .arithmetic<vec3d>() // bind the arithmetic operators to their Python |
105 | | - // equivalents |
106 | | - .type(); |
107 | | - if (PyObject_SetAttrString(m.get(), "Vec3d", static_cast<PyObject*>(type))) { |
108 | | - return nullptr; |
109 | | - } |
110 | | - } |
111 | | - catch (const std::exception& e) { |
112 | | - return py::raise_from_cxx_exception(e); |
113 | | - } |
114 | 70 |
|
115 | | - return std::move(m).escape(); |
| 71 | +LIBPY_AUTOMODULE(libpy_tutorial, classes, ({})) |
| 72 | +(py::borrowed_ref<> m) { |
| 73 | + py::owned_ref t = |
| 74 | + py::autoclass<vec3d>(PyModule_GetName(m) + ".Vec3d") |
| 75 | + .doc("An efficient 3-vector.") // add a class docstring |
| 76 | + .new_<double, double, double>() //__new__ takes parameters |
| 77 | + // bind the named methods to Python |
| 78 | + .def<&vec3d::x>("x") |
| 79 | + .def<&vec3d::y>("y") |
| 80 | + .def<&vec3d::z>("z") |
| 81 | + .def<&vec3d::magnitude>("magnitude") |
| 82 | + .str() // set `operator<<(std::ostream&, vec3d) to `str(x)` in Python |
| 83 | + .repr<repr>() // set `repr` to be the result of `repr(x)` in Python |
| 84 | + .arithmetic<vec3d>() // bind the arithmetic operators to their Python |
| 85 | + // equivalents |
| 86 | + .type(); |
| 87 | + return PyObject_SetAttrString(m.get(), "Vec3d", static_cast<PyObject*>(t)); |
116 | 88 | } |
117 | | -} // namespace |
118 | 89 | } // namespace libpy_tutorial |
0 commit comments