Opened 17 years ago

Closed 17 years ago

#4990 closed (worksforme)

Problems in HttpResponse __str__ method.

Reported by: Thomas Güttler <hv@…> Owned by: nobody
Component: Uncategorized Version: dev
Severity: Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Malcolm Tredinnick)

Hi,

I got this exception:

UnicodeEncodeError at /.../
'ascii' codec can't encode character u'\xe4' in position 33614: ordinal not in range(128)

#  /home/modarch/wfbauantraege/wfba/middleware.py in process_exception
  # Send Debug Traceback as  Email to us
  81. html=smart_unicode(django.views.debug.technical_500_response(request, *sys.exc_info()), errors="replace") ...

# /home/modarch/python/lib64/python2.4/site-packages/django/utils/encoding.py in smart_unicode

  25. return force_unicode(s, encoding, strings_only, errors) ...

# /home/modarch/python/lib64/python2.4/site-packages/django/utils/encoding.py in force_unicode
  35. return s
  36. if not isinstance(s, basestring,):
  37.     if hasattr(s, '__unicode__'):
  38.         s = unicode(s)
  39.     else:

  40.         s = unicode(str(s), encoding, errors) ...

s is a django.http.HttpResponseServerError object.

This patch gives HttpResponse a __unicode__ method.

Attachments (1)

httpresponse-unicode.diff (413 bytes ) - added by Thomas Güttler <hv@…> 17 years ago.

Download all attachments as: .zip

Change History (4)

by Thomas Güttler <hv@…>, 17 years ago

Attachment: httpresponse-unicode.diff added

comment:1 by Simon G. <dev@…>, 17 years ago

Summary: HttpResponse.__unicode__Give HttpResponse a __unicode__ method
Triage Stage: UnreviewedReady for checkin

comment:2 by Malcolm Tredinnick, 17 years ago

Description: modified (diff)
Has patch: unset
Summary: Give HttpResponse a __unicode__ methodProblems in HttpResponse __str__ method.
Triage Stage: Ready for checkinAccepted

This patch doesn't look correct. The Unicode and str representations would only be identical if all the data was guaranteed to be ASCII and that's not true for the "content" attribute. Plus there should at least be a call to unicode() in there somewhere so that the right type is returned (rather than assuming Python's C layer will correct the problem, which is kind of accidental).

Thinking about the actual problem report, the whole approach looks like it's covering over a different problem: what should the encoding of the content be and how to represent that correctly as UTF-8 (in __str__) or as Unicode (which is what the __unicode__ method must return)?

More thinking is required here. No question that something is going wrong, but I need to trace through our encoding assumptions so that we can lay down the right rules for how to treat the content. It's not immediately clear that a __unicode__ method is needed, since an HttpResponse is always sent as a sequence of bytes.

comment:3 by Thomas Güttler <hv@…>, 17 years ago

Resolution: worksforme
Status: newclosed

The real problem was in views/debug.py. The error does not occur anymore.

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