Commits

Anonymous committed 59af992

convsrc for Numpy template file conversion sample

  • Participants
  • Parent commits d600094

Comments (0)

Files changed (3)

 syntax: glob
 build/
+convsrc/*.c

convsrc/convsrcmodule.c.src

+/* -*- c -*- */
+#include <Python.h>
+
+static PyObject *
+fact(PyObject *self, PyObject *args)
+{
+  int n;
+  int i;
+  int ret=1;
+ 
+  if (!PyArg_ParseTuple(args, "i", &n))
+    return NULL;
+ 
+  for (i=n; i>0; i--) ret *= i;
+ 
+  return Py_BuildValue("i", ret);
+}
+
+static PyObject *
+hello(PyObject *self, PyObject *args)
+{
+  printf("Hello World!!\n");
+  Py_RETURN_NONE;
+}
+
+static PyObject *
+goodbye(PyObject *self, PyObject *args)
+{
+  printf("Goodbye!!\n");
+  Py_RETURN_NONE;
+}
+
+static char convsrc_doc[] = "C extention module example\n";
+
+static PyMethodDef methods[] = {
+/**begin repeat
+   #func=fact, hello, goodbye#
+*/
+  {"@func@", (PyCFunction)@func@, METH_VARARGS, "docstring of @func@.\n"},
+/**end repeat**/
+  {NULL, NULL}
+};
+
+#ifndef PyMODINIT_FUNC	/* declarations for DLL import/export */
+#define PyMODINIT_FUNC void
+#endif
+PyMODINIT_FUNC
+initconvsrc(void)
+{
+  Py_InitModule3("convsrc", methods, convsrc_doc);
+}
+from numpy.distutils.core import setup, Extension
+
+modname = 'convsrc'
+
+module1 = Extension(
+    modname,
+    sources = [modname + 'module.c.src'],
+    extra_compile_args = ["-fPIC"],
+    ## extra_compile_args = ["-fPIC", "-g", "-traceback"],
+)
+
+setup( ext_modules = [module1] )
+
+# compile: python setup.py build_ext -i -f