Created
May 19, 2020 12:42
-
-
Save antocuni/6b52406a05e059ba4c084400ff983a52 to your computer and use it in GitHub Desktop.
HPy methods
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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