#include static PyObject *is_palindrome(PyObject *self, PyObject *args) { int i, n; const char *text; int result; /* "s" means a single string: */ if (!PyArg_ParseTuple(args, "s", &text)) { return NULL; } /* The old code, more or less: */ n=strlen(text); result = 1; for (i = 0; i <= n/2; ++i) { if (text[i] != text[n-i-1]) { result = 0; break; } } /* "i" means a single integer: */ return Py_BuildValue("i", result); } /* A listing of our methods/functions: */ static PyMethodDef PalindromeMethods[] = { /* name, function, argument type, docstring */ {"is_palindrome", is_palindrome, METH_VARARGS, "Detect palindromes"}, /* An end-of-listing sentinel: */ {NULL, NULL, 0, NULL} }; static struct PyModuleDef palindrome = { PyModuleDef_HEAD_INIT, "palindrome", /* module name */ "", /* docstring */ -1, /* signals state kept in global variables */ PalindromeMethods }; /* An initialization function for the module: */ PyMODINIT_FUNC PyInit_palindrome(void) { return PyModule_Create(&palindrome); }