Ticket #13332: django-unittest-refcount.diff

File django-unittest-refcount.diff, 4.5 KB (added by Alex Gaynor, 14 years ago)
  • django/test/client.py

    diff --git a/django/test/client.py b/django/test/client.py
    index d321f7f..e5a16b6 100644
    a b class Client(object):  
    221221        # callback function.
    222222        data = {}
    223223        on_template_render = curry(store_rendered_templates, data)
    224         signals.template_rendered.connect(on_template_render)
    225 
     224        signals.template_rendered.connect(on_template_render, dispatch_uid="template-render")
    226225        # Capture exceptions created by the handler.
    227         got_request_exception.connect(self.store_exc_info)
    228 
     226        got_request_exception.connect(self.store_exc_info, dispatch_uid="request-exception")
    229227        try:
    230             response = self.handler(environ)
    231         except TemplateDoesNotExist, e:
    232             # If the view raises an exception, Django will attempt to show
    233             # the 500.html template. If that template is not available,
    234             # we should ignore the error in favor of re-raising the
    235             # underlying exception that caused the 500 error. Any other
    236             # template found to be missing during view error handling
    237             # should be reported as-is.
    238             if e.args != ('500.html',):
    239                 raise
    240 
    241         # Look for a signalled exception, clear the current context
    242         # exception data, then re-raise the signalled exception.
    243         # Also make sure that the signalled exception is cleared from
    244         # the local cache!
    245         if self.exc_info:
    246             exc_info = self.exc_info
    247             self.exc_info = None
    248             raise exc_info[1], None, exc_info[2]
    249 
    250         # Save the client and request that stimulated the response.
    251         response.client = self
    252         response.request = request
    253 
    254         # Add any rendered template detail to the response.
    255         # If there was only one template rendered (the most likely case),
    256         # flatten the list to a single element.
    257         for detail in ('template', 'context'):
    258             if data.get(detail):
    259                 if len(data[detail]) == 1:
    260                     setattr(response, detail, data[detail][0]);
     228
     229            try:
     230                response = self.handler(environ)
     231            except TemplateDoesNotExist, e:
     232                # If the view raises an exception, Django will attempt to show
     233                # the 500.html template. If that template is not available,
     234                # we should ignore the error in favor of re-raising the
     235                # underlying exception that caused the 500 error. Any other
     236                # template found to be missing during view error handling
     237                # should be reported as-is.
     238                if e.args != ('500.html',):
     239                    raise
     240
     241            # Look for a signalled exception, clear the current context
     242            # exception data, then re-raise the signalled exception.
     243            # Also make sure that the signalled exception is cleared from
     244            # the local cache!
     245            if self.exc_info:
     246                exc_info = self.exc_info
     247                self.exc_info = None
     248                raise exc_info[1], None, exc_info[2]
     249
     250            # Save the client and request that stimulated the response.
     251            response.client = self
     252            response.request = request
     253
     254            # Add any rendered template detail to the response.
     255            # If there was only one template rendered (the most likely case),
     256            # flatten the list to a single element.
     257            for detail in ('template', 'context'):
     258                if data.get(detail):
     259                    if len(data[detail]) == 1:
     260                        setattr(response, detail, data[detail][0]);
     261                    else:
     262                        setattr(response, detail, data[detail])
    261263                else:
    262                     setattr(response, detail, data[detail])
    263             else:
    264                 setattr(response, detail, None)
     264                    setattr(response, detail, None)
    265265
    266         # Update persistent cookie data.
    267         if response.cookies:
    268             self.cookies.update(response.cookies)
     266            # Update persistent cookie data.
     267            if response.cookies:
     268                self.cookies.update(response.cookies)
     269
     270            return response
     271        finally:
     272            signals.template_rendered.disconnect(dispatch_uid="template-render")
     273            got_request_exception.disconnect(dispatch_uid="request-exception")
    269274
    270         return response
    271275
    272276    def get(self, path, data={}, follow=False, **extra):
    273277        """
Back to Top