Opened 5 years ago

Closed 5 years ago

Last modified 3 years ago

#13684 closed Bug (fixed)

a bug in a catch-all try..except in Django masks other exceptions: local variable 'resolver' referenced before assignment

Reported by: akaihola Owned by: nobody
Component: HTTP handling Version: 1.3
Severity: Normal Keywords:
Cc: techtonik@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I ran into this when my custom settings for running unit tests didn't include ROOT_URLCONF, and others have seen this too
(1,
2,
3).

In django.core.handlers.base.get_response, the resolver variable is defined inside a try: block and referenced in the final catch-all except: block. I'd move setting up the default url resolver outside the try..except block since a problem there probably prevents rendering 500 pages.

Attachments (3)

13684_local_variable_resolver.1.diff (945 bytes) - added by akaihola 5 years ago.
moves default url resolver setup outside try..except
django-core-handlers-base-1.diff (910 bytes) - added by brendoncrawford 5 years ago.
django-core-handlers-base-2.diff (1.0 KB) - added by sbc 5 years ago.
updating to apply to head

Download all attachments as: .zip

Change History (12)

Changed 5 years ago by akaihola

moves default url resolver setup outside try..except

comment:1 Changed 5 years ago by akaihola

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

Here's the traceback I got before applying the patch:

Traceback (most recent call last):
  File "myapp/tests.py", line 14, in test_example
    self.client.post('/', {})
  File "django/django/django/test/client.py", line 322, in post
    response = self.request(**r)
  File "django/django/django/test/client.py", line 230, in request
    response = self.handler(environ)
  File "django/django/django/test/client.py", line 74, in __call__
    response = self.get_response(request)
  File "django/django/django/core/handlers/base.py", line 142, in get_response
    return self.handle_uncaught_exception(request, resolver, exc_info)
UnboundLocalError: local variable 'resolver' referenced before assignment

This is the proper traceback after applying the patch:

Traceback (most recent call last):
  File "myapp/tests.py", line 14, in test_example
    self.client.post('/', {})
  File "django/django/django/test/client.py", line 322, in post
    response = self.request(**r)
  File "django/django/django/test/client.py", line 230, in request
    response = self.handler(environ)
  File "django/django/django/test/client.py", line 74, in __call__
    response = self.get_response(request)
  File "django/django/django/core/handlers/base.py", line 73, in get_response
    urlconf = settings.ROOT_URLCONF
  File "django/django/django/utils/functional.py", line 277, in __getattr__
    return getattr(self._wrapped, name)
AttributeError: 'Settings' object has no attribute 'ROOT_URLCONF'

comment:2 Changed 5 years ago by akaihola

  • Summary changed from Django bug masks real errors: local variable 'resolver' referenced before assignment to a bug in a catch-all try..except in Django masks other exceptions: local variable 'resolver' referenced before assignment

Changed 5 years ago by brendoncrawford

comment:3 Changed 5 years ago by brendoncrawford

The first patch does not work for me. I have attached a patch above that works for me.

comment:4 Changed 5 years ago by russellm

  • milestone set to 1.3
  • Triage Stage changed from Unreviewed to Accepted

Changed 5 years ago by sbc

updating to apply to head

comment:5 Changed 5 years ago by Alex

  • Resolution set to fixed
  • Status changed from new to closed

(In [14488]) Fixed #13684 -- if settings.ROOT_URLCONF isn't defined don't blow up with an UnboundLocalError.

comment:6 Changed 4 years ago by jacob

  • milestone 1.3 deleted

Milestone 1.3 deleted

comment:7 Changed 3 years ago by anatoly techtonik <techtonik@…>

  • Easy pickings unset
  • Severity set to Normal
  • Type set to Uncategorized
  • UI/UX unset

In which version it is fixed?

comment:8 Changed 3 years ago by anatoly techtonik <techtonik@…>

  • Cc techtonik@… added

comment:9 Changed 3 years ago by anatoly techtonik <techtonik@…>

  • Type changed from Uncategorized to Bug
  • Version changed from SVN to 1.3

Fixed in Django 1.3. Not backported to 1.2

Note: See TracTickets for help on using tickets.
Back to Top