Skip to content

Instantly share code, notes, and snippets.

@qunaibit
Last active May 31, 2022 19:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qunaibit/efbf653995f77ef7b9f6625c70cc1422 to your computer and use it in GitHub Desktop.
Save qunaibit/efbf653995f77ef7b9f6625c70cc1422 to your computer and use it in GitHub Desktop.
HPy + CAPI
#include <Python.h>
static PyObject *classA_foo(PyObject *self, PyObject *arg)
{
Py_INCREF(arg);
return arg;
}
HPyDef_METH(classA_bar, "bar", classA_bar_impl, HPyFunc_NOARGS)
static HPy classA_bar_impl(HPyContext *ctx, HPy self)
{
return HPyLong_FromLong(ctx, 1234);
}
static PyMethodDef classA_methods[] = {
{"foo", classA_foo, METH_O},
{NULL, NULL} /* Sentinel */
};
static PyType_Slot classA_type_slots[] = {
{Py_tp_methods, classA_methods},
{0, 0},
};
static HPyDef *classA_type_defines[] = {
&classA_bar,
NULL
};
static HPyType_Spec classA_type_spec = {
.name = "ClassA",
.legacy = true,
.legacy_slots = classA_type_slots,
.defines = classA_type_defines
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment