1
0
mirror of https://github.com/django/django.git synced 2025-06-07 12:39:12 +00:00

Minimized try block in Client.request().

This commit is contained in:
Jon Dufresne 2019-03-11 07:39:59 -07:00 committed by Tim Graham
parent ff09add274
commit 45dfb3641a

View File

@ -482,45 +482,36 @@ class Client(RequestFactory):
got_request_exception.connect(self.store_exc_info, dispatch_uid=exception_uid) got_request_exception.connect(self.store_exc_info, dispatch_uid=exception_uid)
try: try:
response = self.handler(environ) response = self.handler(environ)
finally:
# Look for a signalled exception, clear the current context signals.template_rendered.disconnect(dispatch_uid=signal_uid)
# exception data, then re-raise the signalled exception. got_request_exception.disconnect(dispatch_uid=exception_uid)
# Also make sure that the signalled exception is cleared from # Look for a signaled exception, clear the current context exception
# the local cache! # data, then re-raise the signaled exception. Also clear the signaled
# exception from the local cache.
response.exc_info = self.exc_info response.exc_info = self.exc_info
if self.exc_info: if self.exc_info:
_, exc_value, _ = self.exc_info _, exc_value, _ = self.exc_info
self.exc_info = None self.exc_info = None
if self.raise_request_exception: if self.raise_request_exception:
raise exc_value raise exc_value
# Save the client and request that stimulated the response. # Save the client and request that stimulated the response.
response.client = self response.client = self
response.request = request response.request = request
# Add any rendered template detail to the response. # Add any rendered template detail to the response.
response.templates = data.get("templates", []) response.templates = data.get('templates', [])
response.context = data.get("context") response.context = data.get('context')
response.json = partial(self._parse_json, response) response.json = partial(self._parse_json, response)
# Attach the ResolverMatch instance to the response.
# Attach the ResolverMatch instance to the response
response.resolver_match = SimpleLazyObject(lambda: resolve(request['PATH_INFO'])) response.resolver_match = SimpleLazyObject(lambda: resolve(request['PATH_INFO']))
# Flatten a single context. Not really necessary anymore thanks to the
# Flatten a single context. Not really necessary anymore thanks to # __getattr__ flattening in ContextList, but has some edge case
# the __getattr__ flattening in ContextList, but has some edge-case # backwards compatibility implications.
# backwards-compatibility implications.
if response.context and len(response.context) == 1: if response.context and len(response.context) == 1:
response.context = response.context[0] response.context = response.context[0]
# Update persistent cookie data. # Update persistent cookie data.
if response.cookies: if response.cookies:
self.cookies.update(response.cookies) self.cookies.update(response.cookies)
return response return response
finally:
signals.template_rendered.disconnect(dispatch_uid=signal_uid)
got_request_exception.disconnect(dispatch_uid=exception_uid)
def get(self, path, data=None, follow=False, secure=False, **extra): def get(self, path, data=None, follow=False, secure=False, **extra):
"""Request a response from the server using GET.""" """Request a response from the server using GET."""