C API String Parsing/Returning

k3xji sumerc at gmail.com
Mon Apr 6 14:13:33 CEST 2009

Hi all,

This might be a newbie question. I am trying to implement a simple
string decoder/encoder algorithm. Just suppose I am substrcating some
values from the string passed as a parameter to the function and I
want the function to return encoded/decoded version of the string.

Here is the call:
ss= esauth.penc('s')
st = esauth.pdec(ss)

static PyObject *
pdec(PyObject *self, PyObject *args)
	unsigned char *s= NULL;

	unsigned int v,len,i = 0;

	if (!PyArg_ParseTuple(args, "s", &s))
        return NULL;
	if (!s)
		return NULL;

	len = strlen(s);

	for(i=0;i<len;i++) {
                if (s[i] > 10)
		    s[i] = s[i] - 10;

	return Py_BuildValue("s",s);

This is returning the original string. I mean the parameter is changed
but the Py_BuildValue is returning the original string passed in as

 have dealt with another nmore complex extension and because of the
same string handling problems, I just stop implementing it. Can
somebody please briefly explain the gotchas in Python's string
handling and returning values, cause I am having real trouble with


More information about the Python-list mailing list