[Python-Dev] MemoryError... how much memory?

Facundo Batista facundobatista at gmail.com
Wed Oct 27 15:30:16 CEST 2010

There are a lot of places where Python or modules do something like:

    self->buf = (char *)malloc(size);
    if (!self->buf) {
              PyErr_SetString(PyExc_MemoryError, "out of memory");

At job, we're having some MemoryErrors, and one thing that we would
love to know, if how much memory it was asking when that happened.

So, I thought about doing something like:

    char message[50];
    self->buf = (char *)malloc(size);
    if (!self->buf) {
              snprintf(message, 50, "out of memory (asked: %lld)", size);
              PyErr_SetString(PyExc_MemoryError, &message);

Is any inherent problem in doing this?

May it be a good idea to make it generic, like providing a
PyErr_MemoryError that could accept a message and a number, and stores
that number in the exception objects internals?


.    Facundo

Blog: http://www.taniquetil.com.ar/plog/
PyAr: http://www.python.org/ar/

More information about the Python-Dev mailing list