#6353: debug.technical_500_response fails on exceptions having localized text in
unicode
---------------------------------------------+------------------------------
          Reporter:  [EMAIL PROTECTED]  |         Owner:  mtredinnick
            Status:  reopened                |     Milestone:  1.0        
         Component:  Core framework          |       Version:  SVN        
        Resolution:                          |      Keywords:             
             Stage:  Accepted                |     Has_patch:  1          
        Needs_docs:  0                       |   Needs_tests:  0          
Needs_better_patch:  0                       |  
---------------------------------------------+------------------------------
Changes (by Karen Tracey <[EMAIL PROTECTED]>):

  * status:  closed => reopened
  * resolution:  fixed =>

Comment:

 You're going to hate me, but there were two problems referenced in this
 ticket, and sadly the change committed fixes neither of them.

 First the original reporter mentions raising Exceptions with non-ASCII
 data.  I was able to recreate this with a view that does:

 {{{
     from django.utils.translation import ugettext
     from django.utils import translation
     translation.activate('sv')
     raise Exception, ugettext("Only POSTs are allowed")
 }}}

 Using [8581] this view still generates a bare traceback page:

 {{{
 Traceback (most recent call last):

   File "/home/kmt/django/trunk/django/core/servers/basehttp.py", line 277,
 in run
     self.result = application(self.environ, self.start_response)

   File "/home/kmt/django/trunk/django/core/servers/basehttp.py", line 634,
 in __call__
     return self.application(environ, start_response)

   File "/home/kmt/django/trunk/django/core/handlers/wsgi.py", line 222, in
 __call__
     response = self.get_response(request)

   File "/home/kmt/django/trunk/django/core/handlers/base.py", line 128, in
 get_response
     return self.handle_uncaught_exception(request, resolver, exc_info)

   File "/home/kmt/django/trunk/django/core/handlers/base.py", line 148, in
 handle_uncaught_exception
     return debug.technical_500_response(request, *exc_info)

   File "/home/kmt/django/trunk/django/views/debug.py", line 39, in
 technical_500_response
     html = reporter.get_traceback_html()

   File "/home/kmt/django/trunk/django/views/debug.py", line 97, in
 get_traceback_html
     'exception_value': smart_unicode(self.exc_value, errors='replace'),

   File "/home/kmt/django/trunk/django/utils/encoding.py", line 35, in
 smart_unicode
     return force_unicode(s, encoding, strings_only, errors)

   File "/home/kmt/django/trunk/django/utils/encoding.py", line 51, in
 force_unicode
     s = unicode(str(s), encoding, errors)

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

 }}}

 Second jdemoor commented on seeing a similar problem when raising an
 Http404.  The traceback there was slightly different.  Changing the test
 view to be:

 {{{
     from django.utils.translation import ugettext
     from django.utils import translation
     translation.activate('sv')
     raise Http404, ugettext("Only POSTs are allowed")
 }}}

 and still using [8581], this also still generates a bare traceback:

 {{{
 Traceback (most recent call last):

   File "/home/kmt/django/trunk/django/core/servers/basehttp.py", line 277,
 in run
     self.result = application(self.environ, self.start_response)

   File "/home/kmt/django/trunk/django/core/servers/basehttp.py", line 634,
 in __call__
     return self.application(environ, start_response)

   File "/home/kmt/django/trunk/django/core/handlers/wsgi.py", line 222, in
 __call__
     response = self.get_response(request)

   File "/home/kmt/django/trunk/django/core/handlers/base.py", line 109, in
 get_response
     return debug.technical_404_response(request, e)

   File "/home/kmt/django/trunk/django/views/debug.py", line 259, in
 technical_404_response
     'reason': smart_str(exception, errors='replace'),

   File "/home/kmt/django/trunk/django/utils/encoding.py", line 79, in
 smart_str
     return unicode(s).encode(encoding, errors)

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

 }}}

 The provided patch included two parts.  First a change to
 django/utils/encoding.py in force_unicode to recognize when it was handed
 an Exception and force the Exception args individually to unicode.  That
 part was not in the commited change at all.  Adding it to [8581] fixes the
 error seen in the first view (the case where Exception was raised).
 Instead of the bare traceback I get a proper debug page:

 {{{
 Exception at /

 Endast POST är tillåtet

 Request Method:         GET
 Request URL:    http://lbox:8000/
 Exception Type:         Exception
 Exception Value:

 Endast POST är tillåtet
 }}}

 But the 2nd case of raising Http404 still fails:

 {{{
 Traceback (most recent call last):

   File "/home/kmt/django/trunk/django/core/servers/basehttp.py", line 277,
 in run
     self.result = application(self.environ, self.start_response)

   File "/home/kmt/django/trunk/django/core/servers/basehttp.py", line 634,
 in __call__
     return self.application(environ, start_response)

   File "/home/kmt/django/trunk/django/core/handlers/wsgi.py", line 222, in
 __call__
     response = self.get_response(request)

   File "/home/kmt/django/trunk/django/core/handlers/base.py", line 109, in
 get_response
     return debug.technical_404_response(request, e)

   File "/home/kmt/django/trunk/django/views/debug.py", line 259, in
 technical_404_response
     'reason': smart_str(exception, errors='replace'),

   File "/home/kmt/django/trunk/django/utils/encoding.py", line 79, in
 smart_str
     return unicode(s).encode(encoding, errors)

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

 }}}

 because I don't think it is smart_str that is needed there, but rather
 smart_unicode.  The 2nd part of the provided patch had smart_unicode
 there, not smart_str.  smart_str doesn't seem to do the job.  Changing it
 to smart_unicode results in a proper debug page:

 {{{

 Page not found (404)
 Request Method:         GET
 Request URL:    http://lbox:8000/

 Endast POST är tillåtet

 You're seeing this error because you have DEBUG = True in your Django
 settings file. Change that to False, and Django will display a standard
 404 page.

 }}}

 So to summarize, the Exception error is fixed with the change to
 encoding.py, but since that was not included in the commited change it was
 not fixed by the commit.
 The Http404 error needs both the change to encoding.py and deubg.py, plus
 it appears to need the originally proposed fix (smart_unicode), not
 smart_str as committed.

-- 
Ticket URL: <http://code.djangoproject.com/ticket/6353#comment:14>
Django Code <http://code.djangoproject.com/>
The web framework for perfectionists with deadlines
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to