Better handling of string-type responses from controllers

--HG--
branch : py3k
master
Dustin C. Hatch 2012-11-28 12:26:58 -06:00
parent afcbf3d314
commit 5735d2b027
1 changed files with 9 additions and 3 deletions

View File

@ -25,7 +25,6 @@ from milla.controllers import FaviconController
from milla.util import asbool from milla.util import asbool
from webob.exc import HTTPNotFound, WSGIHTTPException, HTTPMethodNotAllowed from webob.exc import HTTPNotFound, WSGIHTTPException, HTTPMethodNotAllowed
import milla.dispatch import milla.dispatch
import webob
__all__ = ['Application'] __all__ = ['Application']
@ -126,9 +125,16 @@ class Application(object):
func.func.im_self.__after__(request) func.func.im_self.__after__(request)
# The callable might have returned just a string, which is OK, # The callable might have returned just a string, which is OK,
# but we need to wrap it in a WebOb response # but we need to wrap it in a Response object
try:
# In Python 2, it could be a str or a unicode object
basestring = basestring #@UndefinedVariable
except NameError:
# Python 3 has no unicode objects and thus no need for
# basestring so we, just make it an alias for str
basestring = str
if isinstance(response, basestring) or not response: if isinstance(response, basestring) or not response:
response = webob.Response(response) response = request.ResponseClass(response)
if not start_response_wrapper.called: if not start_response_wrapper.called:
start_response(response.status, response.headerlist) start_response(response.status, response.headerlist)