include python2 Python include stdio static PyObject factorial PyObjec

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
#include "python2.5/Python.h"
#include <stdio.h>
static PyObject *factorial(PyObject *self, PyObject *args) {
int limit;
int x, result = 1;
if (!PyArg_ParseTuple(args, "i", &limit)) return NULL;
for (x = 1; x <= limit; x++) result *= x;
return Py_BuildValue("i", result);
}
static PyMethodDef ExtMethods[] = {
{"factorial", factorial, METH_VARARGS, NULL}
};
PyMODINIT_FUNC initext(void) {
(void) Py_InitModule("ext", ExtMethods);
}