Skip to content

Instantly share code, notes, and snippets.

@tekknolagi
Last active January 23, 2023 17:25
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 tekknolagi/f4acd2202f6448d4a5813a43eda90121 to your computer and use it in GitHub Desktop.
Save tekknolagi/f4acd2202f6448d4a5813a43eda90121 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stddef.h>
struct TypedMethodMetadata {
int type; // could be something similar to Cinder's TypedMethodDef
const char ml_name[100];
};
typedef struct TypedMethodMetadata TypedMethodMetadata;
// Same as CPython
struct PyMethodDef {
const char *ml_name;
void *ml_meth;
int ml_flags;
const char *ml_doc;
};
typedef struct PyMethodDef PyMethodDef;
// Can be generated by the clinic
TypedMethodMetadata foo = {12345, "method.foo"};
TypedMethodMetadata bar = {67890, "method.bar"};
PyMethodDef defs[] = {
// Can be generated by the clinic
{(const char*)foo.ml_name, NULL, 0, "foo doc"},
{(const char*)bar.ml_name, NULL, 0, "bar doc"},
{NULL, NULL},
};
int main() {
for (int i = 0; defs[i].ml_name != NULL; i++) {
TypedMethodMetadata* def = (TypedMethodMetadata*)(defs[i].ml_name - offsetof(TypedMethodMetadata, ml_name));
fprintf(stderr, "type is %10d\tname is %s\tdoc is %s\n", def->type, defs[i].ml_name, defs[i].ml_doc);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment