[Web-SIG] wsgi.errors and close method

Graham Dumpleton graham.dumpleton at gmail.com
Sun Mar 28 03:10:15 CEST 2010

On 28 March 2010 06:10, Manlio Perillo <manlio_perillo at libero.it> wrote:
> Hi.
> Some time ago, someone reported me that an application embedded in Nginx
> with my WSGI module failed to execute, since in my implementation the
> wsgi.errors object does not implement the .close method.
> The same object type is used to replace sys.stderr.
> Of course, both trying to close wsgi.errors and sys.stderr means an
> application/framework is broken, IMHO.

I'd agree, but there isn't much you can do about people/code not doing
the right thing. I had to give up on trying to get people to write
portable WSGI code and not arbitrarily try and read from or write to
sys.stdin and sys.stdout. It is a similar issue with things like
close() and various other methods that sys.stderr may supply. You just
have to provide a complete file like object implementation per Python
documentation requirements and have it appear that it did what was
requested and/or return an appropriate result for your system.

> Unfortunately I never got to know what application or framework was
> causing the problem.
> Any idea?

Looking at code, seems that for Apache/mod_wsgi I will notionally
close the log object, which means that if that did this for sys.stderr
standin, that any subsequent writes would result in a specific error
message to highlight that something wrong was done.

FWIW, the full set of methods and attributes I implement to make it
properly file like are:

static PyMethodDef Log_methods[] = {
    { "flush",      (PyCFunction)Log_flush,      METH_VARARGS, 0 },
    { "close",      (PyCFunction)Log_close,      METH_VARARGS, 0 },
    { "write",      (PyCFunction)Log_write,      METH_VARARGS, 0 },
    { "writelines", (PyCFunction)Log_writelines, METH_VARARGS, 0 },
    { "readable",   (PyCFunction)Log_readable,   METH_VARARGS, 0 },
    { "seekable",   (PyCFunction)Log_seekable,   METH_VARARGS, 0 },
    { "writable",   (PyCFunction)Log_writable,   METH_VARARGS, 0 },
    { NULL, NULL}

static PyGetSetDef Log_getset[] = {
    { "closed", (getter)Log_closed, NULL, 0 },
    { "softspace", (getter)Log_get_softspace, (setter)Log_set_softspace, 0 },
    { "encoding", (getter)Log_get_encoding, NULL, 0 },
    { "errors", (getter)Log_get_errors, NULL, 0 },
    { NULL },


More information about the Web-SIG mailing list