Path: blob/main/python/python-wasm/src/extension/hellomodule.c
1067 views
#include <Python.h>1#include <unistd.h>23static PyObject *hello(PyObject *self, PyObject *args) {4const char *name;5if (!PyArg_ParseTuple(args, "s", &name)) {6return NULL;7}8printf("Hello %s, from C!\n", name);9Py_RETURN_NONE;10}1112static PyObject *add389(PyObject *self, PyObject *args) {13const long n;14if (!PyArg_ParseTuple(args, "l", &n)) {15return NULL;16}17return PyLong_FromLong(n + 389);18}1920long gcd(long a, long b) {21long c;22long a0 = a;23long b0 = b;24while (b0 != 0) {25c = a0 % b0;26a0 = b0;27b0 = c;28}29return a0;30}31static PyObject *gcd_impl(PyObject *self, PyObject *args) {32const long n, m;33if (!PyArg_ParseTuple(args, "ll", &n, &m)) {34return NULL;35}36return PyLong_FromLong(gcd(n, m));37}38394041static PyMethodDef module_methods[] = {42{"hello", _PyCFunction_CAST(hello), METH_VARARGS, "Say hello to you."},43{"add389", _PyCFunction_CAST(add389), METH_VARARGS, "Add 389 to a C long."},44{"gcd", _PyCFunction_CAST(gcd_impl), METH_VARARGS, "GCD of two C longs."},45{NULL, NULL, 0, NULL}};4647static int module_clear(PyObject *module) { return 0; }4849static void module_free(void *module) { module_clear((PyObject *)module); }5051struct PyModuleDef _hellomodule = {52.m_name = "hello",53.m_methods = module_methods,54.m_clear = module_clear,55.m_free = module_free,56};5758PyMODINIT_FUNC PyInit_hello(void) {59// printf("PyInit_hello\n");60// initialize the module:61return PyModuleDef_Init(&_hellomodule);62}6364