Interestaing I changed malloc()/free() usage with PyMem_xx APIs and the problem resolved. However, I really cannot understand why the first version does not work. Here is the latest code that has no problems at all:
static PyObject * penc(PyObject *self, PyObject *args) { PyObject * result = NULL; unsigned char *s= NULL; unsigned char *buf = NULL; unsigned int v,len,i = 0; if (!PyArg_ParseTuple(args, "s#", &s, &len)) return NULL; buf = (unsigned char *) PyMem_Malloc(len); if (buf == NULL) { PyErr_NoMemory(); return NULL; } /* string manipulation. */ result = PyString_FromStringAndSize((char *)buf, len); PyMem_Free(buf); return result; } -- http://mail.python.org/mailman/listinfo/python-list