Browse Source

Merge "Simplify wsgify()"

changes/89/732989/2
Zuul 2 years ago
committed by Gerrit Code Review
parent
commit
b26d208b61
  1. 27
      swift/common/swob.py
  2. 16
      test/unit/common/test_swob.py

27
swift/common/swob.py

@ -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):

16
test/unit/common/test_swob.py

@ -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 = []

Loading…
Cancel
Save