Commits

Yuya Nishihara committed a1e23e7

python: a little example of C extension

Comments (0)

Files changed (2)

python/cexthello/hellomodule.c

+#include <Python.h>
+#include <stdio.h>
+
+static PyObject *
+hello(PyObject *self, PyObject *args)
+{
+    const char *message;
+    if (!PyArg_ParseTuple(args, "s", &message))
+        return NULL;
+    printf("hello %s!\n", message);
+    Py_RETURN_NONE;
+}
+
+static PyMethodDef HelloMethods[] = {
+    {"hello", hello, METH_VARARGS,
+     "Say hello"},
+    {NULL, NULL, 0, NULL}
+};
+
+PyMODINIT_FUNC
+inithello(void)
+{
+    (void) Py_InitModule("hello", HelloMethods);
+}

python/cexthello/setup.py

+from distutils.core import setup, Extension
+
+setup(name='cexthello',
+      version='0.0',
+      author='Yuya Nishihara',
+      author_email='yuya@tcha.org',
+      ext_modules=[Extension('hello', ['hellomodule.c'])],
+    )