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

View File

@ -914,6 +914,22 @@ class TestRequest(unittest.TestCase):
self.assertEqual(used_req[0].path, '/hi/there')
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):
used_req = []