Skip to content

Commit

Permalink
forgot some wrap.cxx files for new tests
Browse files Browse the repository at this point in the history
  • Loading branch information
parcollet committed Nov 12, 2024
1 parent 45193ca commit dd779d7
Show file tree
Hide file tree
Showing 2 changed files with 236 additions and 0 deletions.
110 changes: 110 additions & 0 deletions test/any.wrap.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@

// C.f. https://numpy.org/doc/1.21/reference/c-api/array.html#importing-the-api
#define PY_ARRAY_UNIQUE_SYMBOL _cpp2py_ARRAY_API
#ifdef __clang__
// #pragma clang diagnostic ignored "-W#warnings"
#endif
#ifdef __GNUC__
#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
#pragma GCC diagnostic ignored "-Wcast-function-type"
#pragma GCC diagnostic ignored "-Wcpp"
#endif

#define C2PY_VERSION_MAJOR 0
#define C2PY_VERSION_MINOR 1

#include "c2py/c2py.hpp"
#include "any.cpp"

using c2py::operator"" _a;

// ==================== Wrapped classes =====================

#ifndef C2PY_HXX_DECLARATION_any_GUARDS
#define C2PY_HXX_DECLARATION_any_GUARDS

#endif

// ==================== enums =====================

// ==================== module classes =====================

template <> struct c2py::py_converter<opaque> : c2py::py_converter_as_any<opaque> {};
template <> struct c2py::py_converter<opaque2> : c2py::py_converter_as_any<opaque2> {};

// ==================== module functions ====================

// inc_opaque
static auto const fun_0 = c2py::dispatcher_f_kw_t{c2py::cfun(c2py::cast<opaque &>(&inc_opaque), "x")};

// inc_opaque2
static auto const fun_1 = c2py::dispatcher_f_kw_t{c2py::cfun(c2py::cast<opaque2 &>(&NN::inc_opaque2), "x")};

// make_opaque
static auto const fun_2 = c2py::dispatcher_f_kw_t{c2py::cfun(c2py::cast<>(&make_opaque))};

// make_opaque2
static auto const fun_3 = c2py::dispatcher_f_kw_t{c2py::cfun(c2py::cast<>(&NN::make_opaque2))};

// take_opaque
static auto const fun_4 = c2py::dispatcher_f_kw_t{c2py::cfun(c2py::cast<const opaque &>(&take_opaque), "x")};

// take_opaque2
static auto const fun_5 = c2py::dispatcher_f_kw_t{c2py::cfun(c2py::cast<const opaque2 &>(&NN::take_opaque2), "x")};

static const auto doc_d_0 = fun_0.doc({R"DOC( )DOC"});
static const auto doc_d_1 = fun_1.doc({R"DOC( )DOC"});
static const auto doc_d_2 = fun_2.doc({R"DOC( )DOC"});
static const auto doc_d_3 = fun_3.doc({R"DOC( )DOC"});
static const auto doc_d_4 = fun_4.doc({R"DOC( )DOC"});
static const auto doc_d_5 = fun_5.doc({R"DOC( )DOC"});
//--------------------- module function table -----------------------------

static PyMethodDef module_methods[] = {
{"inc_opaque", (PyCFunction)c2py::pyfkw<fun_0>, METH_VARARGS | METH_KEYWORDS, doc_d_0.c_str()},
{"inc_opaque2", (PyCFunction)c2py::pyfkw<fun_1>, METH_VARARGS | METH_KEYWORDS, doc_d_1.c_str()},
{"make_opaque", (PyCFunction)c2py::pyfkw<fun_2>, METH_VARARGS | METH_KEYWORDS, doc_d_2.c_str()},
{"make_opaque2", (PyCFunction)c2py::pyfkw<fun_3>, METH_VARARGS | METH_KEYWORDS, doc_d_3.c_str()},
{"take_opaque", (PyCFunction)c2py::pyfkw<fun_4>, METH_VARARGS | METH_KEYWORDS, doc_d_4.c_str()},
{"take_opaque2", (PyCFunction)c2py::pyfkw<fun_5>, METH_VARARGS | METH_KEYWORDS, doc_d_5.c_str()},
{nullptr, nullptr, 0, nullptr} // Sentinel
};

//--------------------- module struct & init error definition ------------

//// module doc directly in the code or "" if not present...
/// Or mandatory ?
static struct PyModuleDef module_def = {PyModuleDef_HEAD_INIT,
"any", /* name of module */
R"RAWDOC()RAWDOC", /* module documentation, may be NULL */
-1, /* size of per-interpreter state of the module, or -1 if the module keeps state in global variables. */
module_methods,
NULL,
NULL,
NULL,
NULL};

//--------------------- module init function -----------------------------

extern "C" __attribute__((visibility("default"))) PyObject *PyInit_any() {

if (not c2py::check_python_version("any")) return NULL;

// import numpy iff 'numpy/arrayobject.h' included
#ifdef Py_ARRAYOBJECT_H
import_array();
#endif

PyObject *m;

if (PyType_Ready(&c2py::wrap_pytype<c2py::py_range>) < 0) return NULL;

m = PyModule_Create(&module_def);
if (m == NULL) return NULL;

auto &conv_table = *c2py::conv_table_sptr.get();

conv_table[std::type_index(typeid(c2py::py_range)).name()] = &c2py::wrap_pytype<c2py::py_range>;

return m;
}
126 changes: 126 additions & 0 deletions test/ignore.wrap.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@

// C.f. https://numpy.org/doc/1.21/reference/c-api/array.html#importing-the-api
#define PY_ARRAY_UNIQUE_SYMBOL _cpp2py_ARRAY_API
#ifdef __clang__
// #pragma clang diagnostic ignored "-W#warnings"
#endif
#ifdef __GNUC__
#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
#pragma GCC diagnostic ignored "-Wcast-function-type"
#pragma GCC diagnostic ignored "-Wcpp"
#endif

#define C2PY_VERSION_MAJOR 0
#define C2PY_VERSION_MINOR 1

#include "c2py/c2py.hpp"
#include "ignore.cpp"

using c2py::operator"" _a;

// ==================== Wrapped classes =====================

#ifndef C2PY_HXX_DECLARATION_ignore_GUARDS
#define C2PY_HXX_DECLARATION_ignore_GUARDS
template <> constexpr bool c2py::is_wrapped<a_struct> = true;
#endif

// ==================== enums =====================

// ==================== module classes =====================

template <> inline const std::string c2py::cpp_name<a_struct> = "a_struct";
template <> inline constexpr auto c2py::tp_name<a_struct> = "ignore.AStruct";
template <> inline constexpr const char *c2py::tp_doc<a_struct> = R"DOC( * a: int)DOC";

static int synth_constructor_0(PyObject *self, PyObject *args, PyObject *kwargs) {
if (args and PyTuple_Check(args) and (PyTuple_Size(args) > 0)) {
PyErr_SetString(PyExc_RuntimeError, ("Error in constructing a_struct.\nNo positional arguments allowed. Use keywords arguments"));
return -1;
}
c2py::pydict_extractor de{kwargs};
try {
((c2py::wrap<a_struct> *)self)->_c = new a_struct{};
} catch (std::exception const &e) {
PyErr_SetString(PyExc_RuntimeError, ("Error in constructing a_struct from a Python dict.\n "s + e.what()).c_str());
return -1;
}
auto &self_c = *(((c2py::wrap<a_struct> *)self)->_c);
de("a", self_c.a, false);
return de.check();
}

template <> constexpr initproc c2py::tp_init<a_struct> = synth_constructor_0;

// ----- Method table ----
template <>
PyMethodDef c2py::tp_methods<a_struct>[] = {

{nullptr, nullptr, 0, nullptr} // Sentinel
};

constexpr auto doc_member_0 = R"DOC()DOC";
static PyObject *prop_get_dict_0(PyObject *self, void *) {
auto &self_c = *(((c2py::wrap<a_struct> *)self)->_c);
c2py::pydict dic;
dic["a"] = self_c.a;
dic["b"] = self_c.b;
dic["x_ignore_me"] = self_c.x_ignore_me;
return dic.new_ref();
}

// ----- Method table ----

template <>
constinit PyGetSetDef c2py::tp_getset<a_struct>[] = {c2py::getsetdef_from_member<&a_struct::a, a_struct>("a", doc_member_0),
{"__dict__", (getter)prop_get_dict_0, nullptr, "", nullptr},
{nullptr, nullptr, nullptr, nullptr, nullptr}};

// ==================== module functions ====================

//--------------------- module function table -----------------------------

static PyMethodDef module_methods[] = {
{nullptr, nullptr, 0, nullptr} // Sentinel
};

//--------------------- module struct & init error definition ------------

//// module doc directly in the code or "" if not present...
/// Or mandatory ?
static struct PyModuleDef module_def = {PyModuleDef_HEAD_INIT,
"ignore", /* name of module */
R"RAWDOC()RAWDOC", /* module documentation, may be NULL */
-1, /* size of per-interpreter state of the module, or -1 if the module keeps state in global variables. */
module_methods,
NULL,
NULL,
NULL,
NULL};

//--------------------- module init function -----------------------------

extern "C" __attribute__((visibility("default"))) PyObject *PyInit_ignore() {

if (not c2py::check_python_version("ignore")) return NULL;

// import numpy iff 'numpy/arrayobject.h' included
#ifdef Py_ARRAYOBJECT_H
import_array();
#endif

PyObject *m;

if (PyType_Ready(&c2py::wrap_pytype<c2py::py_range>) < 0) return NULL;
if (PyType_Ready(&c2py::wrap_pytype<a_struct>) < 0) return NULL;

m = PyModule_Create(&module_def);
if (m == NULL) return NULL;

auto &conv_table = *c2py::conv_table_sptr.get();

conv_table[std::type_index(typeid(c2py::py_range)).name()] = &c2py::wrap_pytype<c2py::py_range>;
CLAIR_C2PY_ADD_TYPE_OBJECT(a_struct, "AStruct");

return m;
}

0 comments on commit dd779d7

Please sign in to comment.