#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)
Change History (7)
comment:1 by , 15 years ago
Resolution: | → worksforme |
---|---|
Status: | new → closed |
comment:2 by , 15 years ago
Has patch: | set |
---|---|
Resolution: | worksforme |
Status: | closed → reopened |
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 , 15 years ago
milestone: | → 1.2 |
---|---|
Triage Stage: | Unreviewed → Accepted |
comment:4 by , 15 years ago
Resolution: | → fixed |
---|---|
Status: | reopened → closed |
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.