Skip to content

Commit 200da63

Browse files
committed
Implement suggestion to make tests cleaner
- add ctor to avoid cluttering of generated code
1 parent cd8fe83 commit 200da63

File tree

2 files changed

+5
-36
lines changed

2 files changed

+5
-36
lines changed

test/cls_basic.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ C2PY_MODULE_INIT void my_module_init() { std::cout << "===== Starting module !!=
115115

116116
struct C2PY_RENAME(renamed_class) some_class {
117117
int x = 0;
118+
some_class() = default;
118119
C2PY_RENAME(renamed_method) void some_method(int y) { x += y; }
119120
};
120121

test/cls_basic.wrap.cxx

Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -144,35 +144,9 @@ template <>
144144
const std::string c2py::tp_doc<dummy_class> =
145145
R"DOC(test implementation outside of class)DOC" + std::string{"\n\n----------\n\n"} + c2py::tp_ctor_doc<dummy_class>;
146146
template <> inline constexpr auto c2py::tp_name<some_class> = "cls_basic.renamed_class";
147-
148-
static int synth_constructor_0(PyObject *self, PyObject *args, PyObject *kwargs) {
149-
if (args and PyTuple_Check(args) and (PyTuple_Size(args) > 0)) {
150-
PyErr_SetString(PyExc_RuntimeError, ("Error in constructing some_class.\nNo positional arguments allowed. Use keywords arguments"));
151-
return -1;
152-
}
153-
c2py::pydict_extractor de{kwargs};
154-
try {
155-
((c2py::wrap<some_class> *)self)->_c = new some_class{};
156-
} catch (std::exception const &e) {
157-
PyErr_SetString(PyExc_RuntimeError, ("Error in constructing some_class from a Python dict.\n "s + e.what()).c_str());
158-
return -1;
159-
}
160-
auto &self_c = *(((c2py::wrap<some_class> *)self)->_c);
161-
de("x", self_c.x, true);
162-
return de.check();
163-
}
164-
165-
template <> constexpr initproc c2py::tp_init<some_class> = synth_constructor_0;
166-
167-
template <>
168-
const std::string c2py::tp_ctor_doc<some_class> = c2py::replace_tags(R"DOC(Synthesized constructor with the following keyword arguments:
169-
170-
Parameters
171-
----------
172-
x : {par_0}, default=0
173-
174-
)DOC",
175-
"par", {c2py::python_typename<int>()});
147+
static auto init_2 = c2py::dispatcher_c_kw_t{c2py::c_constructor<some_class>()};
148+
template <> constexpr initproc c2py::tp_init<some_class> = c2py::pyfkw_constructor<init_2>;
149+
template <> const std::string c2py::tp_ctor_doc<some_class> = init_2.doc(R"DOC()DOC");
176150
// renamed_method
177151
static auto const fun_10 = c2py::dispatcher_f_kw_t{c2py::cmethod([](some_class &self, int y) { return self.some_method(y); }, "self", "y")};
178152

@@ -186,18 +160,12 @@ PyMethodDef c2py::tp_methods<some_class>[] = {
186160
};
187161

188162
constexpr auto doc_member_3 = R"DOC()DOC";
189-
static PyObject *prop_get_dict_0(PyObject *self, void *) {
190-
auto &self_c = *(((c2py::wrap<some_class> *)self)->_c);
191-
c2py::pydict dic;
192-
dic["x"] = self_c.x;
193-
return dic.new_ref();
194-
}
195163

196164
// ----- Method table ----
197165

198166
template <>
199167
constinit PyGetSetDef c2py::tp_getset<some_class>[] = {c2py::getsetdef_from_member<&some_class::x, some_class>("x", doc_member_3),
200-
{"__dict__", (getter)prop_get_dict_0, nullptr, "", nullptr},
168+
201169
{nullptr, nullptr, nullptr, nullptr, nullptr}};
202170

203171
template <> const std::string c2py::tp_doc<some_class> = R"DOC()DOC" + c2py::tp_ctor_doc<some_class>;

0 commit comments

Comments
 (0)