Opened 15 years ago

Closed 15 years ago

Last modified 13 years ago

#12083 closed (fixed)

technical_404_response should handle KeyError because 'tried' key might be not present

Reported by: Maciej Wiśniowski Owned by: nobody
Component: Core (Other) Version: 1.1
Severity: Keywords:
Cc: pigletto@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

django/views.py->technical_404_response expects key 'tried' to exist in args[0] dictionary:

def technical_404_response(request, exception):
    "Create a technical 404 error response. The exception should be the Http404."
    try:
        tried = exception.args[0]['tried']
    except (IndexError, TypeError):
        tried = []
    (...)

Above is not always true. For example: django/core/urlresolvers.py->RegexURLResolver.resolve can raise Resolver404 exception without 'tried' key.

                raise Resolver404, {'tried': tried, 'path': new_path}
        raise Resolver404, {'path' : path}

Possible solution is to add KeyError to list of exceptions in technical_404_response.

Attachments (1)

patch_12083.diff (2.4 KB ) - added by Maciej Wiśniowski 15 years ago.
proposed patch

Download all attachments as: .zip

Change History (7)

comment:1 by Russell Keith-Magee, 15 years ago

Resolution: worksforme
Status: newclosed

As a matter of principle, I'm marking this worksforme.

The most important part of a bug report is *the instructions for how to reproduce the bug*. I have no difficulty believing that you have seen this error. However, I am not going to apply a fix without an understanding of the underlying problem.

If you care to provide instructions on how to reproduce your problem (or better yet, a programatic test case for Django's test suite), feel free to reopen.

by Maciej Wiśniowski, 15 years ago

Attachment: patch_12083.diff added

proposed patch

comment:2 by Maciej Wiśniowski, 15 years ago

Has patch: set
Resolution: worksforme
Status: closedreopened

I've attached diff file with test case and patch included.

Failing test gives output like:

ERROR: test_404 (regressiontests.views.tests.debug.DebugViewTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/svnSandbox/django_dev/tests/regressiontests/views/tests/debug.py", line 24, in test_404
    response = self.client.get('/views/raises404/')
  File "/home/svnSandbox/django_dev/django/test/client.py", line 281, in get
    response = self.request(**r)
  File "/home/svnSandbox/django_dev/django/core/handlers/base.py", line 92, in get_response
    response = callback(request, *callback_args, **callback_kwargs)
  File "/home/svnSandbox/django_dev/tests/regressiontests/views/views.py", line 46, in raises404
    return technical_404_response(request, e)
  File "/home/svnSandbox/django_dev/django/views/debug.py", line 247, in technical_404_response
    tried = exception.args[0]['tried']
KeyError: 'tried'

The problem may appear only when request.path_info doesn't match regex r'/' defined for RegexURLResolver (urlresolvers.py):

    def get_resolver(urlconf):
        if urlconf is None:
            from django.conf import settings
            urlconf = settings.ROOT_URLCONF
        return RegexURLResolver(r'^/', urlconf)

    (...)
    
    class RegexURLResolver(object):
        def __init__(self, regex, urlconf_name, default_kwargs=None, app_name=None, namespace=None):
            # regex is a string representing a regular expression.
            # urlconf_name is a string representing the module containing URLconfs.
            self.regex = re.compile(regex, re.UNICODE)
            (...)

        def resolve(self, path):
            tried = []
            match = self.regex.search(path)
            if match:
                (...)
                raise Resolver404, {'tried': tried, 'path': new_path}
            raise Resolver404, {'path' : path}

I've found this issue when doing some (possibly dirty) hacking on request.path_info in my middleware class.
Otherwise it seems to be not possible to raise this exception as both WSGIRequest and ModPythonRequest are forcing request.path_info to be '/' instead of empty string. However, RegexURLResolver.resolve reflects this situation so provided patch might be desirable.

comment:3 by Russell Keith-Magee, 15 years ago

milestone: 1.2
Triage Stage: UnreviewedAccepted

comment:4 by Karen Tracey, 15 years ago

Resolution: fixed
Status: reopenedclosed

(In [12679]) Fixed #12083: Ensured generating debug 404 page won't raise a key error. Thanks pigletto.

comment:5 by Karen Tracey, 15 years ago

(In [12680]) [1.1.X] Fixed #12083: Ensured generating debug 404 page won't raise a key error. Thanks pigletto.

r12679 from trunk.

comment:6 by Jacob, 13 years ago

milestone: 1.2

Milestone 1.2 deleted

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