04.08.2014 Views

o_18ufhmfmq19t513t3lgmn5l1qa8a.pdf

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 17 ■ EXTENDING PYTHON 369<br />

Once you’ve got your function in place, there is still some extra wrapping needed to make<br />

your C code act as a module. But let’s get back to that once we’ve got a real example to work<br />

with, shall we?<br />

Palindromes, Detartrated 1 for Your Pleasure<br />

Without further ado, I give you the hand-coded Python C API version of the palindrome<br />

module (with some interesting new stuff added) in Listing 17-6.<br />

Listing 17-6. Palindrome Checking Again (palindrome2.c)<br />

#include <br />

static PyObject *is_palindrome(PyObject *self, PyObject *args) {<br />

int i, n;<br />

const char *text;<br />

int result;<br />

/* "s" means a single string: */<br />

if (!PyArg_ParseTuple(args, "s", &text)) {<br />

return NULL;<br />

}<br />

/* The old code, more or less: */<br />

n=strlen(text);<br />

result = 1;<br />

for (i=0; i

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!