Skip to content

Instantly share code, notes, and snippets.

@antocuni
Created May 19, 2020 12:42
Show Gist options
  • Save antocuni/6b52406a05e059ba4c084400ff983a52 to your computer and use it in GitHub Desktop.
Save antocuni/6b52406a05e059ba4c084400ff983a52 to your computer and use it in GitHub Desktop.
HPy methods
#define METH_NOARGS 1
#define METH_O 2
typedef void* HPyContext;
typedef long HPy;
typedef HPy (*HPyMeth_NOARGS)(HPyContext, HPy);
typedef HPy (*HPyMeth_O)(HPyContext, HPy, HPy);
typedef struct {
const char *name;
union {
void *pfunc;
HPyMeth_NOARGS meth_noargs;
HPyMeth_O meth_o;
};
int flags;
} HPyMethodDef;
typedef struct {
const char *name;
int size;
HPyMethodDef **methods;
} HPyModuleDef;
#include "hpy.h"
static HPy foo_impl(HPyContext ctx, HPy self) {
}
HPyMethodDef foo = {
.name = "foo",
.meth_noargs = foo_impl,
.flags = METH_NOARGS
};
static HPy bar_impl(HPyContext ctx, HPy self, HPy arg) {
}
HPyMethodDef bar = {
.name = "bar",
.meth_o = bar_impl,
.flags = METH_O
};
#include <stddef.h>
#include <stdio.h>
#include "hpy.h"
// defined in mymethods.c
HPyMethodDef foo;
HPyMethodDef bar;
static HPyMethodDef *Methods[] = {&foo, &bar, NULL};
static HPyModuleDef moduledef = {
.name = "MyModule",
.size = -1,
.methods = Methods
};
int main(void)
{
HPyMethodDef **def = moduledef.methods;
while (*def) {
printf("name = %s, flags = %d, pfunc = %p\n", (*def)->name, (*def)->flags, (*def)->pfunc);
def++;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment