[Web-SIG] WSGI application object returning a string/buffer.

Graham Dumpleton graham.dumpleton at gmail.com
Sun Jun 3 08:06:21 CEST 2007

The WSGI specification says:

"""When called by the server, the application object must return an
iterable yielding zero or more strings. This can be accomplished in a
variety of ways, such as by returning a list of strings, or by the
application being a generator function that yields strings, or by the
application being a class whose instances are iterable. Regardless of
how it is accomplished, the application object must always return an
iterable yielding zero or more strings."""

This requirement would be satisfied by an application object returning
the response content using:

  # list of strings
  return ['s1','s2']

  # tuple of strings
  return ('s1','s2')

  # generator
  yield 's1'
  yield 's2'

One could also return any object which is iterable, provide it yields
strings for each item.

A consequence of this last option though is that the following are
actually valid:

  # string
  return 's1s2'

  # buffer
  return buffer('s1s2')

This is because when iterated over, the result is a string object for
each item albeit where each string object is of length 1.

Although string and buffer objects can be returned, one cannot however
return a unicode object. Ie., the following will fail:

  # unicode (fails)
  return u's1s2'

This fails because each item yield when iterated over is a unicode object.

For the string and buffer return value, although it is technically
legal by the specification, that it is also a requirement that data be
flushed between each yielded item from a sequence means that each
character is written back to the user one at a time. The result of
this is that performance will suck something terrible.

The question is, although returning a string object is technically
allowed by the specification, should a good WSGI adapter actually
allow it given that it is more than likely a programming mistake that
someone has done it this way rather than return the string object
enclosed in an array?

The buffer object is a more problematic case, as they can't even be
returned in an array as is, but would need to be converted to a string
object first before being put in the array.

Any comments or suggestion? Should I just process string and buffer
objects as a sequence without complaint as required by specification,
or disallow it based on probability that it isn't probably what the
user meant to do anyway?


More information about the Web-SIG mailing list