Python C API String Memory Consumption

k3xji sumerc at gmail.com
Tue Apr 7 04:43:41 EDT 2009


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;
}




More information about the Python-list mailing list