Hi,

I have this method in my controllers:

@expose()
def encode_string(self, enc="utf-8"):
    s = u'áéó'.encode(enc)
    return s

When I access "/encode_string" at Safari or Firefox everything is
right, but when I change the encoding on the url like this: "/
encode_string?enc=iso-8859-1" it works OK on Firefox but raises an
UnicodeDecodeError on Safari.

And if I put a "content_type" or "format" option in the expose
decorator it doesn't respect it, overwriting the option with the
project's default encoding (utf-8).

The traceback:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/2.4/lib/
python2.4/site-packages/CherryPy-2.2.1-py2.4.egg/cherrypy/
_cphttptools.py", line 105, in _run
    self.main()
  File "/Library/Frameworks/Python.framework/Versions/2.4/lib/
python2.4/site-packages/CherryPy-2.2.1-py2.4.egg/cherrypy/
_cphttptools.py", line 254, in main
    body = page_handler(*virtual_path, **self.params)
  File "<string>", line 3, in encode_string
  File "/Library/Frameworks/Python.framework/Versions/2.4/lib/
python2.4/site-packages/TurboGears-1.0.3-py2.4.egg/turbogears/
controllers.py", line 342, in expose
    output = database.run_with_transaction(
  File "<string>", line 5, in run_with_transaction
  File "/Library/Frameworks/Python.framework/Versions/2.4/lib/
python2.4/site-packages/TurboGears-1.0.3-py2.4.egg/turbogears/
database.py", line 307, in so_rwt
    retval = func(*args, **kw)
  File "<string>", line 5, in _expose
  File "/Library/Frameworks/Python.framework/Versions/2.4/lib/
python2.4/site-packages/TurboGears-1.0.3-py2.4.egg/turbogears/
controllers.py", line 359, in <lambda>
    mapping, fragment, args, kw)))
  File "/Library/Frameworks/Python.framework/Versions/2.4/lib/
python2.4/site-packages/TurboGears-1.0.3-py2.4.egg/turbogears/
controllers.py", line 399, in _execute_func
    return _process_output(output, template, format, content_type,
mapping, fragment)
  File "/Library/Frameworks/Python.framework/Versions/2.4/lib/
python2.4/site-packages/TurboGears-1.0.3-py2.4.egg/turbogears/
controllers.py", line 103, in _process_output
    output = output.decode(enc)
  File "/Library/Frameworks/Python.framework/Versions/2.4//lib/
python2.4/encodings/utf_8.py", line 16, in decode
    return codecs.utf_8_decode(input, errors, True)
UnicodeDecodeError: 'utf8' codec can't decode bytes in position 0-2:
unsupported Unicode code range

Inside the file "TurboGears-1.0.3-py2.4.egg/turbogears/controllers.py"
I found this piece of code. The 3rd line is decoding my string with a
wrong encoding:

if ua.browser == "safari":
    if isinstance(output, str):
        output = output.decode(enc)
    elif isinstance(output, types.GeneratorType):
        output = "".join(output)

    output = unicodechars.sub(
        lambda m: "&#x%x;" % ord(m.group(1)), output).encode("ascii")

If I comment these lines everything works OK on Firefox and Safari.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"TurboGears" 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/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to