Problem with non-ascii attributes #7

Open
phdru opened this Issue Apr 14, 2011 · 1 comment

Comments

Projects
None yet
1 participant

phdru commented Apr 14, 2011

Hi! First I got the problem with Cheetah 2.4.2 in Debian 6.0, but it seems latest Cheetah 2.4.4 has the same bug (if it's a bug and not an error on my side). I narrowed the problem to the following code in Compiler.py, line 1476:

def attributes(self):
    attribs = [self.setting('indentationStep') + str(attrib)
                  for attrib in self._generatedAttribs ]
    return '\n\n'.join(attribs)

The problem is in str(attrib) - if attrib is non-ascii unicode (I don't know how Cheetah works but it seems it converts the entire source to unicode) then str(attrib) fails. I cannot even compile the following simple template:

#encoding utf-8
#attr $test = 'тест' # this is 'test' in Russian

See:

$ cheetah compile test.tmpl
Compiling test.tmpl -> test.py
Traceback (most recent call last):
        ...
      File "/usr/lib/pymodules/python2.6/Cheetah/Compiler.py", line 1478, in attributes for attrib in self._generatedAttribs ]
UnicodeEncodeError: 'ascii' codec can't encode characters in position 8-11: ordinal not in range(128)

phdru commented Apr 14, 2011

Instead of str(attrib) there should be something like

if isinstance(attrib, unicode):
    value = attrib.encode(template_encoding)
else:
    value = str(attrib)

@phdru phdru referenced this issue in cheetahtemplate/cheetah Dec 25, 2016

Open

Fix python 3 stuff #9

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment