summaryrefslogtreecommitdiff
path: root/django
diff options
context:
space:
mode:
authorLuke Plant <L.Plant.98@cantab.net>2011-06-09 23:15:42 +0000
committerLuke Plant <L.Plant.98@cantab.net>2011-06-09 23:15:42 +0000
commitdb2f9bfae173b565215a9f7320d7a45d0532f2fe (patch)
treed558e4e6b16aadfa1cdc39cfce3d882f774e5153 /django
parentdff31de20aac85e4f4834c2466762cbf23fc0a7b (diff)
Fixed #16178 - Cleanup request classes' `__repr__()`
Thanks to julien for the patch. git-svn-id: http://code.djangoproject.com/svn/django/trunk@16350 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django')
-rw-r--r--django/core/handlers/modpython.py26
-rw-r--r--django/core/handlers/wsgi.py25
-rw-r--r--django/http/__init__.py52
-rw-r--r--django/views/debug.py31
4 files changed, 51 insertions, 83 deletions
diff --git a/django/core/handlers/modpython.py b/django/core/handlers/modpython.py
index f0c77015b4..5affea45c5 100644
--- a/django/core/handlers/modpython.py
+++ b/django/core/handlers/modpython.py
@@ -45,32 +45,6 @@ class ModPythonRequest(http.HttpRequest):
self._stream = self._req
self._read_started = False
- def __repr__(self):
- # Since this is called as part of error handling, we need to be very
- # robust against potentially malformed input.
- try:
- get = pformat(self.GET)
- except:
- get = '<could not parse>'
- if self._post_parse_error:
- post = '<could not parse>'
- else:
- try:
- post = pformat(self.POST)
- except:
- post = '<could not parse>'
- try:
- cookies = pformat(self.COOKIES)
- except:
- cookies = '<could not parse>'
- try:
- meta = pformat(self.META)
- except:
- meta = '<could not parse>'
- return smart_str(u'<ModPythonRequest\npath:%s,\nGET:%s,\nPOST:%s,\nCOOKIES:%s,\nMETA:%s>' %
- (self.path, unicode(get), unicode(post),
- unicode(cookies), unicode(meta)))
-
def get_full_path(self):
# RFC 3986 requires self._req.args to be in the ASCII range, but this
# doesn't always happen, so rather than crash, we defensively encode it.
diff --git a/django/core/handlers/wsgi.py b/django/core/handlers/wsgi.py
index 434f91ccf3..2acb3b1ee7 100644
--- a/django/core/handlers/wsgi.py
+++ b/django/core/handlers/wsgi.py
@@ -157,31 +157,6 @@ class WSGIRequest(http.HttpRequest):
self._stream = self.environ['wsgi.input']
self._read_started = False
- def __repr__(self):
- # Since this is called as part of error handling, we need to be very
- # robust against potentially malformed input.
- try:
- get = pformat(self.GET)
- except:
- get = '<could not parse>'
- if self._post_parse_error:
- post = '<could not parse>'
- else:
- try:
- post = pformat(self.POST)
- except:
- post = '<could not parse>'
- try:
- cookies = pformat(self.COOKIES)
- except:
- cookies = '<could not parse>'
- try:
- meta = pformat(self.META)
- except:
- meta = '<could not parse>'
- return '<WSGIRequest\nGET:%s,\nPOST:%s,\nCOOKIES:%s,\nMETA:%s>' % \
- (get, post, cookies, meta)
-
def get_full_path(self):
# RFC 3986 requires query string arguments to be in the ASCII range.
# Rather than crash if this doesn't happen, we encode defensively.
diff --git a/django/http/__init__.py b/django/http/__init__.py
index 066346c181..7f198a7b08 100644
--- a/django/http/__init__.py
+++ b/django/http/__init__.py
@@ -134,6 +134,53 @@ class Http404(Exception):
RAISE_ERROR = object()
+
+def build_request_repr(request, path_override=None, GET_override=None,
+ POST_override=None, COOKIES_override=None,
+ META_override=None):
+ """
+ Builds and returns the request's representation string. The request's
+ attributes may be overridden by pre-processed values.
+ """
+ # Since this is called as part of error handling, we need to be very
+ # robust against potentially malformed input.
+ try:
+ get = (pformat(GET_override)
+ if GET_override is not None
+ else pformat(request.GET))
+ except:
+ get = '<could not parse>'
+ if request._post_parse_error:
+ post = '<could not parse>'
+ else:
+ try:
+ post = (pformat(POST_override)
+ if POST_override is not None
+ else pformat(request.POST))
+ except:
+ post = '<could not parse>'
+ try:
+ cookies = (pformat(COOKIES_override)
+ if COOKIES_override is not None
+ else pformat(request.COOKIES))
+ except:
+ cookies = '<could not parse>'
+ try:
+ meta = (pformat(META_override)
+ if META_override is not None
+ else pformat(request.META))
+ except:
+ meta = '<could not parse>'
+ path = path_override if path_override is not None else request.path
+ return smart_str(u'<%s\npath:%s,\nGET:%s,\nPOST:%s,\nCOOKIES:%s,\nMETA:%s>' %
+ (request.__class__.__name__,
+ path,
+ unicode(get),
+ unicode(post),
+ unicode(cookies),
+ unicode(meta)))
+
+
class HttpRequest(object):
"""A basic HTTP request."""
@@ -146,11 +193,10 @@ class HttpRequest(object):
self.path = ''
self.path_info = ''
self.method = None
+ self._post_parse_error = False
def __repr__(self):
- return '<HttpRequest\nGET:%s,\nPOST:%s,\nCOOKIES:%s,\nMETA:%s>' % \
- (pformat(self.GET), pformat(self.POST), pformat(self.COOKIES),
- pformat(self.META))
+ return build_request_repr(self)
def get_host(self):
"""Returns the HTTP host using the environment or request headers."""
diff --git a/django/views/debug.py b/django/views/debug.py
index 7401a51da6..490d4bbecf 100644
--- a/django/views/debug.py
+++ b/django/views/debug.py
@@ -7,7 +7,7 @@ from pprint import pformat
from django.conf import settings
from django.http import (HttpResponse, HttpResponseServerError,
- HttpResponseNotFound, HttpRequest)
+ HttpResponseNotFound, HttpRequest, build_request_repr)
from django.template import (Template, Context, TemplateDoesNotExist,
TemplateSyntaxError)
from django.template.defaultfilters import force_escape, pprint
@@ -96,34 +96,7 @@ class ExceptionReporterFilter(object):
if request is None:
return repr(None)
else:
- # Since this is called as part of error handling, we need to be very
- # robust against potentially malformed input.
- try:
- get = pformat(request.GET)
- except:
- get = '<could not parse>'
- if request._post_parse_error:
- post = '<could not parse>'
- else:
- try:
- post = pformat(self.get_post_parameters(request))
- except:
- post = '<could not parse>'
- try:
- cookies = pformat(request.COOKIES)
- except:
- cookies = '<could not parse>'
- try:
- meta = pformat(request.META)
- except:
- meta = '<could not parse>'
- return smart_str(u'<%s\npath:%s,\nGET:%s,\nPOST:%s,\nCOOKIES:%s,\nMETA:%s>' %
- (request.__class__.__name__,
- request.path,
- unicode(get),
- unicode(post),
- unicode(cookies),
- unicode(meta)))
+ return build_request_repr(request, POST_override=self.get_post_parameters(request))
def get_post_parameters(self, request):
if request is None: