-2

I'm using web2py framework and i'm getting the error <type 'exceptions.UnicodeEncodeError'> 'ascii' codec can't encode character u'\xc3' in position 12: ordinal not in range(128) only if i try to use the string value of self.servidor['NOME_SERVIDOR'] inside web2py's INPUT html helper: INPUT(_name='NOME_SERVIDOR', _type='text', _value=self.servidor['NOME_SERVIDOR'], _readonly='true').

Yes, i'm using # -*- coding: utf-8 -*- on top of the file.

# coding=utf-8
from gluon.html import *


def exampleControllerFunction():
    class FormAvaliacao(object):
        def __init__(self):
            self.servidor = current.session.dadosServidor
            self.tipo = current.session.avaliacaoTipo

        @property
        def exampleError(self):
            return FORM(
                INPUT(_name='NOME_SERVIDOR', _type='text', _value=self.servidor['NOME_SERVIDOR'], _readonly='true')
            )

        @property
        def exampleOk(self):
            return self.servidor['NOME_SERVIDOR']

    form1 = FormAvaliacao().exampleError
    form2 = FormAvaliacao().exampleOk

    return dict(form=form1)

As an example, the above code represents two situations where the same unicode string behaves differently.

ps.: I'm using the same string on many other parts of my code, and it only became a problem inside an INPUT.

Diogo Martins
  • 887
  • 7
  • 15
  • 1
    You have omitted two important elements from your post. First, there is no question in your post. StackOverflow is a question-and-answer site, but it's difficult to form a useful answer if there is no question. Second, you have not shown us where in your code it is failing. Please provide the shortest possible **complete** program that demonstrates your question. – Robᵩ Oct 28 '14 at 20:11
  • The `coding` comment line only affects the string literals in your code, it does *not* affect the way strings are treated within the program. It's not some kind of magic bullet. – Mark Ransom Oct 28 '14 at 20:32
  • Thanks for the useful comment, @Robᵩ . I edited my question. – Diogo Martins Oct 28 '14 at 20:37
  • @MarkRansom Thanks for explaining, but i already know that and i don't think that the source of my problem is in something like that. – Diogo Martins Oct 28 '14 at 20:40

1 Answers1

2

http://web2py.com/examples/static/epydoc/web2py.gluon.html-pysrc.html#

When you initalize the Input class, it calls self._postprocessing(), which in turn calls str(self['_value']). This is likely where the error occurs.

1766          if self['_value'] is None or isinstance(self['_value'],cgi.FieldStorage): 
1767              _value = None 
1768          else: 
1769              _value = str(self['_value'])
mehtunguh
  • 2,668
  • 17
  • 25