Merge "Simplify wsgify()"

This commit is contained in:
Zuul 2020-06-02 19:53:58 +00:00 committed by Gerrit Code Review
commit b26d208b61
2 changed files with 25 additions and 18 deletions

View File

@ -43,7 +43,6 @@ from email.utils import parsedate
import re import re
import random import random
import functools import functools
import inspect
from io import BytesIO from io import BytesIO
import six import six
@ -1563,23 +1562,15 @@ def wsgify(func):
return a Response object into WSGI callables. Also catches any raised return a Response object into WSGI callables. Also catches any raised
HTTPExceptions and treats them as a returned Response. HTTPExceptions and treats them as a returned Response.
""" """
argspec = inspect.getargspec(func) @functools.wraps(func)
if argspec.args and argspec.args[0] == 'self': def _wsgify(*args):
@functools.wraps(func) env, start_response = args[-2:]
def _wsgify_self(self, env, start_response): new_args = args[:-2] + (Request(env), )
try: try:
return func(self, Request(env))(env, start_response) return func(*new_args)(env, start_response)
except HTTPException as err_resp: except HTTPException as err_resp:
return err_resp(env, start_response) return err_resp(env, start_response)
return _wsgify_self return _wsgify
else:
@functools.wraps(func)
def _wsgify_bare(env, start_response):
try:
return func(Request(env))(env, start_response)
except HTTPException as err_resp:
return err_resp(env, start_response)
return _wsgify_bare
class StatusMap(object): class StatusMap(object):

View File

@ -914,6 +914,22 @@ class TestRequest(unittest.TestCase):
self.assertEqual(used_req[0].path, '/hi/there') self.assertEqual(used_req[0].path, '/hi/there')
self.assertEqual(resp.status_int, 200) self.assertEqual(resp.status_int, 200)
def test_wsgify_method(self):
class _wsgi_class(object):
def __init__(self):
self.used_req = []
@swob.wsgify
def __call__(self, req):
self.used_req.append(req)
return swob.Response(b'200 OK')
req = swob.Request.blank('/hi/there')
handler = _wsgi_class()
resp = req.get_response(handler)
self.assertIs(handler.used_req[0].environ, req.environ)
self.assertEqual(resp.status_int, 200)
def test_wsgify_raise(self): def test_wsgify_raise(self):
used_req = [] used_req = []