Skip to content

Instantly share code, notes, and snippets.

@steve-s
Last active July 21, 2022 13:07
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 steve-s/225d54f9e8d85e024c2c57605be36b34 to your computer and use it in GitHub Desktop.
Save steve-s/225d54f9e8d85e024c2c57605be36b34 to your computer and use it in GitHub Desktop.
#include "Python.h"
static PyObject *myabs(PyObject *self, PyObject *arg) {
return Py_Absolute(arg);
}
static PyMethodDef methods[] = {
{"myabs", myabs, METH_O, ""},
{NULL, NULL, 0, NULL}
};
static struct PyModuleDef module = {
.m_methods = methods
// ...
};
// -------------------------------------------
// The same with HPy:
#include "hpy.h"
HPyDef_METH(myabs, "myabs", myabs_impl, HPyFunc_O)
static HPy myabs_impl(HPyContext *ctx, HPy self, HPy arg) {
return HPy_Absolute(ctx, arg);
}
static HPyDef *methods[] = { &myabs, NULL };
static HPyModuleDef module = {
.defines = methods,
// ...
};
@steve-s
Copy link
Author

steve-s commented Jul 21, 2022

Used in a medium blog post about HPy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment