3

I've running Python 2.6.6.

cat tbuild.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#

from mako.template import Template

_template="""
% for v in my_list:
  ${'abc'.encode('utf-8')}
  ${'風連町?'.encode('utf-8')}
% endfor
"""


print Template(_template).render_unicode(my_list = [1, 2],
                                         input_encoding='utf-8',
                                         output_encoding='utf-8',
                                         encoding_errors='replace'
                                        )

./tbuild.py gives.
  File "./tbuild.py", 
  line 15, in <module> print Template(_template).render_unicode(my_list = [1, 2],
  File "/usr/lib/python2.6/site-packages/mako/template.py", 
  line 91, in __init__ (code, module) = _compile_text(self, text, filename)
  File "/usr/lib/python2.6/site-packages/mako/template.py", 
  line 357, in _compile_text node = lexer.parse()
  File "/usr/lib/python2.6/site-packages/mako/lexer.py", 
  line 192, in parse self.filename,)
  File "/usr/lib/python2.6/site-packages/mako/lexer.py", 
  line 184, in decode_raw_stream 0, 0, filename)
  mako.exceptions.CompileException: Unicode decode operation of 
  encoding 'ascii' failed at line: 0 char: 0

If I remove the line with Japanese it works fine. There clearly is something fundamental that I'm miss understanding.

Thanks for your help, eo

alditis
  • 4,633
  • 3
  • 49
  • 76
eorojas
  • 73
  • 7
  • This is Python basics, I don't see how it is related to Mako. See http://docs.python.org/2/howto/unicode.html http://docs.python.org/3/howto/unicode.html – mmgp Dec 06 '12 at 04:21

1 Answers1

2

I would be surprised even if ${'á'.encode('utf-8')} worked. You need to specify unicode strings as such, using the unicode literal u. Rewrite ${'風連町?'.encode('utf-8')} as ${u'風連町?'.encode('utf-8')} and do the same for any text that you are handling.

EDIT:

Taking mako into consideration:

# -*- coding: utf-8 -*-

from mako.template import Template

_template=u"${u'風連町?'}"
x = Template(_template, output_encoding='utf-8')
print x.render()

The output_encoding parameter makes sense when creating a Template, it has no meaning in the render method. Also, why would you encode input, decode input using same encoding, and then use render_unicode ? In fact, render_unicode ignores output_encoding, so it seems you actually want to use render.

mmgp
  • 18,901
  • 3
  • 53
  • 80
  • 1
    I tried that it didn't work either. I've tried every combination I could think of. – eorojas Dec 06 '12 at 04:59
  • Thank you that was very helpful! The code I posted was an iteration of what I started with. Now let's see what happens when I apply this to the real problem. – eorojas Dec 06 '12 at 15:16
  • It seems you need a better example then. Maybe you want `render_unicode`, but as it stands it is meaningless for the case presented. – mmgp Dec 06 '12 at 15:18
  • It would be nice user1364598 if you marked this, the @mmgp answer, as the answer to your question. – Dale E. Moore Mar 03 '13 at 17:09